preparation of 1.1.4 release
[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 70
ab621f39 71 static THREAD_RESULT THREAD_CALL WorkerThreadStarter(void *);
3929b1ca 72 static void sendNotification(ClientSession *session, void *arg);
ab621f39 73
878b4261
VK
74 void createHistoryRecord();
75 void updateHistoryRecord(bool onStart);
76
ab621f39
VK
77protected:
78 virtual bool run();
79 virtual bool onCancel();
24dc5346 80 virtual const TCHAR *getAdditionalInfo();
ab621f39 81
3929b1ca
VK
82 void notifyClients(bool isStatusChange);
83 void changeStatus(ServerJobStatus newStatus);
ab621f39
VK
84 void markProgress(int pctCompleted);
85 void setFailureMessage(const TCHAR *msg);
86
3929b1ca
VK
87 void setDescription(const TCHAR *description);
88
ab621f39 89public:
509bb045 90 ServerJob(const TCHAR *type, const TCHAR *description, DWORD node, DWORD userId, bool createOnHold);
ab621f39
VK
91 virtual ~ServerJob();
92
93 void start();
94 bool cancel();
509bb045
VK
95 bool hold();
96 bool unhold();
ab621f39 97
3929b1ca
VK
98 void setAutoCancelDelay(int delay) { m_autoCancelDelay = delay; }
99 int getAutoCancelDelay() { return m_autoCancelDelay; }
100
8134d3a3
VK
101 void setBlockNextJobsOnFailure(bool flag) { m_blockNextJobsOnFailure = flag; }
102 bool isBlockNextJobsOnFailure() { return m_blockNextJobsOnFailure; }
103
ab621f39 104 DWORD getId() { return m_id; }
0edd0ab0 105 DWORD getUserId() { return m_userId; }
ab621f39
VK
106 const TCHAR *getType() { return m_type; }
107 const TCHAR *getDescription() { return m_description; }
108 ServerJobStatus getStatus() { return m_status; }
109 int getProgress() { return m_progress; }
110 DWORD getRemoteNode() { return m_remoteNode; }
509bb045 111 const TCHAR *getFailureMessage() { return CHECK_NULL_EX(m_failureMessage); }
3929b1ca 112 time_t getLastStatusChange() { return m_lastStatusChange; }
ab621f39
VK
113
114 void setOwningQueue(ServerJobQueue *queue);
3929b1ca
VK
115
116 void fillMessage(CSCPMessage *msg);
ab621f39
VK
117};
118
119
120//
121// Job queue class
122//
123
124class ServerJobQueue
125{
126private:
127 int m_jobCount;
128 ServerJob **m_jobList;
129 MUTEX m_accessMutex;
130
131public:
132 ServerJobQueue();
133 ~ServerJobQueue();
134
135 void add(ServerJob *job);
f40831eb 136 bool cancel(DWORD jobId);
509bb045
VK
137 bool hold(DWORD jobId);
138 bool unhold(DWORD jobId);
ab621f39 139 void runNext();
8134d3a3 140 void cleanup();
ab621f39 141
f40831eb
VK
142 ServerJob *findJob(DWORD jobId);
143
ab621f39
VK
144 void jobCompleted(ServerJob *job);
145
146 DWORD fillMessage(CSCPMessage *msg, DWORD *varIdBase);
147};
148
149
150//
151// Job manager API
152//
153
154bool NXCORE_EXPORTABLE AddJob(ServerJob *job);
155void GetJobList(CSCPMessage *msg);
509bb045
VK
156DWORD NXCORE_EXPORTABLE CancelJob(DWORD userId, CSCPMessage *msg);
157DWORD NXCORE_EXPORTABLE HoldJob(DWORD userId, CSCPMessage *msg);
158DWORD NXCORE_EXPORTABLE UnholdJob(DWORD userId, CSCPMessage *msg);
ab621f39 159
45d84f8a 160
619e5c9b
VK
161//
162// File upload job
163//
164
165class FileUploadJob : public ServerJob
166{
167protected:
168 Node *m_node;
169 TCHAR *m_localFile;
170 TCHAR *m_remoteFile;
24dc5346 171 TCHAR *m_info;
619e5c9b
VK
172 INT64 m_fileSize;
173
174 virtual bool run();
24dc5346 175 virtual const TCHAR *getAdditionalInfo();
619e5c9b
VK
176 static void uploadCallback(INT64 size, void *arg);
177
178public:
509bb045 179 FileUploadJob(Node *node, const TCHAR *localFile, const TCHAR *remoteFile, DWORD userId, bool createOnHold);
619e5c9b
VK
180 virtual ~FileUploadJob();
181};
182
183
45d84f8a
VK
184//
185// Agent policy deployment job
186//
187
188class AgentPolicy;
189
190class PolicyDeploymentJob : public ServerJob
191{
192protected:
193 Node *m_node;
194 AgentPolicy *m_policy;
195
196 virtual bool run();
197
198public:
0edd0ab0 199 PolicyDeploymentJob(Node *node, AgentPolicy *policy, DWORD userId);
45d84f8a
VK
200 virtual ~PolicyDeploymentJob();
201};
202
93599cfd
VK
203
204//
205// Agent policy uninstall job
206//
207
208class PolicyUninstallJob : public ServerJob
209{
210protected:
211 Node *m_node;
212 AgentPolicy *m_policy;
213
214 virtual bool run();
215
216public:
0edd0ab0 217 PolicyUninstallJob(Node *node, AgentPolicy *policy, DWORD userId);
93599cfd
VK
218 virtual ~PolicyUninstallJob();
219};
220
221
ab621f39 222#endif /* _nxcore_jobs_h_ */