e5e2406b0527bd58aed0f0e84f37540d894225a9
[public/netxms.git] / src / server / include / nxcore_schedule.h
1 /*
2 ** NetXMS - Network Management System
3 ** Server Core
4 ** Copyright (C) 2015 Victor Kirhenshtein
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, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: nxcore_schedule.h
21 **
22 **/
23
24 #ifndef _nxcore_schedule_h_
25 #define _nxcore_schedule_h_
26
27 #include "nms_core.h"
28
29 #define NEVER 0
30
31 #define SCHEDULE_DISABLED 1
32 #define SCHEDULE_EXECUTED 2
33 #define SCHEDULE_IN_PROGRES 4
34 #define SCHEDULE_INTERNAL 5
35
36 class Schedule;
37 class ScheduleCallback;
38
39 typedef void (*scheduled_action_executor)(const TCHAR *params);
40
41 /**
42 * Static fields
43 */
44 static StringObjectMap<ScheduleCallback> s_callbacks(true);
45 static ObjectArray<Schedule> s_cronSchedules(5, 5, true);
46 static ObjectArray<Schedule> s_oneTimeSchedules(5, 5, true);
47 static CONDITION s_cond = ConditionCreate(false);
48
49 /**
50 * Static functions
51 */
52 void InitializeTaskScheduler();
53 void CloseTaskScheduler();
54 static THREAD_RESULT THREAD_CALL OneTimeEventThread(void *arg);
55 static THREAD_RESULT THREAD_CALL CronCheckThread(void *arg);
56 static bool IsItTime(struct tm *currTime, const TCHAR *schedule, time_t currTimestamp);
57 static int ScheduleListSortCallback(const void *e1, const void *e2);
58
59 /**
60 * Mutex for schedule structures
61 */
62 static MUTEX s_cronScheduleLock = MutexCreate();
63 static MUTEX s_oneTimeScheduleLock = MutexCreate();
64
65 class ScheduleCallback
66 {
67 public:
68 scheduled_action_executor m_func;
69 UINT64 m_accessRight;
70 ScheduleCallback(scheduled_action_executor func, UINT64 accessRight) { m_func = func; m_accessRight = accessRight; }
71 };
72
73 class Schedule
74 {
75 private:
76 UINT32 m_id;
77 TCHAR *m_taskId;
78 TCHAR *m_schedule;
79 TCHAR *m_params;
80 time_t m_executionTime;
81 time_t m_lastExecution;
82 int m_flags;
83 UINT32 m_owner;
84
85 public:
86
87 Schedule(int id, const TCHAR *taskId, const TCHAR *schedule, const TCHAR *params, UINT32 owner, int flags = 0);
88 Schedule(int id, const TCHAR *taskId, time_t executionTime, const TCHAR *params, UINT32 owner, int flags = 0);
89 Schedule(DB_RESULT hResult, int row);
90
91 ~Schedule(){ delete m_taskId; delete m_schedule; delete m_params; }
92
93 UINT32 getId() { return m_id; }
94 const TCHAR *getTaskId() { return m_taskId; }
95 const TCHAR *getSchedule() { return m_schedule; }
96 const TCHAR *getParams() { return m_params; }
97 time_t getExecutionTime() { return m_executionTime; }
98 UINT32 getOwner() { return m_owner; }
99
100 void setLastExecutionTime(time_t time) { m_lastExecution = time; };
101 void setExecutionTime(time_t time) { m_executionTime = time; }
102 void setFlag(int flag) { m_flags |= flag; }
103 void removeFlag(int flag) { m_flags &= ~flag; }
104
105 void update(const TCHAR *taskId, const TCHAR *schedule, const TCHAR *params, UINT32 owner);
106 void update(const TCHAR *taskId, time_t nextExecution, const TCHAR *params, UINT32 owner);
107
108 void saveToDatabase(bool newObject);
109 void run(ScheduleCallback *callback);
110 void fillMessage(NXCPMessage *msg);
111 void fillMessage(NXCPMessage *msg, UINT32 base);
112
113 bool checkFlag(int flag) { return (m_flags & flag) > 0 ? true : false; }
114 bool isInProgress() { return (m_flags & SCHEDULE_IN_PROGRES) > 0 ? true : false; }
115 bool canAccess(UINT32 user, UINT64 systemAccess);
116 };
117
118 void RegisterSchedulerTaskHandler(const TCHAR *id, scheduled_action_executor exec, UINT64 accessRight);
119 UINT32 AddSchedule(const TCHAR *task, const TCHAR *schedule, const TCHAR *params, UINT32 owner, UINT64 systemRights, int flags = 0);
120 UINT32 AddOneTimeSchedule(const TCHAR *task, time_t nextExecutionTime, const TCHAR *params, UINT32 owner, UINT64 systemRights, int flags = 0);
121 UINT32 UpdateSchedule(int id, const TCHAR *task, const TCHAR *schedule, const TCHAR *params, UINT32 owner, UINT32 systemAccessRights, int flags);
122 UINT32 UpdateOneTimeAction(int id, const TCHAR *task, time_t nextExecutionTime, const TCHAR *params, UINT32 owner, UINT64 systemAccessRights, int flags);
123 UINT32 RemoveSchedule(UINT32 id, UINT32 user, UINT64 systemRights);
124 void GetCallbackIdList(NXCPMessage *msg, UINT64 accessRights);
125 void GetSheduleList(NXCPMessage *msg, UINT32 user, UINT32 systemRights);
126 UINT32 UpdateScheduleFromMsg(NXCPMessage *request, UINT32 owner, UINT64 systemAccessRights);
127 UINT32 CreateScehduleFromMsg(NXCPMessage *request, UINT32 owner, UINT64 systemAccessRights);
128
129 #endif