annotate OrthancFramework/Sources/JobsEngine/JobsRegistry.cpp @ 5853:4d932683049d get-scu tip

very first implementation of C-Get SCU
author Alain Mazy <am@orthanc.team>
date Tue, 29 Oct 2024 17:25:49 +0100
parents f7adfb22e20e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
5640
f7adfb22e20e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5485
diff changeset
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
f7adfb22e20e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5485
diff changeset
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
5485
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5364
diff changeset
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 *
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * This program is free software: you can redistribute it and/or
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
10 * modify it under the terms of the GNU Lesser General Public License
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
11 * as published by the Free Software Foundation, either version 3 of
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
12 * the License, or (at your option) any later version.
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
13 *
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * This program is distributed in the hope that it will be useful, but
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
17 * Lesser General Public License for more details.
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 *
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
19 * You should have received a copy of the GNU Lesser General Public
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
20 * License along with this program. If not, see
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
21 * <http://www.gnu.org/licenses/>.
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 **/
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include "../PrecompiledHeaders.h"
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include "JobsRegistry.h"
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #include "../Logging.h"
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #include "../OrthancException.h"
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 #include "../Toolbox.h"
2656
a6d3e45eeff5 SerializationToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2655
diff changeset
31 #include "../SerializationToolbox.h"
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 namespace Orthanc
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
35 static const char* STATE = "State";
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
36 static const char* TYPE = "Type";
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
37 static const char* PRIORITY = "Priority";
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
38 static const char* JOB = "Job";
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
39 static const char* JOBS = "Jobs";
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
40 static const char* JOBS_REGISTRY = "JobsRegistry";
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
41 static const char* CREATION_TIME = "CreationTime";
2669
eaf10085ffa1 no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2668
diff changeset
42 static const char* LAST_CHANGE_TIME = "LastChangeTime";
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
43 static const char* RUNTIME = "Runtime";
4733
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
44 static const char* ERROR_CODE = "ErrorCode";
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
45 static const char* ERROR_DETAILS = "ErrorDetails";
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
46
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
47
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 class JobsRegistry::JobHandler : public boost::noncopyable
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
49 {
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 private:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 std::string id_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 JobState state_;
2648
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
53 std::string jobType_;
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
54 std::unique_ptr<IJob> job_;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 int priority_; // "+inf()" means highest priority
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 boost::posix_time::ptime creationTime_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 boost::posix_time::ptime lastStateChangeTime_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 boost::posix_time::time_duration runtime_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 boost::posix_time::ptime retryTime_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 bool pauseScheduled_;
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
61 bool cancelScheduled_;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 JobStatus lastStatus_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 void Touch()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 if (state_ == JobState_Running)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 runtime_ += (now - lastStateChangeTime_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 lastStateChangeTime_ = now;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
76 void SetStateInternal(JobState state)
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 state_ = state;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 pauseScheduled_ = false;
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
80 cancelScheduled_ = false;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 Touch();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 public:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 JobHandler(IJob* job,
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 int priority) :
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 id_(Toolbox::GenerateUuid()),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 state_(JobState_Pending),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 job_(job),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 priority_(priority),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 creationTime_(boost::posix_time::microsec_clock::universal_time()),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 lastStateChangeTime_(creationTime_),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 runtime_(boost::posix_time::milliseconds(0)),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 retryTime_(creationTime_),
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
95 pauseScheduled_(false),
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
96 cancelScheduled_(false)
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 if (job == NULL)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 throw OrthancException(ErrorCode_NullPointer);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
2648
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
103 job->GetJobType(jobType_);
2668
d26dd081df97 saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2667
diff changeset
104 job->Start();
2630
00327e989458 creating archives/medias is now a job
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2620
diff changeset
105
3240
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
106 lastStatus_ = JobStatus(ErrorCode_Success, "", *job_);
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 const std::string& GetId() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 return id_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 IJob& GetJob() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 assert(job_.get() != NULL);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 return *job_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 void SetPriority(int priority)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 priority_ = priority;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 int GetPriority() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 return priority_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 JobState GetState() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 return state_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
135 void SetState(JobState state)
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 if (state == JobState_Retry)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 // Use "SetRetryState()"
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 SetStateInternal(state);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 void SetRetryState(unsigned int timeout)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 if (state_ == JobState_Running)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 SetStateInternal(JobState_Retry);
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
153 retryTime_ = (boost::posix_time::microsec_clock::universal_time() +
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 boost::posix_time::milliseconds(timeout));
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 // Only valid for running jobs
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 void SchedulePause()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 if (state_ == JobState_Running)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 pauseScheduled_ = true;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 // Only valid for running jobs
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
176 void ScheduleCancel()
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
177 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
178 if (state_ == JobState_Running)
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
179 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
180 cancelScheduled_ = true;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
181 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
182 else
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
183 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
184 // Only valid for running jobs
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
185 throw OrthancException(ErrorCode_BadSequenceOfCalls);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
186 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
187 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
188
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 bool IsPauseScheduled()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 return pauseScheduled_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
194 bool IsCancelScheduled()
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
195 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
196 return cancelScheduled_;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
197 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
198
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 bool IsRetryReady(const boost::posix_time::ptime& now) const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 if (state_ != JobState_Retry)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 return retryTime_ <= now;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 const boost::posix_time::ptime& GetCreationTime() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 return creationTime_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 const boost::posix_time::ptime& GetLastStateChangeTime() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 return lastStateChangeTime_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220
2669
eaf10085ffa1 no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2668
diff changeset
221 void SetLastStateChangeTime(const boost::posix_time::ptime& time)
eaf10085ffa1 no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2668
diff changeset
222 {
eaf10085ffa1 no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2668
diff changeset
223 lastStateChangeTime_ = time;
eaf10085ffa1 no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2668
diff changeset
224 }
eaf10085ffa1 no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2668
diff changeset
225
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 const boost::posix_time::time_duration& GetRuntime() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 return runtime_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230
4768
a046e91cc76f cont fix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4733
diff changeset
231 void ResetRuntime()
a046e91cc76f cont fix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4733
diff changeset
232 {
a046e91cc76f cont fix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4733
diff changeset
233 runtime_ = boost::posix_time::milliseconds(0);
a046e91cc76f cont fix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4733
diff changeset
234 }
a046e91cc76f cont fix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4733
diff changeset
235
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 const JobStatus& GetLastStatus() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 return lastStatus_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 void SetLastStatus(const JobStatus& status)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 lastStatus_ = status;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 Touch();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 }
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
246
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
247 void SetLastErrorCode(ErrorCode code)
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
248 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
249 lastStatus_.SetErrorCode(code);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
250 }
2648
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
251
2663
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
252 bool Serialize(Json::Value& target) const
2648
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
253 {
2655
c196d76cb8fa serialization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2652
diff changeset
254 target = Json::objectValue;
2663
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
255
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
256 bool ok;
2648
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
257
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
258 if (state_ == JobState_Running)
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
259 {
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
260 // WARNING: Cannot directly access the "job_" member, as long
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
261 // as a "RunningJob" instance is running. We do not use a
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
262 // mutex at the "JobHandler" level, as serialization would be
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
263 // blocked while a step in the job is running. Instead, we
2976
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
264 // save a snapshot of the serialized job. (*)
2663
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
265
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
266 if (lastStatus_.HasSerialized())
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
267 {
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
268 target[JOB] = lastStatus_.GetSerialized();
2663
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
269 ok = true;
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
270 }
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
271 else
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
272 {
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
273 ok = false;
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
274 }
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
275 }
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
276 else
2663
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
277 {
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
278 ok = job_->Serialize(target[JOB]);
2663
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
279 }
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
280
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
281 if (ok)
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
282 {
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
283 target[STATE] = EnumerationToString(state_);
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
284 target[PRIORITY] = priority_;
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
285 target[CREATION_TIME] = boost::posix_time::to_iso_string(creationTime_);
2669
eaf10085ffa1 no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2668
diff changeset
286 target[LAST_CHANGE_TIME] = boost::posix_time::to_iso_string(lastStateChangeTime_);
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
287 target[RUNTIME] = static_cast<unsigned int>(runtime_.total_milliseconds());
4733
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
288
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
289 // New in Orthanc 1.9.5
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
290 target[ERROR_CODE] = static_cast<int>(lastStatus_.GetErrorCode());
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
291 target[ERROR_DETAILS] = lastStatus_.GetDetails();
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
292
2663
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
293 return true;
2648
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
294 }
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
295 else
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
296 {
4268
0ae2ca210077 new macro TLOG() to replace VLOG() for trace logs with a category
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4203
diff changeset
297 LOG(TRACE) << "Job backup is not supported for job of type: " << jobType_;
2663
228e2783ce83 some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2656
diff changeset
298 return false;
2648
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
299 }
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
300 }
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
301
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
302 JobHandler(IJobUnserializer& unserializer,
2670
c5646f766b3e serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2669
diff changeset
303 const Json::Value& serialized,
c5646f766b3e serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2669
diff changeset
304 const std::string& id) :
c5646f766b3e serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2669
diff changeset
305 id_(id),
2648
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
306 pauseScheduled_(false),
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
307 cancelScheduled_(false)
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
308 {
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
309 state_ = StringToJobState(SerializationToolbox::ReadString(serialized, STATE));
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
310 priority_ = SerializationToolbox::ReadInteger(serialized, PRIORITY);
2648
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
311 creationTime_ = boost::posix_time::from_iso_string
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
312 (SerializationToolbox::ReadString(serialized, CREATION_TIME));
2669
eaf10085ffa1 no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2668
diff changeset
313 lastStateChangeTime_ = boost::posix_time::from_iso_string
eaf10085ffa1 no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2668
diff changeset
314 (SerializationToolbox::ReadString(serialized, LAST_CHANGE_TIME));
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
315 runtime_ = boost::posix_time::milliseconds
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
316 (SerializationToolbox::ReadInteger(serialized, RUNTIME));
2648
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
317
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
318 retryTime_ = creationTime_;
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
319
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
320 job_.reset(unserializer.UnserializeJob(serialized[JOB]));
2652
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2648
diff changeset
321 job_->GetJobType(jobType_);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2648
diff changeset
322 job_->Start();
2648
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
323
4733
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
324 ErrorCode errorCode;
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
325 if (serialized.isMember(ERROR_CODE))
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
326 {
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
327 errorCode = static_cast<ErrorCode>(SerializationToolbox::ReadInteger(serialized, ERROR_CODE));
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
328 }
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
329 else
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
330 {
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
331 errorCode = ErrorCode_Success; // Backward compatibility with Orthanc <= 1.9.4
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
332 }
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
333
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
334 std::string details;
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
335 if (serialized.isMember(ERROR_DETAILS)) // Backward compatibility with Orthanc <= 1.9.4
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
336 {
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
337 details = SerializationToolbox::ReadString(serialized, ERROR_DETAILS);
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
338 }
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
339
1db3b79d97bd Error code and description of jobs are now saved into the Orthanc database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
340 lastStatus_ = JobStatus(errorCode, details, *job_);
2648
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
341 }
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
342 };
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344
4203
4d42408da117 improving const-correctness in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4121
diff changeset
345 bool JobsRegistry::PriorityComparator::operator() (JobHandler* const& a,
4d42408da117 improving const-correctness in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4121
diff changeset
346 JobHandler* const& b) const
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
347 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348 return a->GetPriority() < b->GetPriority();
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
349 }
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 #if defined(NDEBUG)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353 void JobsRegistry::CheckInvariants() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 }
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
356
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 #else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358 bool JobsRegistry::IsPendingJob(const JobHandler& job) const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 PendingJobs copy = pendingJobs_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
361 while (!copy.empty())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
362 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363 if (copy.top() == &job)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365 return true;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
366 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
367
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
368 copy.pop();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
369 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
370
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371 return false;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
373
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374 bool JobsRegistry::IsCompletedJob(JobHandler& job) const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376 for (CompletedJobs::const_iterator it = completedJobs_.begin();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377 it != completedJobs_.end(); ++it)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379 if (*it == &job)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381 return true;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
383 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385 return false;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 bool JobsRegistry::IsRetryJob(JobHandler& job) const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390 return retryJobs_.find(&job) != retryJobs_.end();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 void JobsRegistry::CheckInvariants() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
395 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396 PendingJobs copy = pendingJobs_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
397 while (!copy.empty())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
398 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
399 assert(copy.top()->GetState() == JobState_Pending);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
400 copy.pop();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
402 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
404 assert(completedJobs_.size() <= maxCompletedJobs_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
405
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
406 for (CompletedJobs::const_iterator it = completedJobs_.begin();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407 it != completedJobs_.end(); ++it)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 assert((*it)->GetState() == JobState_Success ||
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410 (*it)->GetState() == JobState_Failure);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
411 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
412
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
413 for (RetryJobs::const_iterator it = retryJobs_.begin();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
414 it != retryJobs_.end(); ++it)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
415 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
416 assert((*it)->GetState() == JobState_Retry);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
417 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
418
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
419 for (JobsIndex::const_iterator it = jobsIndex_.begin();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
420 it != jobsIndex_.end(); ++it)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
421 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
422 JobHandler& job = *it->second;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
423
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
424 assert(job.GetId() == it->first);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
425
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
426 switch (job.GetState())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
427 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
428 case JobState_Pending:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
429 assert(!IsRetryJob(job) && IsPendingJob(job) && !IsCompletedJob(job));
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
430 break;
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
431
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
432 case JobState_Success:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
433 case JobState_Failure:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
434 assert(!IsRetryJob(job) && !IsPendingJob(job) && IsCompletedJob(job));
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
435 break;
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
436
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
437 case JobState_Retry:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
438 assert(IsRetryJob(job) && !IsPendingJob(job) && !IsCompletedJob(job));
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
439 break;
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
440
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
441 case JobState_Running:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
442 case JobState_Paused:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
443 assert(!IsRetryJob(job) && !IsPendingJob(job) && !IsCompletedJob(job));
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
444 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
445
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
446 default:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
447 throw OrthancException(ErrorCode_InternalError);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
448 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
449 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
450 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
451 #endif
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
452
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
453
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
454 void JobsRegistry::ForgetOldCompletedJobs()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
455 {
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
456 while (completedJobs_.size() > maxCompletedJobs_)
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
457 {
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
458 assert(completedJobs_.front() != NULL);
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
459
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
460 std::string id = completedJobs_.front()->GetId();
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
461 assert(jobsIndex_.find(id) != jobsIndex_.end());
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
462
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
463 jobsIndex_.erase(id);
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
464 delete(completedJobs_.front());
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
465 completedJobs_.pop_front();
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
466 }
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
467
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
468 CheckInvariants();
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
469 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
470
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
471
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
472 void JobsRegistry::SetCompletedJob(JobHandler& job,
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
473 bool success)
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
474 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
475 job.SetState(success ? JobState_Success : JobState_Failure);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
476
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
477 completedJobs_.push_back(&job);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
478 someJobComplete_.notify_all();
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
479 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
480
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
481
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
482 void JobsRegistry::MarkRunningAsCompleted(JobHandler& job,
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
483 CompletedReason reason)
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
484 {
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
485 const char* tmp;
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
486
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
487 switch (reason)
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
488 {
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
489 case CompletedReason_Success:
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
490 tmp = "success";
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
491 break;
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
492
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
493 case CompletedReason_Failure:
3371
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3240
diff changeset
494 tmp = "failure";
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
495 break;
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
496
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
497 case CompletedReason_Canceled:
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
498 tmp = "cancel";
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
499 break;
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
500
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
501 default:
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
502 throw OrthancException(ErrorCode_InternalError);
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
503 }
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
504
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
505 LOG(INFO) << "Job has completed with " << tmp << ": " << job.GetId();
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
506
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
507 CheckInvariants();
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
508
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
509 assert(job.GetState() == JobState_Running);
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
510 SetCompletedJob(job, reason == CompletedReason_Success);
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
511
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
512 if (reason == CompletedReason_Canceled)
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
513 {
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
514 job.SetLastErrorCode(ErrorCode_CanceledJob);
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
515 }
2570
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
516
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
517 if (observer_ != NULL)
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
518 {
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
519 if (reason == CompletedReason_Success)
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
520 {
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
521 observer_->SignalJobSuccess(job.GetId());
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
522 }
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
523 else
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
524 {
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
525 observer_->SignalJobFailure(job.GetId());
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
526 }
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
527 }
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
528
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
529 // WARNING: The following call might make "job" invalid if the job
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
530 // history size is empty
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
531 ForgetOldCompletedJobs();
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
532 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
533
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
534
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
535 void JobsRegistry::MarkRunningAsRetry(JobHandler& job,
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
536 unsigned int timeout)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
537 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
538 LOG(INFO) << "Job scheduled for retry in " << timeout << "ms: " << job.GetId();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
539
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
540 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
541
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
542 assert(job.GetState() == JobState_Running &&
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
543 retryJobs_.find(&job) == retryJobs_.end());
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
544
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
545 retryJobs_.insert(&job);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
546 job.SetRetryState(timeout);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
547
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
548 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
549 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
550
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
551
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
552 void JobsRegistry::MarkRunningAsPaused(JobHandler& job)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
553 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
554 LOG(INFO) << "Job paused: " << job.GetId();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
555
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
556 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
557 assert(job.GetState() == JobState_Running);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
558
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
559 job.SetState(JobState_Paused);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
560
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
561 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
562 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
563
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
564
2570
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
565 bool JobsRegistry::GetStateInternal(JobState& state,
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
566 const std::string& id)
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
567 {
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
568 CheckInvariants();
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
569
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
570 JobsIndex::const_iterator it = jobsIndex_.find(id);
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
571 if (it == jobsIndex_.end())
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
572 {
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
573 return false;
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
574 }
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
575 else
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
576 {
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
577 state = it->second->GetState();
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
578 return true;
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
579 }
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
580 }
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
581
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
582
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
583 JobsRegistry::~JobsRegistry()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
584 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
585 for (JobsIndex::iterator it = jobsIndex_.begin(); it != jobsIndex_.end(); ++it)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
586 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
587 assert(it->second != NULL);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
588 delete it->second;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
589 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
590 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
591
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
592
2620
1232922c8793 speeding up shutdown if Lua script is in trailing phase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2600
diff changeset
593 void JobsRegistry::SetMaxCompletedJobs(size_t n)
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
594 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
595 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
596 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
597
2620
1232922c8793 speeding up shutdown if Lua script is in trailing phase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2600
diff changeset
598 LOG(INFO) << "The size of the history of the jobs engine is set to: " << n << " job(s)";
1232922c8793 speeding up shutdown if Lua script is in trailing phase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2600
diff changeset
599
1232922c8793 speeding up shutdown if Lua script is in trailing phase
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2600
diff changeset
600 maxCompletedJobs_ = n;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
601 ForgetOldCompletedJobs();
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
602 }
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
603
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
604
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
605 size_t JobsRegistry::GetMaxCompletedJobs()
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
606 {
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
607 boost::mutex::scoped_lock lock(mutex_);
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
608 CheckInvariants();
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
609 return maxCompletedJobs_;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
610 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
611
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
612
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
613 void JobsRegistry::ListJobs(std::set<std::string>& target)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
614 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
615 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
616 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
617
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
618 for (JobsIndex::const_iterator it = jobsIndex_.begin();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
619 it != jobsIndex_.end(); ++it)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
620 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
621 target.insert(it->first);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
622 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
623 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
624
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
625
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
626 bool JobsRegistry::GetJobInfo(JobInfo& target,
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
627 const std::string& id)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
628 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
629 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
630 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
631
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
632 JobsIndex::const_iterator found = jobsIndex_.find(id);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
633
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
634 if (found == jobsIndex_.end())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
635 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
636 return false;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
637 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
638 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
639 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
640 const JobHandler& handler = *found->second;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
641 target = JobInfo(handler.GetId(),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
642 handler.GetPriority(),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
643 handler.GetState(),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
644 handler.GetLastStatus(),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
645 handler.GetCreationTime(),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
646 handler.GetLastStateChangeTime(),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
647 handler.GetRuntime());
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
648 return true;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
649 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
650 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
651
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
652
5364
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
653 bool JobsRegistry::DeleteJobInfo(const std::string& id)
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
654 {
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
655 LOG(INFO) << "Deleting job: " << id;
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
656
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
657 boost::mutex::scoped_lock lock(mutex_);
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
658 CheckInvariants();
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
659
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
660 JobsIndex::iterator found = jobsIndex_.find(id);
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
661
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
662 if (found == jobsIndex_.end())
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
663 {
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
664 LOG(WARNING) << "Unknown job to delete: " << id;
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
665 return false;
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
666 }
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
667 else
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
668 {
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
669 for (CompletedJobs::iterator it = completedJobs_.begin();
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
670 it != completedJobs_.end(); ++it)
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
671 {
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
672 if (*it == found->second)
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
673 {
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
674 found->second->GetJob().DeleteAllOutputs();
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
675 delete found->second;
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
676
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
677 completedJobs_.erase(it);
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
678 jobsIndex_.erase(id);
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
679 return true;
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
680 }
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
681 }
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
682
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
683 LOG(WARNING) << "Can not delete a job that is not complete: " << id;
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
684 return false;
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
685 }
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
686 }
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
687
b5f2122a1334 Added a route to delete completed jobs from history: DELETE /jobs/{id}
Alain Mazy <am@osimis.io>
parents: 5310
diff changeset
688
2976
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
689 bool JobsRegistry::GetJobOutput(std::string& output,
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
690 MimeType& mime,
4805
0a38000b086d Archive jobs response now contains a header Content-Disposition:filename='archive.zip'
Alain Mazy <am@osimis.io>
parents: 4768
diff changeset
691 std::string& filename,
2976
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
692 const std::string& job,
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
693 const std::string& key)
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
694 {
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
695 boost::mutex::scoped_lock lock(mutex_);
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
696 CheckInvariants();
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
697
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
698 JobsIndex::const_iterator found = jobsIndex_.find(job);
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
699
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
700 if (found == jobsIndex_.end())
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
701 {
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
702 return false;
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
703 }
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
704 else
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
705 {
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
706 const JobHandler& handler = *found->second;
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
707
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
708 if (handler.GetState() == JobState_Success)
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
709 {
4805
0a38000b086d Archive jobs response now contains a header Content-Disposition:filename='archive.zip'
Alain Mazy <am@osimis.io>
parents: 4768
diff changeset
710 return handler.GetJob().GetOutput(output, mime, filename, key);
2976
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
711 }
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
712 else
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
713 {
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
714 return false;
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
715 }
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
716 }
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
717 }
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
718
5310
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
719 bool JobsRegistry::DeleteJobOutput(const std::string& job,
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
720 const std::string& key)
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
721 {
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
722 boost::mutex::scoped_lock lock(mutex_);
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
723 CheckInvariants();
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
724
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
725 JobsIndex::const_iterator found = jobsIndex_.find(job);
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
726
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
727 if (found == jobsIndex_.end())
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
728 {
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
729 return false;
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
730 }
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
731 else
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
732 {
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
733 const JobHandler& handler = *found->second;
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
734
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
735 if (handler.GetState() == JobState_Success)
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
736 {
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
737 return handler.GetJob().DeleteOutput(key);
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
738 }
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
739 else
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
740 {
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
741 return false;
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
742 }
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
743 }
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
744 }
b5c502bcaf99 added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
745
2976
cb5d75143da0 Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
746
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
747 void JobsRegistry::SubmitInternal(std::string& id,
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
748 JobHandler* handler)
2648
e1893d31652a serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2630
diff changeset
749 {
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
750 if (handler == NULL)
2668
d26dd081df97 saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2667
diff changeset
751 {
d26dd081df97 saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2667
diff changeset
752 throw OrthancException(ErrorCode_NullPointer);
d26dd081df97 saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2667
diff changeset
753 }
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
754
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
755 std::unique_ptr<JobHandler> protection(handler);
2669
eaf10085ffa1 no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2668
diff changeset
756
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
757 {
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
758 boost::mutex::scoped_lock lock(mutex_);
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
759 CheckInvariants();
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
760
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
761 id = handler->GetId();
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
762 int priority = handler->GetPriority();
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
763
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
764 jobsIndex_.insert(std::make_pair(id, protection.release()));
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
765
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
766 switch (handler->GetState())
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
767 {
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
768 case JobState_Pending:
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
769 case JobState_Retry:
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
770 case JobState_Running:
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
771 handler->SetState(JobState_Pending);
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
772 pendingJobs_.push(handler);
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
773 pendingJobAvailable_.notify_one();
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
774 break;
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
775
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
776 case JobState_Success:
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
777 SetCompletedJob(*handler, true);
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
778 break;
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
779
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
780 case JobState_Failure:
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
781 SetCompletedJob(*handler, false);
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
782 break;
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
783
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
784 case JobState_Paused:
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
785 break;
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
786
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
787 default:
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2950
diff changeset
788 {
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2950
diff changeset
789 std::string details = ("A job should not be loaded from state: " +
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2950
diff changeset
790 std::string(EnumerationToString(handler->GetState())));
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2950
diff changeset
791 throw OrthancException(ErrorCode_InternalError, details);
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2950
diff changeset
792 }
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
793 }
2668
d26dd081df97 saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2667
diff changeset
794
5853
4d932683049d very first implementation of C-Get SCU
Alain Mazy <am@orthanc.team>
parents: 5640
diff changeset
795 std::string jobType;
4d932683049d very first implementation of C-Get SCU
Alain Mazy <am@orthanc.team>
parents: 5640
diff changeset
796 handler->GetJob().GetJobType(jobType);
4d932683049d very first implementation of C-Get SCU
Alain Mazy <am@orthanc.team>
parents: 5640
diff changeset
797
4d932683049d very first implementation of C-Get SCU
Alain Mazy <am@orthanc.team>
parents: 5640
diff changeset
798 LOG(INFO) << "New " << jobType << " job submitted with priority " << priority << ": " << id;
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
799
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
800 if (observer_ != NULL)
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
801 {
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
802 observer_->SignalJobSubmitted(id);
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
803 }
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
804
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
805 // WARNING: The following call might make "handler" invalid if
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
806 // the job history size is empty
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
807 ForgetOldCompletedJobs();
2668
d26dd081df97 saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2667
diff changeset
808 }
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
809 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
810
4300
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4268
diff changeset
811 JobsRegistry::JobsRegistry(size_t maxCompletedJobs) :
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4268
diff changeset
812 maxCompletedJobs_(maxCompletedJobs),
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4268
diff changeset
813 observer_(NULL)
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4268
diff changeset
814 {
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4268
diff changeset
815 }
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4268
diff changeset
816
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
817
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
818 void JobsRegistry::Submit(std::string& id,
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
819 IJob* job, // Takes ownership
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
820 int priority)
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
821 {
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
822 SubmitInternal(id, new JobHandler(job, priority));
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
823 }
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
824
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
825
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
826 void JobsRegistry::Submit(IJob* job, // Takes ownership
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
827 int priority)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
828 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
829 std::string id;
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
830 SubmitInternal(id, new JobHandler(job, priority));
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
831 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
832
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
833
3240
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
834 void JobsRegistry::SubmitAndWait(Json::Value& successContent,
2867
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
835 IJob* job, // Takes ownership
2570
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
836 int priority)
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
837 {
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
838 std::string id;
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
839 Submit(id, job, priority);
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
840
2867
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
841 JobState state = JobState_Pending; // Dummy initialization
2570
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
842
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
843 {
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
844 boost::mutex::scoped_lock lock(mutex_);
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
845
2867
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
846 for (;;)
2570
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
847 {
2867
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
848 if (!GetStateInternal(state, id))
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
849 {
3162
94a4f75cc746 Fix issue #119 (/patients/.../archive returns a 500 when JobsHistorySize is 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
850 // Job has finished and has been lost (typically happens if
94a4f75cc746 Fix issue #119 (/patients/.../archive returns a 500 when JobsHistorySize is 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
851 // "JobsHistorySize" is 0)
94a4f75cc746 Fix issue #119 (/patients/.../archive returns a 500 when JobsHistorySize is 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
852 throw OrthancException(ErrorCode_InexistentItem,
94a4f75cc746 Fix issue #119 (/patients/.../archive returns a 500 when JobsHistorySize is 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
853 "Cannot retrieve the status of the job, "
94a4f75cc746 Fix issue #119 (/patients/.../archive returns a 500 when JobsHistorySize is 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
854 "make sure that \"JobsHistorySize\" is not 0");
2867
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
855 }
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
856 else if (state == JobState_Failure)
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
857 {
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
858 // Failure
3240
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
859 JobsIndex::const_iterator it = jobsIndex_.find(id);
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
860 if (it != jobsIndex_.end()) // Should always be true, already tested in GetStateInternal()
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
861 {
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
862 ErrorCode code = it->second->GetLastStatus().GetErrorCode();
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
863 const std::string& details = it->second->GetLastStatus().GetDetails();
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
864
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
865 if (details.empty())
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
866 {
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
867 throw OrthancException(code);
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
868 }
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
869 else
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
870 {
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
871 throw OrthancException(code, details);
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
872 }
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
873 }
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
874 else
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
875 {
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
876 throw OrthancException(ErrorCode_InternalError);
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
877 }
2867
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
878 }
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
879 else if (state == JobState_Success)
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
880 {
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
881 // Success, try and retrieve the status of the job
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
882 JobsIndex::const_iterator it = jobsIndex_.find(id);
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
883 if (it == jobsIndex_.end())
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
884 {
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
885 // Should not happen
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
886 state = JobState_Failure;
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
887 }
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
888 else
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
889 {
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
890 const JobStatus& status = it->second->GetLastStatus();
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
891 successContent = status.GetPublicContent();
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
892 }
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
893
3240
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
894 return;
2867
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
895 }
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
896 else
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
897 {
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
898 // This job has not finished yet, wait for new completion
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
899 someJobComplete_.wait(lock);
251614c2edac DicomMoveScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2828
diff changeset
900 }
2570
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
901 }
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
902 }
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
903 }
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
904
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
905
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
906 bool JobsRegistry::SetPriority(const std::string& id,
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
907 int priority)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
908 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
909 LOG(INFO) << "Changing priority to " << priority << " for job: " << id;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
910
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
911 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
912 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
913
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
914 JobsIndex::iterator found = jobsIndex_.find(id);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
915
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
916 if (found == jobsIndex_.end())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
917 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
918 LOG(WARNING) << "Unknown job: " << id;
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
919 return false;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
920 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
921 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
922 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
923 found->second->SetPriority(priority);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
924
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
925 if (found->second->GetState() == JobState_Pending)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
926 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
927 // If the job is pending, we need to reconstruct the
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
928 // priority queue, as the heap condition has changed
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
929
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
930 PendingJobs copy;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
931 std::swap(copy, pendingJobs_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
932
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
933 assert(pendingJobs_.empty());
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
934 while (!copy.empty())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
935 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
936 pendingJobs_.push(copy.top());
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
937 copy.pop();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
938 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
939 }
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
940
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
941 CheckInvariants();
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
942 return true;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
943 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
944 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
945
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
946
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
947 void JobsRegistry::RemovePendingJob(const std::string& id)
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
948 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
949 // If the job is pending, we need to reconstruct the priority
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
950 // queue to remove it
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
951 PendingJobs copy;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
952 std::swap(copy, pendingJobs_);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
953
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
954 assert(pendingJobs_.empty());
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
955 while (!copy.empty())
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
956 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
957 if (copy.top()->GetId() != id)
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
958 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
959 pendingJobs_.push(copy.top());
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
960 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
961
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
962 copy.pop();
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
963 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
964 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
965
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
966
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
967 void JobsRegistry::RemoveRetryJob(JobHandler* handler)
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
968 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
969 RetryJobs::iterator item = retryJobs_.find(handler);
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
970 assert(item != retryJobs_.end());
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
971 retryJobs_.erase(item);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
972 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
973
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
974
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
975 bool JobsRegistry::Pause(const std::string& id)
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
976 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
977 LOG(INFO) << "Pausing job: " << id;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
978
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
979 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
980 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
981
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
982 JobsIndex::iterator found = jobsIndex_.find(id);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
983
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
984 if (found == jobsIndex_.end())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
985 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
986 LOG(WARNING) << "Unknown job: " << id;
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
987 return false;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
988 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
989 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
990 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
991 switch (found->second->GetState())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
992 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
993 case JobState_Pending:
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
994 RemovePendingJob(id);
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
995 found->second->SetState(JobState_Paused);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
996 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
997
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
998 case JobState_Retry:
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
999 RemoveRetryJob(found->second);
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1000 found->second->SetState(JobState_Paused);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1001 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1002
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1003 case JobState_Paused:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1004 case JobState_Success:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1005 case JobState_Failure:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1006 // Nothing to be done
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1007 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1008
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1009 case JobState_Running:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1010 found->second->SchedulePause();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1011 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1012
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1013 default:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1014 throw OrthancException(ErrorCode_InternalError);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1015 }
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
1016
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
1017 CheckInvariants();
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
1018 return true;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1019 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1020 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1021
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1022
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1023 bool JobsRegistry::Cancel(const std::string& id)
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1024 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1025 LOG(INFO) << "Canceling job: " << id;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1026
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1027 boost::mutex::scoped_lock lock(mutex_);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1028 CheckInvariants();
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1029
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1030 JobsIndex::iterator found = jobsIndex_.find(id);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1031
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1032 if (found == jobsIndex_.end())
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1033 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1034 LOG(WARNING) << "Unknown job: " << id;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1035 return false;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1036 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1037 else
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1038 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1039 switch (found->second->GetState())
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1040 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1041 case JobState_Pending:
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1042 RemovePendingJob(id);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1043 SetCompletedJob(*found->second, false);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1044 found->second->SetLastErrorCode(ErrorCode_CanceledJob);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1045 break;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1046
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1047 case JobState_Retry:
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1048 RemoveRetryJob(found->second);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1049 SetCompletedJob(*found->second, false);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1050 found->second->SetLastErrorCode(ErrorCode_CanceledJob);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1051 break;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1052
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1053 case JobState_Paused:
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1054 SetCompletedJob(*found->second, false);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1055 found->second->SetLastErrorCode(ErrorCode_CanceledJob);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1056 break;
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1057
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1058 case JobState_Success:
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1059 case JobState_Failure:
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1060 // Nothing to be done
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1061 break;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1062
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1063 case JobState_Running:
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1064 found->second->ScheduleCancel();
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1065 break;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1066
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1067 default:
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1068 throw OrthancException(ErrorCode_InternalError);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1069 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1070
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1071 // WARNING: The following call might make "handler" invalid if
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1072 // the job history size is empty
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1073 ForgetOldCompletedJobs();
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1074
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1075 return true;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1076 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1077 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1078
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1079
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
1080 bool JobsRegistry::Resume(const std::string& id)
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1081 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1082 LOG(INFO) << "Resuming job: " << id;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1083
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1084 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1085 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1086
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1087 JobsIndex::iterator found = jobsIndex_.find(id);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1088
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1089 if (found == jobsIndex_.end())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1090 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1091 LOG(WARNING) << "Unknown job: " << id;
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
1092 return false;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1093 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1094 else if (found->second->GetState() != JobState_Paused)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1095 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1096 LOG(WARNING) << "Cannot resume a job that is not paused: " << id;
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
1097 return false;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1098 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1099 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1100 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1101 found->second->SetState(JobState_Pending);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1102 pendingJobs_.push(found->second);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1103 pendingJobAvailable_.notify_one();
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
1104 CheckInvariants();
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1105 return true;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1106 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1107 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1108
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1109
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
1110 bool JobsRegistry::Resubmit(const std::string& id)
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1111 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1112 LOG(INFO) << "Resubmitting failed job: " << id;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1113
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1114 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1115 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1116
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1117 JobsIndex::iterator found = jobsIndex_.find(id);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1118
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1119 if (found == jobsIndex_.end())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1120 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1121 LOG(WARNING) << "Unknown job: " << id;
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
1122 return false;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1123 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1124 else if (found->second->GetState() != JobState_Failure)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1125 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1126 LOG(WARNING) << "Cannot resubmit a job that has not failed: " << id;
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
1127 return false;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1128 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1129 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1130 {
2812
ea7aea6f6a95 improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2673
diff changeset
1131 found->second->GetJob().Reset();
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1132
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1133 bool ok = false;
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1134 for (CompletedJobs::iterator it = completedJobs_.begin();
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1135 it != completedJobs_.end(); ++it)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1136 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1137 if (*it == found->second)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1138 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1139 ok = true;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1140 completedJobs_.erase(it);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1141 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1142 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1143 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1144
4121
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
1145 (void) ok; // Remove warning about unused variable in release builds
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1146 assert(ok);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1147
4768
a046e91cc76f cont fix
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4733
diff changeset
1148 found->second->ResetRuntime();
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1149 found->second->SetState(JobState_Pending);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1150 pendingJobs_.push(found->second);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1151 pendingJobAvailable_.notify_one();
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
1152
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
1153 CheckInvariants();
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
1154 return true;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1155 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1156 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1157
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1158
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1159 void JobsRegistry::ScheduleRetries()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1160 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1161 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1162 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1163
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1164 RetryJobs copy;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1165 std::swap(copy, retryJobs_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1166
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1167 const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1168
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1169 assert(retryJobs_.empty());
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1170 for (RetryJobs::iterator it = copy.begin(); it != copy.end(); ++it)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1171 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1172 if ((*it)->IsRetryReady(now))
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1173 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1174 LOG(INFO) << "Retrying job: " << (*it)->GetId();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1175 (*it)->SetState(JobState_Pending);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1176 pendingJobs_.push(*it);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1177 pendingJobAvailable_.notify_one();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1178 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1179 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1180 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1181 retryJobs_.insert(*it);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1182 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1183 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1184
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1185 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1186 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1187
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1188
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1189 bool JobsRegistry::GetState(JobState& state,
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1190 const std::string& id)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1191 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1192 boost::mutex::scoped_lock lock(mutex_);
2570
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
1193 return GetStateInternal(state, id);
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1194 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1195
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
1196
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1197 void JobsRegistry::SetObserver(JobsRegistry::IObserver& observer)
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
1198 {
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1199 boost::mutex::scoped_lock lock(mutex_);
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1200 observer_ = &observer;
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
1201 }
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
1202
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1203
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1204 void JobsRegistry::ResetObserver()
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
1205 {
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1206 boost::mutex::scoped_lock lock(mutex_);
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1207 observer_ = NULL;
2673
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
1208 }
8e0bc055d18c JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2670
diff changeset
1209
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1210
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1211 JobsRegistry::RunningJob::RunningJob(JobsRegistry& registry,
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1212 unsigned int timeout) :
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1213 registry_(registry),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1214 handler_(NULL),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1215 targetState_(JobState_Failure),
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1216 targetRetryTimeout_(0),
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1217 canceled_(false)
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1218 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1219 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1220 boost::mutex::scoped_lock lock(registry_.mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1221
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1222 while (registry_.pendingJobs_.empty())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1223 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1224 if (timeout == 0)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1225 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1226 registry_.pendingJobAvailable_.wait(lock);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1227 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1228 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1229 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1230 bool success = registry_.pendingJobAvailable_.timed_wait
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1231 (lock, boost::posix_time::milliseconds(timeout));
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1232 if (!success)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1233 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1234 // No pending job
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1235 return;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1236 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1237 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1238 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1239
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1240 handler_ = registry_.pendingJobs_.top();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1241 registry_.pendingJobs_.pop();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1242
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1243 assert(handler_->GetState() == JobState_Pending);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1244 handler_->SetState(JobState_Running);
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1245 handler_->SetLastErrorCode(ErrorCode_Success);
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1246
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1247 job_ = &handler_->GetJob();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1248 id_ = handler_->GetId();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1249 priority_ = handler_->GetPriority();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1250 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1251 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1252
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1253
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1254 JobsRegistry::RunningJob::~RunningJob()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1255 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1256 if (IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1257 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1258 boost::mutex::scoped_lock lock(registry_.mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1259
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1260 switch (targetState_)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1261 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1262 case JobState_Failure:
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1263 registry_.MarkRunningAsCompleted
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1264 (*handler_, canceled_ ? CompletedReason_Canceled : CompletedReason_Failure);
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1265 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1266
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1267 case JobState_Success:
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1268 registry_.MarkRunningAsCompleted(*handler_, CompletedReason_Success);
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1269 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1270
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1271 case JobState_Paused:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1272 registry_.MarkRunningAsPaused(*handler_);
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1273 break;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1274
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1275 case JobState_Retry:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1276 registry_.MarkRunningAsRetry(*handler_, targetRetryTimeout_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1277 break;
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1278
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1279 default:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1280 assert(0);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1281 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1282 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1283 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1284
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1285
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1286 bool JobsRegistry::RunningJob::IsValid() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1287 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1288 return (handler_ != NULL &&
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1289 job_ != NULL);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1290 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1291
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1292
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1293 const std::string& JobsRegistry::RunningJob::GetId() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1294 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1295 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1296 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1297 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1298 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1299 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1300 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1301 return id_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1302 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1303 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1304
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1305
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1306 int JobsRegistry::RunningJob::GetPriority() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1307 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1308 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1309 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1310 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1311 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1312 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1313 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1314 return priority_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1315 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1316 }
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1317
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1318
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1319 IJob& JobsRegistry::RunningJob::GetJob()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1320 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1321 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1322 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1323 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1324 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1325 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1326 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1327 return *job_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1328 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1329 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1330
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1331
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1332 bool JobsRegistry::RunningJob::IsPauseScheduled()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1333 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1334 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1335 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1336 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1337 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1338 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1339 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1340 boost::mutex::scoped_lock lock(registry_.mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1341 registry_.CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1342 assert(handler_->GetState() == JobState_Running);
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1343
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1344 return handler_->IsPauseScheduled();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1345 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1346 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1347
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1348
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1349 bool JobsRegistry::RunningJob::IsCancelScheduled()
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1350 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1351 if (!IsValid())
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1352 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1353 throw OrthancException(ErrorCode_BadSequenceOfCalls);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1354 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1355 else
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1356 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1357 boost::mutex::scoped_lock lock(registry_.mutex_);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1358 registry_.CheckInvariants();
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1359 assert(handler_->GetState() == JobState_Running);
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1360
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1361 return handler_->IsCancelScheduled();
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1362 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1363 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1364
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1365
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1366 void JobsRegistry::RunningJob::MarkSuccess()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1367 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1368 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1369 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1370 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1371 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1372 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1373 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1374 targetState_ = JobState_Success;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1375 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1376 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1377
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1378
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1379 void JobsRegistry::RunningJob::MarkFailure()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1380 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1381 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1382 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1383 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1384 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1385 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1386 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1387 targetState_ = JobState_Failure;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1388 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1389 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1390
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1391
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1392 void JobsRegistry::RunningJob::MarkCanceled()
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1393 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1394 if (!IsValid())
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1395 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1396 throw OrthancException(ErrorCode_BadSequenceOfCalls);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1397 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1398 else
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1399 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1400 targetState_ = JobState_Failure;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1401 canceled_ = true;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1402 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1403 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1404
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1405
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1406 void JobsRegistry::RunningJob::MarkPause()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1407 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1408 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1409 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1410 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1411 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1412 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1413 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1414 targetState_ = JobState_Paused;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1415 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1416 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1417
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1418
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1419 void JobsRegistry::RunningJob::MarkRetry(unsigned int timeout)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1420 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1421 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1422 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1423 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1424 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1425 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1426 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1427 targetState_ = JobState_Retry;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1428 targetRetryTimeout_ = timeout;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1429 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1430 }
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1431
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1432
3240
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
1433 void JobsRegistry::RunningJob::UpdateStatus(ErrorCode code,
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
1434 const std::string& details)
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1435 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1436 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1437 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1438 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1439 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1440 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1441 {
3240
e44e0127e553 Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3191
diff changeset
1442 JobStatus status(code, details, *job_);
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1443
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1444 boost::mutex::scoped_lock lock(registry_.mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1445 registry_.CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1446 assert(handler_->GetState() == JobState_Running);
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1447
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1448 handler_->SetLastStatus(status);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1449 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1450 }
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1451
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1452
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1453
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1454 void JobsRegistry::Serialize(Json::Value& target)
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1455 {
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1456 boost::mutex::scoped_lock lock(mutex_);
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1457 CheckInvariants();
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1458
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1459 target = Json::objectValue;
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1460 target[TYPE] = JOBS_REGISTRY;
2670
c5646f766b3e serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2669
diff changeset
1461 target[JOBS] = Json::objectValue;
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1462
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1463 for (JobsIndex::const_iterator it = jobsIndex_.begin();
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1464 it != jobsIndex_.end(); ++it)
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1465 {
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1466 Json::Value v;
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1467 if (it->second->Serialize(v))
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1468 {
2670
c5646f766b3e serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2669
diff changeset
1469 target[JOBS][it->first] = v;
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1470 }
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1471 }
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1472 }
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1473
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1474
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1475 JobsRegistry::JobsRegistry(IJobUnserializer& unserializer,
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1476 const Json::Value& s,
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1477 size_t maxCompletedJobs) :
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1478 maxCompletedJobs_(maxCompletedJobs),
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1479 observer_(NULL)
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1480 {
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1481 if (SerializationToolbox::ReadString(s, TYPE) != JOBS_REGISTRY ||
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1482 !s.isMember(JOBS) ||
2670
c5646f766b3e serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2669
diff changeset
1483 s[JOBS].type() != Json::objectValue)
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1484 {
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1485 throw OrthancException(ErrorCode_BadFileFormat);
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1486 }
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1487
2670
c5646f766b3e serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2669
diff changeset
1488 Json::Value::Members members = s[JOBS].getMemberNames();
c5646f766b3e serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2669
diff changeset
1489
c5646f766b3e serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2669
diff changeset
1490 for (Json::Value::Members::const_iterator it = members.begin();
c5646f766b3e serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2669
diff changeset
1491 it != members.end(); ++it)
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1492 {
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
1493 std::unique_ptr<JobHandler> job;
3191
20826867141f ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3179
diff changeset
1494
20826867141f ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3179
diff changeset
1495 try
20826867141f ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3179
diff changeset
1496 {
20826867141f ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3179
diff changeset
1497 job.reset(new JobHandler(unserializer, s[JOBS][*it], *it));
20826867141f ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3179
diff changeset
1498 }
20826867141f ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3179
diff changeset
1499 catch (OrthancException& e)
20826867141f ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3179
diff changeset
1500 {
20826867141f ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3179
diff changeset
1501 LOG(WARNING) << "Cannot unserialize one job from previous execution, "
20826867141f ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3179
diff changeset
1502 << "skipping it: " << e.What();
20826867141f ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3179
diff changeset
1503 continue;
20826867141f ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3179
diff changeset
1504 }
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1505
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1506 const boost::posix_time::ptime lastChangeTime = job->GetLastStateChangeTime();
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1507
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1508 std::string id;
2950
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1509 SubmitInternal(id, job.release());
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1510
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1511 // Check whether the job has not been removed (which could be
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1512 // the case if the "maxCompletedJobs_" value gets smaller)
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1513 JobsIndex::iterator found = jobsIndex_.find(id);
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1514 if (found != jobsIndex_.end())
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1515 {
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1516 // The job still lies in the history: Update the time of its
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1517 // last change to the time that was serialized
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1518 assert(found->second != NULL);
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1519 found->second->SetLastStateChangeTime(lastChangeTime);
dc18d5804746 support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2939
diff changeset
1520 }
2667
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1521 }
5fa2f2ce74f0 serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2663
diff changeset
1522 }
3177
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1523
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1524
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1525 void JobsRegistry::GetStatistics(unsigned int& pending,
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1526 unsigned int& running,
3179
fca730c267d7 New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3177
diff changeset
1527 unsigned int& success,
fca730c267d7 New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3177
diff changeset
1528 unsigned int& failed)
3177
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1529 {
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1530 boost::mutex::scoped_lock lock(mutex_);
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1531 CheckInvariants();
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1532
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1533 pending = 0;
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1534 running = 0;
3179
fca730c267d7 New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3177
diff changeset
1535 success = 0;
fca730c267d7 New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3177
diff changeset
1536 failed = 0;
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1537
3177
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1538 for (JobsIndex::const_iterator it = jobsIndex_.begin();
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1539 it != jobsIndex_.end(); ++it)
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1540 {
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1541 JobHandler& job = *it->second;
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1542
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1543 switch (job.GetState())
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1544 {
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1545 case JobState_Retry:
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1546 case JobState_Pending:
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1547 pending ++;
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1548 break;
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1549
3179
fca730c267d7 New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3177
diff changeset
1550 case JobState_Paused:
3177
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1551 case JobState_Running:
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1552 running ++;
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1553 break;
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1554
3177
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1555 case JobState_Success:
3179
fca730c267d7 New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3177
diff changeset
1556 success ++;
fca730c267d7 New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3177
diff changeset
1557 break;
fca730c267d7 New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3177
diff changeset
1558
3177
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1559 case JobState_Failure:
3179
fca730c267d7 New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3177
diff changeset
1560 failed ++;
3177
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1561 break;
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1562
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1563 default:
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1564 throw OrthancException(ErrorCode_InternalError);
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1565 }
4024
1d2b31fc782f new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents: 4022
diff changeset
1566 }
3177
053e72ff9fc5 new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3162
diff changeset
1567 }
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1568 }