1 /*
2  *  This file is part of nzbget. See <http://nzbget.net>.
3  *
4  *  Copyright (C) 2008-2019 Andrey Prygunkov <hugbug@users.sourceforge.net>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 #ifndef SCHEDULER_H
22 #define SCHEDULER_H
23 
24 #include "NString.h"
25 #include "Thread.h"
26 #include "Service.h"
27 
28 class Scheduler : public Service
29 {
30 public:
31 	enum ECommand
32 	{
33 		scPauseDownload,
34 		scUnpauseDownload,
35 		scPausePostProcess,
36 		scUnpausePostProcess,
37 		scDownloadRate,
38 		scExtensions,
39 		scProcess,
40 		scPauseScan,
41 		scUnpauseScan,
42 		scActivateServer,
43 		scDeactivateServer,
44 		scFetchFeed
45 	};
46 
47 	class Task
48 	{
49 	public:
Task(int id,int hours,int minutes,int weekDaysBits,ECommand command,const char * param)50 		Task(int id, int hours, int minutes, int weekDaysBits, ECommand command,
51 			const char* param) :
52 			m_id(id), m_hours(hours), m_minutes(minutes),
53 			m_weekDaysBits(weekDaysBits), m_command(command), m_param(param) {}
54 		friend class Scheduler;
55 		static const int STARTUP_TASK = -1;
56 	private:
57 		int m_id;
58 		int m_hours;
59 		int m_minutes;
60 		int m_weekDaysBits;
61 		ECommand m_command;
62 		CString m_param;
63 		time_t m_lastExecuted = 0;
64 	};
65 
66 	void AddTask(std::unique_ptr<Task> task);
67 
68 protected:
ServiceInterval()69 	virtual int ServiceInterval() { return m_serviceInterval; }
70 	virtual void ServiceWork();
71 
72 private:
73 	typedef std::vector<std::unique_ptr<Task>> TaskList;
74 	typedef std::vector<bool> ServerStatusList;
75 
76 	TaskList m_taskList;
77 	Mutex m_taskListMutex;
78 	time_t m_lastCheck = 0;
79 	bool m_downloadRateChanged;
80 	bool m_executeProcess;
81 	bool m_pauseDownloadChanged;
82 	bool m_pausePostProcessChanged;
83 	bool m_pauseScanChanged;
84 	bool m_serverChanged;
85 	ServerStatusList m_serverStatusList;
86 	bool m_firstChecked = false;
87 	int m_serviceInterval = 1;
88 
89 	void ExecuteTask(Task* task);
90 	void CheckTasks();
91 	void PrepareLog();
92 	void PrintLog();
93 	void EditServer(bool active, const char* serverList);
94 	void FetchFeed(const char* feedList);
95 	void CheckScheduledResume();
96 	void FirstCheck();
97 	void ScheduleNextWork();
98 };
99 
100 #endif
101