svn:ignore changes
[public/netxms.git] / src / server / include / nxcore_jobs.h
CommitLineData
ab621f39
VK
1/*
2** NetXMS - Network Management System
3** Copyright (C) 2003-2009 Victor Kirhenshtein
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
19** File: nxcore_jobs.h
20**
21**/
22
23#ifndef _nxcore_jobs_h_
24#define _nxcore_jobs_h_
25
26
27//
28// Job status
29//
30
31enum ServerJobStatus
32{
33 JOB_PENDING = 0,
34 JOB_ACTIVE,
35 JOB_ON_HOLD,
36 JOB_COMPLETED,
f40831eb 37 JOB_FAILED,
f2665675
VK
38 JOB_CANCELLED,
39 JOB_CANCEL_PENDING
ab621f39
VK
40};
41
42
43//
44// Job class
45//
46
47class ServerJobQueue;
3929b1ca 48class NetObj;
ab621f39
VK
49
50class NXCORE_EXPORTABLE ServerJob
51{
52private:
53 DWORD m_id;
0edd0ab0 54 DWORD m_userId;
ab621f39
VK
55 TCHAR *m_type;
56 DWORD m_remoteNode;
57 TCHAR *m_description;
58 ServerJobStatus m_status;
59 int m_progress;
60 TCHAR *m_failureMessage;
61 THREAD m_workerThread;
62 ServerJobQueue *m_owningQueue;
3929b1ca
VK
63 time_t m_lastStatusChange;
64 int m_autoCancelDelay; // Interval in seconds to cancel failed job automatically (0 = disabled)
65 time_t m_lastNotification;
66 NetObj *m_resolvedObject;
67 MUTEX m_notificationLock;
68 CSCPMessage m_notificationMessage;
8134d3a3 69 bool m_blockNextJobsOnFailure;
ab621f39
VK
70
71 static DWORD s_freeId;
72 static THREAD_RESULT THREAD_CALL WorkerThreadStarter(void *);
3929b1ca 73 static void sendNotification(ClientSession *session, void *arg);
ab621f39
VK
74
75protected:
76 virtual bool run();
77 virtual bool onCancel();
78
3929b1ca
VK
79 void notifyClients(bool isStatusChange);
80 void changeStatus(ServerJobStatus newStatus);
ab621f39
VK
81 void markProgress(int pctCompleted);
82 void setFailureMessage(const TCHAR *msg);
83
3929b1ca
VK
84 void setDescription(const TCHAR *description);
85
ab621f39 86public:
0edd0ab0 87 ServerJob(const TCHAR *type, const TCHAR *description, DWORD node, DWORD userId);
ab621f39
VK
88 virtual ~ServerJob();
89
90 void start();
91 bool cancel();
92
3929b1ca
VK
93 void setAutoCancelDelay(int delay) { m_autoCancelDelay = delay; }
94 int getAutoCancelDelay() { return m_autoCancelDelay; }
95
8134d3a3
VK
96 void setBlockNextJobsOnFailure(bool flag) { m_blockNextJobsOnFailure = flag; }
97 bool isBlockNextJobsOnFailure() { return m_blockNextJobsOnFailure; }
98
ab621f39 99 DWORD getId() { return m_id; }
0edd0ab0 100 DWORD getUserId() { return m_userId; }
ab621f39
VK
101 const TCHAR *getType() { return m_type; }
102 const TCHAR *getDescription() { return m_description; }
103 ServerJobStatus getStatus() { return m_status; }
104 int getProgress() { return m_progress; }
105 DWORD getRemoteNode() { return m_remoteNode; }
106 const TCHAR *getFailureMessage() { return CHECK_NULL(m_failureMessage); }
3929b1ca 107 time_t getLastStatusChange() { return m_lastStatusChange; }
ab621f39
VK
108
109 void setOwningQueue(ServerJobQueue *queue);
3929b1ca
VK
110
111 void fillMessage(CSCPMessage *msg);
ab621f39
VK
112};
113
114
115//
116// Job queue class
117//
118
119class ServerJobQueue
120{
121private:
122 int m_jobCount;
123 ServerJob **m_jobList;
124 MUTEX m_accessMutex;
125
126public:
127 ServerJobQueue();
128 ~ServerJobQueue();
129
130 void add(ServerJob *job);
f40831eb 131 bool cancel(DWORD jobId);
ab621f39 132 void runNext();
8134d3a3 133 void cleanup();
ab621f39 134
f40831eb
VK
135 ServerJob *findJob(DWORD jobId);
136
ab621f39
VK
137 void jobCompleted(ServerJob *job);
138
139 DWORD fillMessage(CSCPMessage *msg, DWORD *varIdBase);
140};
141
142
143//
144// Job manager API
145//
146
147bool NXCORE_EXPORTABLE AddJob(ServerJob *job);
148void GetJobList(CSCPMessage *msg);
f40831eb 149DWORD CancelJob(DWORD userId, CSCPMessage *msg);
ab621f39 150
45d84f8a
VK
151
152//
153// Agent policy deployment job
154//
155
156class AgentPolicy;
157
158class PolicyDeploymentJob : public ServerJob
159{
160protected:
161 Node *m_node;
162 AgentPolicy *m_policy;
163
164 virtual bool run();
165
166public:
0edd0ab0 167 PolicyDeploymentJob(Node *node, AgentPolicy *policy, DWORD userId);
45d84f8a
VK
168 virtual ~PolicyDeploymentJob();
169};
170
93599cfd
VK
171
172//
173// Agent policy uninstall job
174//
175
176class PolicyUninstallJob : public ServerJob
177{
178protected:
179 Node *m_node;
180 AgentPolicy *m_policy;
181
182 virtual bool run();
183
184public:
0edd0ab0 185 PolicyUninstallJob(Node *node, AgentPolicy *policy, DWORD userId);
93599cfd
VK
186 virtual ~PolicyUninstallJob();
187};
188
189
ab621f39 190#endif /* _nxcore_jobs_h_ */