fixed server crash when accessing scheduled tasks via API; minor refactoring
[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 #define NEVER 0
28
29 #define SCHEDULED_TASK_DISABLED 1
30 #define SCHEDULED_TASK_EXECUTED 2
31 #define SCHEDULED_TASK_RUNNING 4
32 #define SCHEDULED_TASK_SYSTEM 8
33
34 class ScheduledTask;
35 class SchedulerCallback;
36 class ScheduledTaskParameters;
37
38 typedef void (*scheduled_action_executor)(const ScheduledTaskParameters *params);
39
40 /**
41 * Scheduler callback
42 */
43 class SchedulerCallback
44 {
45 public:
46 scheduled_action_executor m_func;
47 UINT64 m_accessRight;
48
49 SchedulerCallback(scheduled_action_executor func, UINT64 accessRight) { m_func = func; m_accessRight = accessRight; }
50 };
51
52 /**
53 * Parameters for scheduled task
54 */
55 class ScheduledTaskParameters
56 {
57 public:
58 TCHAR *m_params;
59 UINT32 m_userId;
60 UINT32 m_objectId;
61
62 ScheduledTaskParameters(const TCHAR *param, UINT32 userId, UINT32 objectId) { m_params = _tcsdup(param); m_userId = userId; m_objectId = objectId; }
63 ScheduledTaskParameters() { m_params = NULL; }
64 ~ScheduledTaskParameters() { safe_free(m_params); }
65 };
66
67 /**
68 * Scheduled task
69 */
70 class ScheduledTask
71 {
72 private:
73 UINT32 m_id;
74 TCHAR *m_taskHandlerId;
75 TCHAR *m_schedule;
76 TCHAR *m_params;
77 time_t m_executionTime;
78 time_t m_lastExecution;
79 UINT32 m_flags;
80 UINT32 m_owner;
81 UINT32 m_objectId;
82
83 public:
84 ScheduledTask(int id, const TCHAR *taskHandlerId, const TCHAR *schedule, const TCHAR *params, UINT32 owner, UINT32 objectId, UINT32 flags = 0);
85 ScheduledTask(int id, const TCHAR *taskHandlerId, time_t executionTime, const TCHAR *params, UINT32 owner, UINT32 objectId, UINT32 flags = 0);
86 ScheduledTask(DB_RESULT hResult, int row);
87 ~ScheduledTask();
88
89 UINT32 getId() { return m_id; }
90 const TCHAR *getTaskHandlerId() { return m_taskHandlerId; }
91 const TCHAR *getSchedule() { return m_schedule; }
92 const TCHAR *getParams() { return m_params; }
93 time_t getExecutionTime() { return m_executionTime; }
94 UINT32 getOwner() { return m_owner; }
95 UINT32 getObjectId() { return m_objectId; }
96
97 void setLastExecutionTime(time_t time) { m_lastExecution = time; };
98 void setExecutionTime(time_t time) { m_executionTime = time; }
99 void setFlag(UINT32 flag) { m_flags |= flag; }
100 void removeFlag(UINT32 flag) { m_flags &= ~flag; }
101
102 void update(const TCHAR *taskHandlerId, const TCHAR *schedule, const TCHAR *params, UINT32 owner, UINT32 objectId, UINT32 flags);
103 void update(const TCHAR *taskHandlerId, time_t nextExecution, const TCHAR *params, UINT32 owner, UINT32 objectId, UINT32 flags);
104
105 void saveToDatabase(bool newObject);
106 void run(SchedulerCallback *callback);
107 void fillMessage(NXCPMessage *msg);
108 void fillMessage(NXCPMessage *msg, UINT32 base);
109
110 bool checkFlag(UINT32 flag) { return (m_flags & flag) != 0; }
111 bool isRunning() { return (m_flags & SCHEDULED_TASK_RUNNING) != 0; }
112 bool canAccess(UINT32 userId, UINT64 systemAccess);
113 };
114
115 /**
116 * Scheduler public API
117 */
118 void InitializeTaskScheduler();
119 void CloseTaskScheduler();
120 void RegisterSchedulerTaskHandler(const TCHAR *id, scheduled_action_executor exec, UINT64 accessRight);
121 UINT32 AddScheduledTask(const TCHAR *task, const TCHAR *schedule, const TCHAR *params, UINT32 owner, UINT32 objectId, UINT64 systemRights, UINT32 flags = 0);
122 UINT32 AddOneTimeScheduledTask(const TCHAR *task, time_t nextExecutionTime, const TCHAR *params, UINT32 owner, UINT32 objectId, UINT64 systemRights, UINT32 flags = 0);
123 UINT32 UpdateScheduledTask(int id, const TCHAR *task, const TCHAR *schedule, const TCHAR *params, UINT32 owner, UINT32 objectId, UINT64 systemAccessRights, UINT32 flags);
124 UINT32 UpdateOneTimeScheduledTask(int id, const TCHAR *task, time_t nextExecutionTime, const TCHAR *params, UINT32 owner, UINT32 objectId, UINT64 systemAccessRights, UINT32 flags);
125 UINT32 RemoveScheduledTask(UINT32 id, UINT32 user, UINT64 systemRights);
126 void GetSchedulerTaskHandlers(NXCPMessage *msg, UINT64 accessRights);
127 void GetSheduledTasks(NXCPMessage *msg, UINT32 userId, UINT64 systemRights);
128 UINT32 UpdateScheduledTaskFromMsg(NXCPMessage *request, UINT32 owner, UINT64 systemAccessRights);
129 UINT32 CreateScehduledTaskFromMsg(NXCPMessage *request, UINT32 owner, UINT64 systemAccessRights);
130
131 #endif