Added JobRetryCount server configuration parameter
[public/netxms.git] / src / server / core / upload_job.cpp
CommitLineData
74d2f1ea 1/*
4cd1e46b 2** NetXMS - Network Management System
8581943e 3** Copyright (C) 2003-2014 Victor Kirhenshtein
4cd1e46b
AK
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: upload_job.cpp
20**
21**/
22
23#include "nxcore.h"
24
74d2f1ea 25/**
26 * Static members
27 */
4cd1e46b
AK
28int FileUploadJob::m_activeJobs = 0;
29int FileUploadJob::m_maxActiveJobs = 10;
30MUTEX FileUploadJob::m_sharedDataMutex = INVALID_MUTEX_HANDLE;
31
ec13a467
VK
32/**
33 * Scheduled file upload
34 */
35void ScheduledFileUpload(const ScheduledTaskParameters *params)
4fe83ebd
Z
36{
37 //get parameters - node id or name, server file name, agent file name
4fe83ebd
Z
38 TCHAR serverFile[MAX_PATH];
39 TCHAR agentFile[MAX_PATH];
0e0867c6 40 AgentGetParameterArg(params->m_params, 1, serverFile, MAX_PATH, false);
41 AgentGetParameterArg(params->m_params, 2, agentFile, MAX_PATH, false);
b3beb7f4 42
9ff40954 43 if(params->m_objectId == 0 || serverFile[0] == 0 || agentFile[0] == 0)
4fe83ebd 44 {
9ff40954 45 DbgPrintf(4, _T("UploadFile: Input parameters are invalid. nodeId=%d, serverFile='%s', agentFile='%s'"),
b3beb7f4 46 params->m_userId, serverFile, agentFile);
4fe83ebd
Z
47 return;
48 }
49
b3beb7f4 50 Node *object = (Node *)FindObjectById(params->m_objectId, OBJECT_NODE);
4fe83ebd
Z
51 if (object != NULL)
52 {
53 if (object->checkAccessRights(params->m_userId, OBJECT_ACCESS_CONTROL))
54 {
55 TCHAR fullPath[MAX_PATH];
56
57 // Create full path to the file store
58 _tcscpy(fullPath, g_netxmsdDataDir);
59 _tcscat(fullPath, DDIR_FILES);
60 _tcscat(fullPath, FS_PATH_SEPARATOR);
61 int len = (int)_tcslen(fullPath);
62 nx_strncpy(&fullPath[len], GetCleanFileName(serverFile), MAX_PATH - len);
63
64 ServerJob *job = new FileUploadJob((Node *)object, fullPath, agentFile, params->m_userId, false);
65 if (AddJob(job))
66 {
67 DbgPrintf(4, _T("ScheduledUploadFile: File(%s) uploaded to %s node, to %s "), serverFile, object->getName(), agentFile);
68 //auditlog?
69 }
70 else
71 {
72 delete job;
73 }
74 }
75 else
76 DbgPrintf(4, _T("ScheduledUploadFile: Access to node %s denied"), object->getName());
77 }
78 else
b3beb7f4 79 DbgPrintf(4, _T("ScheduledUploadFile: Node with id=\'%d\' not found"), params->m_userId);
4fe83ebd
Z
80}
81
74d2f1ea 82/**
83 * Static initializer
84 */
4cd1e46b
AK
85void FileUploadJob::init()
86{
87 m_sharedDataMutex = MutexCreate();
88 m_maxActiveJobs = ConfigReadInt(_T("MaxActiveUploadJobs"), 10);
ec13a467 89 RegisterSchedulerTaskHandler(_T("Upload.File"), ScheduledFileUpload, SYSTEM_ACCESS_SCHEDULE_FILE_UPLOAD);
4cd1e46b
AK
90}
91
74d2f1ea 92/**
93 * Constructor
94 */
4cd1e46b 95FileUploadJob::FileUploadJob(Node *node, const TCHAR *localFile, const TCHAR *remoteFile, UINT32 userId, bool createOnHold)
c42b4551 96 : ServerJob(_T("UPLOAD_FILE"), _T("Upload file to managed node"), node->getId(), userId, createOnHold)
4cd1e46b
AK
97{
98 m_node = node;
99 node->incRefCount();
100
101 TCHAR buffer[1024];
102 _sntprintf(buffer, 1024, _T("Upload file %s"), GetCleanFileName(localFile));
103 setDescription(buffer);
104
105 m_localFile = _tcsdup(localFile);
106 m_remoteFile = (remoteFile != NULL) ? _tcsdup(remoteFile) : NULL;
107
108 _sntprintf(buffer, 1024, _T("Local file: %s; Remote file: %s"), m_localFile, CHECK_NULL(m_remoteFile));
109 m_info = _tcsdup(buffer);
48126778
VK
110
111 m_fileSize = 0;
4cd1e46b
AK
112}
113
74d2f1ea 114/**
115 * Destructor
116 */
4cd1e46b
AK
117FileUploadJob::~FileUploadJob()
118{
119 m_node->decRefCount();
120 safe_free(m_localFile);
121 safe_free(m_remoteFile);
122 safe_free(m_info);
123}
124
74d2f1ea 125/**
126 * Run job
127 */
4cd1e46b
AK
128bool FileUploadJob::run()
129{
130 bool success = false;
74d2f1ea 131
4cd1e46b
AK
132 while(true)
133 {
134 MutexLock(m_sharedDataMutex);
135 if (m_activeJobs < m_maxActiveJobs)
136 {
137 m_activeJobs++;
138 MutexUnlock(m_sharedDataMutex);
139 break;
140 }
141 MutexUnlock(m_sharedDataMutex);
142 ThreadSleep(5);
143 }
144
145 AgentConnectionEx *conn = m_node->createAgentConnection();
146 if (conn != NULL)
147 {
148 m_fileSize = (INT64)FileSize(m_localFile);
149 UINT32 rcc = conn->uploadFile(m_localFile, m_remoteFile, uploadCallback, this);
150 if (rcc == ERR_SUCCESS)
151 {
152 success = true;
153 }
154 else
155 {
156 setFailureMessage(AgentErrorCodeToText(rcc));
157 }
1693f955 158 conn->decRefCount();
4cd1e46b
AK
159 }
160 else
161 {
162 setFailureMessage(_T("Agent connection not available"));
163 }
74d2f1ea 164
4cd1e46b
AK
165 MutexLock(m_sharedDataMutex);
166 m_activeJobs--;
167 MutexUnlock(m_sharedDataMutex);
168
169 return success;
170}
171
74d2f1ea 172/**
173 * Upload progress callback
174 */
4cd1e46b
AK
175void FileUploadJob::uploadCallback(INT64 size, void *arg)
176{
177 if (((FileUploadJob *)arg)->m_fileSize > 0)
178 ((FileUploadJob *)arg)->markProgress((int)(size * _LL(100) / ((FileUploadJob *)arg)->m_fileSize));
179 else
180 ((FileUploadJob *)arg)->markProgress(100);
181}
182
74d2f1ea 183/**
184 * Get additional info for logging
185 */
4cd1e46b
AK
186const TCHAR *FileUploadJob::getAdditionalInfo()
187{
188 return m_info;
189}