implemented communication/deployment job mechanism
[public/netxms.git] / src / server / core / job.cpp
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: job.cpp
20**
21**/
22
23#include "nxcore.h"
24
25
26//
27// Static members
28//
29
30DWORD ServerJob::s_freeId = 1;
31
32
33//
34// Constructor
35//
36
37ServerJob::ServerJob(const TCHAR *type, const TCHAR *description, DWORD node)
38{
39 m_id = s_freeId++;
40 m_type = _tcsdup(CHECK_NULL(type));
41 m_description = _tcsdup(CHECK_NULL(description));
42 m_status = JOB_PENDING;
43 m_remoteNode = node;
44 m_progress = 0;
45 m_failureMessage = NULL;
46 m_owningQueue = NULL;
47 m_workerThread = INVALID_THREAD_HANDLE;
48}
49
50
51//
52// Destructor
53//
54
55ServerJob::~ServerJob()
56{
57 ThreadJoin(m_workerThread);
58
59 safe_free(m_type);
60 safe_free(m_description);
61}
62
63
64//
65// Set owning queue
66//
67
68void ServerJob::setOwningQueue(ServerJobQueue *queue)
69{
70 m_owningQueue = queue;
71}
72
73
74//
75// Update progress
76//
77
78void ServerJob::markProgress(int pctCompleted)
79{
80 if ((pctCompleted > m_progress) && (pctCompleted <= 100))
81 m_progress = pctCompleted;
82}
83
84
85//
86// Worker thread starter
87//
88
89THREAD_RESULT THREAD_CALL ServerJob::WorkerThreadStarter(void *arg)
90{
91 DbgPrintf(4, _T("Job %d started"), ((ServerJob *)arg)->m_id);
92
93 if (((ServerJob *)arg)->run())
94 ((ServerJob *)arg)->m_status = JOB_COMPLETED;
95 else
96 ((ServerJob *)arg)->m_status = JOB_FAILED;
97 ((ServerJob *)arg)->m_workerThread = INVALID_THREAD_HANDLE;
98
99 DbgPrintf(4, _T("Job %d finished, status=%s"), ((ServerJob *)arg)->m_id, (((ServerJob *)arg)->m_status = JOB_COMPLETED) ? _T("COMPLETED") : _T("FAILED"));
100
101 if (((ServerJob *)arg)->m_owningQueue != NULL)
102 ((ServerJob *)arg)->m_owningQueue->jobCompleted((ServerJob *)arg);
103 return THREAD_OK;
104}
105
106
107//
108// Start job
109//
110
111void ServerJob::start()
112{
113 m_status = JOB_ACTIVE;
114 m_workerThread = ThreadCreateEx(WorkerThreadStarter, 0, this);
115}
116
117
118//
119// Cancel job
120//
121
122bool ServerJob::cancel()
123{
124 if (m_status != JOB_ACTIVE)
125 return false;
126 return onCancel();
127}
128
129
130//
131// Default run (empty)
132//
133
134bool ServerJob::run()
135{
136 return true;
137}
138
139
140//
141// Default cancel handler
142//
143
144bool ServerJob::onCancel()
145{
146 return false;
147}
148
149
150//
151// Set failure message
152//
153
154void ServerJob::setFailureMessage(const TCHAR *msg)
155{
156 safe_free(m_failureMessage);
157 m_failureMessage = (msg != NULL) ? _tcsdup(msg) : NULL;
158}