annotate OrthancFramework/Sources/JobsEngine/JobsRegistry.cpp @ 4300:b30a8de92ad9

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