fixed build error; fixed compilation warnings
[public/netxms.git] / src / server / include / nxcore_jobs.h
CommitLineData
9fa031cd 1/*
ab621f39
VK
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
0ff10010
VK
26/**
27 * Job status
28 */
ab621f39
VK
29enum ServerJobStatus
30{
31 JOB_PENDING = 0,
32 JOB_ACTIVE,
33 JOB_ON_HOLD,
34 JOB_COMPLETED,
f40831eb 35 JOB_FAILED,
f2665675
VK
36 JOB_CANCELLED,
37 JOB_CANCEL_PENDING
ab621f39
VK
38};
39
0ff10010
VK
40/**
41 * Job class
42 */
ab621f39 43class ServerJobQueue;
3929b1ca 44class NetObj;
ab621f39
VK
45
46class NXCORE_EXPORTABLE ServerJob
47{
48private:
967893bb
VK
49 UINT32 m_id;
50 UINT32 m_userId;
ab621f39 51 TCHAR *m_type;
967893bb 52 UINT32 m_remoteNode;
ab621f39
VK
53 TCHAR *m_description;
54 ServerJobStatus m_status;
55 int m_progress;
56 TCHAR *m_failureMessage;
57 THREAD m_workerThread;
58 ServerJobQueue *m_owningQueue;
3929b1ca
VK
59 time_t m_lastStatusChange;
60 int m_autoCancelDelay; // Interval in seconds to cancel failed job automatically (0 = disabled)
61 time_t m_lastNotification;
62 NetObj *m_resolvedObject;
63 MUTEX m_notificationLock;
64 CSCPMessage m_notificationMessage;
8134d3a3 65 bool m_blockNextJobsOnFailure;
ab621f39 66
ab621f39 67 static THREAD_RESULT THREAD_CALL WorkerThreadStarter(void *);
3929b1ca 68 static void sendNotification(ClientSession *session, void *arg);
ab621f39 69
878b4261
VK
70 void createHistoryRecord();
71 void updateHistoryRecord(bool onStart);
72
ab621f39
VK
73protected:
74 virtual bool run();
75 virtual bool onCancel();
24dc5346 76 virtual const TCHAR *getAdditionalInfo();
ab621f39 77
3929b1ca
VK
78 void notifyClients(bool isStatusChange);
79 void changeStatus(ServerJobStatus newStatus);
ab621f39
VK
80 void markProgress(int pctCompleted);
81 void setFailureMessage(const TCHAR *msg);
82
3929b1ca
VK
83 void setDescription(const TCHAR *description);
84
ab621f39 85public:
967893bb 86 ServerJob(const TCHAR *type, const TCHAR *description, UINT32 node, UINT32 userId, bool createOnHold);
ab621f39
VK
87 virtual ~ServerJob();
88
89 void start();
90 bool cancel();
509bb045
VK
91 bool hold();
92 bool unhold();
ab621f39 93
3929b1ca
VK
94 void setAutoCancelDelay(int delay) { m_autoCancelDelay = delay; }
95 int getAutoCancelDelay() { return m_autoCancelDelay; }
96
8134d3a3
VK
97 void setBlockNextJobsOnFailure(bool flag) { m_blockNextJobsOnFailure = flag; }
98 bool isBlockNextJobsOnFailure() { return m_blockNextJobsOnFailure; }
99
967893bb
VK
100 UINT32 getId() { return m_id; }
101 UINT32 getUserId() { return m_userId; }
ab621f39
VK
102 const TCHAR *getType() { return m_type; }
103 const TCHAR *getDescription() { return m_description; }
104 ServerJobStatus getStatus() { return m_status; }
105 int getProgress() { return m_progress; }
967893bb 106 UINT32 getRemoteNode() { return m_remoteNode; }
509bb045 107 const TCHAR *getFailureMessage() { return CHECK_NULL_EX(m_failureMessage); }
3929b1ca 108 time_t getLastStatusChange() { return m_lastStatusChange; }
ab621f39
VK
109
110 void setOwningQueue(ServerJobQueue *queue);
3929b1ca
VK
111
112 void fillMessage(CSCPMessage *msg);
ab621f39
VK
113};
114
0ff10010
VK
115/**
116 * Job queue class
117 */
ab621f39
VK
118class ServerJobQueue
119{
120private:
121 int m_jobCount;
122 ServerJob **m_jobList;
123 MUTEX m_accessMutex;
124
125public:
126 ServerJobQueue();
127 ~ServerJobQueue();
128
129 void add(ServerJob *job);
967893bb
VK
130 bool cancel(UINT32 jobId);
131 bool hold(UINT32 jobId);
132 bool unhold(UINT32 jobId);
ab621f39 133 void runNext();
8134d3a3 134 void cleanup();
ab621f39 135
967893bb 136 ServerJob *findJob(UINT32 jobId);
0ff10010 137 int getJobCount(const TCHAR *type = NULL);
f40831eb 138
ab621f39
VK
139 void jobCompleted(ServerJob *job);
140
967893bb 141 UINT32 fillMessage(CSCPMessage *msg, UINT32 *varIdBase);
ab621f39
VK
142};
143
0ff10010
VK
144/**
145 * Job manager API
146 */
ab621f39
VK
147bool NXCORE_EXPORTABLE AddJob(ServerJob *job);
148void GetJobList(CSCPMessage *msg);
967893bb
VK
149UINT32 NXCORE_EXPORTABLE CancelJob(UINT32 userId, CSCPMessage *msg);
150UINT32 NXCORE_EXPORTABLE HoldJob(UINT32 userId, CSCPMessage *msg);
151UINT32 NXCORE_EXPORTABLE UnholdJob(UINT32 userId, CSCPMessage *msg);
ab621f39 152
0ff10010
VK
153/**
154 * File upload job
155 */
619e5c9b
VK
156class FileUploadJob : public ServerJob
157{
158protected:
d77baddd
VK
159 static int m_activeJobs;
160 static int m_maxActiveJobs;
161 static MUTEX m_sharedDataMutex;
162
619e5c9b
VK
163 Node *m_node;
164 TCHAR *m_localFile;
165 TCHAR *m_remoteFile;
24dc5346 166 TCHAR *m_info;
619e5c9b
VK
167 INT64 m_fileSize;
168
169 virtual bool run();
24dc5346 170 virtual const TCHAR *getAdditionalInfo();
619e5c9b
VK
171 static void uploadCallback(INT64 size, void *arg);
172
173public:
d77baddd
VK
174 static void init();
175
967893bb 176 FileUploadJob(Node *node, const TCHAR *localFile, const TCHAR *remoteFile, UINT32 userId, bool createOnHold);
619e5c9b
VK
177 virtual ~FileUploadJob();
178};
179
0ff10010
VK
180/**
181 * File download job
182 */
6f16f12d
VK
183class FileDownloadJob : public ServerJob
184{
185private:
186 Node *m_node;
187 ClientSession *m_session;
967893bb 188 UINT32 m_requestId;
6f16f12d
VK
189 TCHAR *m_localFile;
190 TCHAR *m_remoteFile;
191 TCHAR *m_info;
192 INT64 m_fileSize;
76b4edb5 193 INT64 m_currentSize;
6f16f12d 194 SOCKET m_socket;
9fa031cd 195 UINT32 m_maxFileSize;
196 bool m_follow;
6f16f12d
VK
197
198protected:
199 virtual bool run();
200 virtual bool onCancel();
201 virtual const TCHAR *getAdditionalInfo();
202
203 static void progressCallback(size_t size, void *arg);
76b4edb5 204 static void fileResendCallback(CSCP_MESSAGE *msg, void *arg);
6f16f12d
VK
205
206public:
9fa031cd 207 FileDownloadJob(Node *node, const TCHAR *remoteName, UINT32 maxFileSize, bool follow, ClientSession *session, UINT32 requestId);
6f16f12d
VK
208 virtual ~FileDownloadJob();
209
967893bb 210 static TCHAR *buildServerFileName(UINT32 nodeId, const TCHAR *remoteFile, TCHAR *buffer, size_t bufferSize);
ece85e30 211 TCHAR *getLocalFileName();
6f16f12d
VK
212};
213
0ff10010
VK
214/**
215 * Agent policy deployment job
216 */
45d84f8a
VK
217class AgentPolicy;
218
219class PolicyDeploymentJob : public ServerJob
220{
221protected:
222 Node *m_node;
223 AgentPolicy *m_policy;
224
225 virtual bool run();
226
227public:
967893bb 228 PolicyDeploymentJob(Node *node, AgentPolicy *policy, UINT32 userId);
45d84f8a
VK
229 virtual ~PolicyDeploymentJob();
230};
231
93599cfd 232
a2871ffc 233/**
234 * Agent policy uninstall job
235 */
93599cfd
VK
236class PolicyUninstallJob : public ServerJob
237{
238protected:
239 Node *m_node;
240 AgentPolicy *m_policy;
241
242 virtual bool run();
243
244public:
967893bb 245 PolicyUninstallJob(Node *node, AgentPolicy *policy, UINT32 userId);
93599cfd
VK
246 virtual ~PolicyUninstallJob();
247};
248
249
ab621f39 250#endif /* _nxcore_jobs_h_ */