"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.8.166/src/server/include/nxcore_jobs.h" (23 Feb 2021, 6103 Bytes) of package /linux/misc/netxms-3.8.166.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "nxcore_jobs.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.8.120_vs_3.8.166.

    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: nxcore_jobs.h
   20 **
   21 **/
   22 
   23 #ifndef _nxcore_jobs_h_
   24 #define _nxcore_jobs_h_
   25 
   26 #include "nxcore_schedule.h"
   27 
   28 #define JOB_RESCHEDULE_OFFSET 600
   29 
   30 #define MAX_JOB_NAME_LEN      128
   31 
   32 /**
   33  * Job status
   34  */
   35 enum ServerJobStatus
   36 {
   37     JOB_PENDING = 0,
   38     JOB_ACTIVE,
   39     JOB_ON_HOLD,
   40     JOB_COMPLETED,
   41     JOB_FAILED,
   42     JOB_CANCELLED,
   43     JOB_CANCEL_PENDING
   44 };
   45 
   46 
   47 /**
   48  * Job status
   49  */
   50 enum ServerJobResult
   51 {
   52     JOB_RESULT_SUCCESS = 0,
   53     JOB_RESULT_FAILED,
   54     JOB_RESULT_RESCHEDULE
   55 };
   56 
   57 class ServerJobQueue;
   58 class NetObj;
   59 
   60 #ifdef _WIN32
   61 template class NXCORE_EXPORTABLE shared_ptr<NetObj>;
   62 #endif
   63 
   64 /**
   65  * Job class
   66  */
   67 class NXCORE_EXPORTABLE ServerJob
   68 {
   69 private:
   70    uint32_t m_id;
   71    uint32_t m_userId;
   72     TCHAR m_type[MAX_JOB_NAME_LEN];
   73     TCHAR m_description[MAX_DB_STRING];
   74     ServerJobStatus m_status;
   75     int m_progress;
   76     TCHAR *m_failureMessage;
   77     THREAD m_workerThread;
   78     ServerJobQueue *m_owningQueue;
   79     time_t m_lastStatusChange;
   80     int m_autoCancelDelay;  // Interval in seconds to cancel failed job automatically (0 = disabled)
   81     time_t m_lastNotification;
   82     MUTEX m_notificationLock;
   83     NXCPMessage m_notificationMessage;
   84     bool m_blockNextJobsOnFailure;
   85     bool m_valid;
   86 
   87     static THREAD_RESULT THREAD_CALL WorkerThreadStarter(void *);
   88     static void sendNotification(ClientSession *session, void *arg);
   89 
   90 protected:
   91    uint32_t m_objectId;
   92    shared_ptr<NetObj> m_object;
   93     int m_retryCount;
   94 
   95     virtual ServerJobResult run();
   96     virtual bool onCancel();
   97     virtual const TCHAR *getAdditionalInfo();
   98     virtual const String serializeParameters();
   99 
  100     void notifyClients(bool isStatusChange);
  101     void changeStatus(ServerJobStatus newStatus);
  102     void markProgress(int pctCompleted);
  103     void setFailureMessage(const TCHAR *msg);
  104     void setDescription(const TCHAR *description);
  105    void invalidate() { m_valid = false; }
  106 
  107     int getRetryDelay();
  108 
  109 public:
  110     ServerJob(const TCHAR *type, const TCHAR *description, const shared_ptr<NetObj>& object, UINT32 userId, bool createOnHold, int retryCount = -1);
  111     virtual ~ServerJob();
  112 
  113     void start();
  114     bool cancel();
  115     bool hold();
  116     bool unhold();
  117 
  118     void setAutoCancelDelay(int delay) { m_autoCancelDelay = delay; }
  119     int getAutoCancelDelay() const { return m_autoCancelDelay; }
  120 
  121     void setBlockNextJobsOnFailure(bool flag) { m_blockNextJobsOnFailure = flag; }
  122     bool isBlockNextJobsOnFailure() const { return m_blockNextJobsOnFailure; }
  123     bool isValid() const { return m_valid; }
  124 
  125     uint32_t getId() const { return m_id; }
  126     uint32_t getUserId() const { return m_userId; }
  127     const TCHAR *getType() const { return m_type; }
  128     const TCHAR *getDescription() const { return m_description; }
  129     ServerJobStatus getStatus() const { return m_status; }
  130     int getProgress() const { return m_progress; }
  131     uint32_t getObjectId() const { return m_objectId; }
  132    const shared_ptr<NetObj>& getObject() const { return m_object; }
  133     const TCHAR *getFailureMessage() const { return CHECK_NULL_EX(m_failureMessage); }
  134     time_t getLastStatusChange() const { return m_lastStatusChange; }
  135 
  136     void setOwningQueue(ServerJobQueue *queue);
  137 
  138     void fillMessage(NXCPMessage *msg);
  139 
  140     virtual void rescheduleExecution();
  141 };
  142 
  143 /**
  144  * Job queue class
  145  */
  146 class NXCORE_EXPORTABLE ServerJobQueue
  147 {
  148 private:
  149     int m_jobCount;
  150     ServerJob **m_jobList;
  151     MUTEX m_accessMutex;
  152     uint32_t m_objectId;
  153 
  154 public:
  155     ServerJobQueue(uint32_t objectId);
  156     ~ServerJobQueue();
  157 
  158     void add(ServerJob *job);
  159     bool cancel(UINT32 jobId);
  160     bool hold(UINT32 jobId);
  161     bool unhold(UINT32 jobId);
  162     void runNext();
  163     void cleanup();
  164 
  165     ServerJob *findJob(UINT32 jobId);
  166     int getJobCount(const TCHAR *type = nullptr) const;
  167     uint32_t getObjectId() const { return m_objectId; }
  168 
  169     void jobCompleted(ServerJob *job);
  170 
  171     UINT32 fillMessage(NXCPMessage *msg, UINT32 *varIdBase) const;
  172 };
  173 
  174 /**
  175  * Job manager API
  176  */
  177 bool NXCORE_EXPORTABLE AddJob(ServerJob *job);
  178 void GetJobList(NXCPMessage *msg);
  179 int NXCORE_EXPORTABLE GetJobCount(uint32_t objectId = 0, const TCHAR *type = nullptr);
  180 UINT32 NXCORE_EXPORTABLE CancelJob(UINT32 userId, NXCPMessage *msg);
  181 UINT32 NXCORE_EXPORTABLE HoldJob(UINT32 userId, NXCPMessage *msg);
  182 UINT32 NXCORE_EXPORTABLE UnholdJob(UINT32 userId, NXCPMessage *msg);
  183 
  184 /**
  185  * File upload job
  186  */
  187 class FileUploadJob : public ServerJob
  188 {
  189 private:
  190     static int m_activeJobs;
  191     static int m_maxActiveJobs;
  192     static MUTEX m_sharedDataMutex;
  193 
  194     TCHAR *m_localFile;
  195     TCHAR *m_localFileFullPath;
  196     TCHAR *m_remoteFile;
  197     TCHAR *m_info;
  198     int64_t m_fileSize;
  199 
  200    void setLocalFileFullPath();
  201 
  202    static void uploadCallback(INT64 size, void *arg);
  203 
  204 protected:
  205     virtual ServerJobResult run() override;
  206     virtual const TCHAR *getAdditionalInfo() override;
  207     virtual const String serializeParameters() override;
  208 
  209 public:
  210     static void init();
  211 
  212     FileUploadJob(const shared_ptr<Node>& node, const TCHAR *localFile, const TCHAR *remoteFile, uint32_t userId, bool createOnHold);
  213     FileUploadJob(const TCHAR *params, const shared_ptr<Node>& node, uint32_t userId);
  214     virtual ~FileUploadJob();
  215 
  216     virtual void rescheduleExecution() override;
  217 };
  218 
  219 /**
  220  * DCI recalculation job
  221  */
  222 class DCIRecalculationJob : public ServerJob
  223 {
  224 private:
  225    DCItem *m_dci;
  226    bool m_cancelled;
  227 
  228 protected:
  229    virtual ServerJobResult run();
  230    virtual bool onCancel();
  231 
  232 public:
  233    DCIRecalculationJob(const shared_ptr<DataCollectionTarget>& object, const DCItem *dci, uint32_t userId);
  234    virtual ~DCIRecalculationJob();
  235 };
  236 
  237 #endif   /* _nxcore_jobs_h_ */