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