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