added debug option to log long running SQL queries
[public/netxms.git] / src / server / core / upload_job.cpp
CommitLineData
4cd1e46b
AK
1/*
2** NetXMS - Network Management System
3** Copyright (C) 2003-2011 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: upload_job.cpp
20**
21**/
22
23#include "nxcore.h"
24
25
26//
27// Static members
28//
29
30int FileUploadJob::m_activeJobs = 0;
31int FileUploadJob::m_maxActiveJobs = 10;
32MUTEX FileUploadJob::m_sharedDataMutex = INVALID_MUTEX_HANDLE;
33
34
35//
36// Static initializer
37//
38
39void FileUploadJob::init()
40{
41 m_sharedDataMutex = MutexCreate();
42 m_maxActiveJobs = ConfigReadInt(_T("MaxActiveUploadJobs"), 10);
43}
44
45
46//
47// Constructor
48//
49
50FileUploadJob::FileUploadJob(Node *node, const TCHAR *localFile, const TCHAR *remoteFile, UINT32 userId, bool createOnHold)
51 : ServerJob(_T("UPLOAD_FILE"), _T("Upload file to managed node"), node->Id(), userId, createOnHold)
52{
53 m_node = node;
54 node->incRefCount();
55
56 TCHAR buffer[1024];
57 _sntprintf(buffer, 1024, _T("Upload file %s"), GetCleanFileName(localFile));
58 setDescription(buffer);
59
60 m_localFile = _tcsdup(localFile);
61 m_remoteFile = (remoteFile != NULL) ? _tcsdup(remoteFile) : NULL;
62
63 _sntprintf(buffer, 1024, _T("Local file: %s; Remote file: %s"), m_localFile, CHECK_NULL(m_remoteFile));
64 m_info = _tcsdup(buffer);
65}
66
67
68//
69// Destructor
70//
71
72FileUploadJob::~FileUploadJob()
73{
74 m_node->decRefCount();
75 safe_free(m_localFile);
76 safe_free(m_remoteFile);
77 safe_free(m_info);
78}
79
80
81//
82// Run job
83//
84
85bool FileUploadJob::run()
86{
87 bool success = false;
88
89 while(true)
90 {
91 MutexLock(m_sharedDataMutex);
92 if (m_activeJobs < m_maxActiveJobs)
93 {
94 m_activeJobs++;
95 MutexUnlock(m_sharedDataMutex);
96 break;
97 }
98 MutexUnlock(m_sharedDataMutex);
99 ThreadSleep(5);
100 }
101
102 AgentConnectionEx *conn = m_node->createAgentConnection();
103 if (conn != NULL)
104 {
105 m_fileSize = (INT64)FileSize(m_localFile);
106 UINT32 rcc = conn->uploadFile(m_localFile, m_remoteFile, uploadCallback, this);
107 if (rcc == ERR_SUCCESS)
108 {
109 success = true;
110 }
111 else
112 {
113 setFailureMessage(AgentErrorCodeToText(rcc));
114 }
115 }
116 else
117 {
118 setFailureMessage(_T("Agent connection not available"));
119 }
120
121 MutexLock(m_sharedDataMutex);
122 m_activeJobs--;
123 MutexUnlock(m_sharedDataMutex);
124
125 return success;
126}
127
128
129//
130// Upload progress callback
131//
132
133void FileUploadJob::uploadCallback(INT64 size, void *arg)
134{
135 if (((FileUploadJob *)arg)->m_fileSize > 0)
136 ((FileUploadJob *)arg)->markProgress((int)(size * _LL(100) / ((FileUploadJob *)arg)->m_fileSize));
137 else
138 ((FileUploadJob *)arg)->markProgress(100);
139}
140
141
142//
143// Get additional info for logging
144//
145
146const TCHAR *FileUploadJob::getAdditionalInfo()
147{
148 return m_info;
149}