annotate Core/JobsEngine/JobsRegistry.cpp @ 2589:a3fdfb6979ed jobs

getting rid of ReusableDicomConnection in REST API
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 15 May 2018 16:10:03 +0200
parents 1b6a6d80b6f2
children 140a539b4eba
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
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
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
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU General Public License as
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * published by the Free Software Foundation, either version 3 of the
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * License, or (at your option) any later version.
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * In addition, as a special exception, the copyright holders of this
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * program give permission to link the code of its release with the
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * that use the same license as the "OpenSSL" library), and distribute
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * the linked executables. You must obey the GNU General Public License
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * in all respects for all of the code used other than "OpenSSL". If you
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * modify file(s) with this exception, you may extend this exception to
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * your version of the file(s), but you are not obligated to do so. If
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * you do not wish to do so, delete this exception statement from your
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * version. If you delete this exception statement from all source files
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 * in the program, then also delete it here.
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 *
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * This program is distributed in the hope that it will be useful, but
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 * General Public License for more details.
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 *
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * You should have received a copy of the GNU General Public License
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 **/
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "../PrecompiledHeaders.h"
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include "JobsRegistry.h"
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #include "../Logging.h"
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 #include "../OrthancException.h"
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 #include "../Toolbox.h"
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 namespace Orthanc
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 class JobsRegistry::JobHandler : public boost::noncopyable
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 private:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 std::string id_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 JobState state_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 std::auto_ptr<IJob> job_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 int priority_; // "+inf()" means highest priority
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 boost::posix_time::ptime creationTime_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 boost::posix_time::ptime lastStateChangeTime_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 boost::posix_time::time_duration runtime_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 boost::posix_time::ptime retryTime_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 bool pauseScheduled_;
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
55 bool cancelScheduled_;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 JobStatus lastStatus_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 void Touch()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 if (state_ == JobState_Running)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 runtime_ += (now - lastStateChangeTime_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 lastStateChangeTime_ = now;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 void SetStateInternal(JobState state)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 state_ = state;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 pauseScheduled_ = false;
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
74 cancelScheduled_ = false;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 Touch();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 public:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 JobHandler(IJob* job,
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 int priority) :
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 id_(Toolbox::GenerateUuid()),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 state_(JobState_Pending),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 job_(job),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 priority_(priority),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 creationTime_(boost::posix_time::microsec_clock::universal_time()),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 lastStateChangeTime_(creationTime_),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 runtime_(boost::posix_time::milliseconds(0)),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 retryTime_(creationTime_),
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
89 pauseScheduled_(false),
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
90 cancelScheduled_(false)
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 if (job == NULL)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 throw OrthancException(ErrorCode_NullPointer);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 lastStatus_ = JobStatus(ErrorCode_Success, *job);
2570
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
98 job->Start();
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 const std::string& GetId() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 return id_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 IJob& GetJob() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 assert(job_.get() != NULL);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 return *job_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 void SetPriority(int priority)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 priority_ = priority;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 int GetPriority() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 return priority_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 JobState GetState() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 return state_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 void SetState(JobState state)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 if (state == JobState_Retry)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 // Use "SetRetryState()"
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 SetStateInternal(state);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 void SetRetryState(unsigned int timeout)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 if (state_ == JobState_Running)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 SetStateInternal(JobState_Retry);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 retryTime_ = (boost::posix_time::microsec_clock::universal_time() +
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 boost::posix_time::milliseconds(timeout));
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 // Only valid for running jobs
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 void SchedulePause()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 if (state_ == JobState_Running)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 pauseScheduled_ = true;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 // Only valid for running jobs
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
168 void ScheduleCancel()
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
169 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
170 if (state_ == JobState_Running)
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
171 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
172 cancelScheduled_ = true;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
173 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
174 else
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
175 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
176 // Only valid for running jobs
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
177 throw OrthancException(ErrorCode_BadSequenceOfCalls);
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 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
180
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 bool IsPauseScheduled()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 return pauseScheduled_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
186 bool IsCancelScheduled()
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 return cancelScheduled_;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
189 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
190
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 bool IsRetryReady(const boost::posix_time::ptime& now) const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 if (state_ != JobState_Retry)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 return retryTime_ <= now;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 const boost::posix_time::ptime& GetCreationTime() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 return creationTime_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 const boost::posix_time::ptime& GetLastStateChangeTime() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 return lastStateChangeTime_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 const boost::posix_time::time_duration& GetRuntime() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 return runtime_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 const JobStatus& GetLastStatus() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 return lastStatus_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 void SetLastStatus(const JobStatus& status)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 lastStatus_ = status;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 Touch();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 }
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
228
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
229 void SetLastErrorCode(ErrorCode code)
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
230 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
231 lastStatus_.SetErrorCode(code);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
232 }
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 };
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 bool JobsRegistry::PriorityComparator::operator() (JobHandler*& a,
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 JobHandler*& b) const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 return a->GetPriority() < b->GetPriority();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 #if defined(NDEBUG)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 void JobsRegistry::CheckInvariants() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 #else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 bool JobsRegistry::IsPendingJob(const JobHandler& job) const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 PendingJobs copy = pendingJobs_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 while (!copy.empty())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 if (copy.top() == &job)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 return true;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 copy.pop();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 return false;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 bool JobsRegistry::IsCompletedJob(JobHandler& job) const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 for (CompletedJobs::const_iterator it = completedJobs_.begin();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 it != completedJobs_.end(); ++it)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 if (*it == &job)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 return true;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 return false;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 bool JobsRegistry::IsRetryJob(JobHandler& job) const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 return retryJobs_.find(&job) != retryJobs_.end();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 void JobsRegistry::CheckInvariants() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287 PendingJobs copy = pendingJobs_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 while (!copy.empty())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 assert(copy.top()->GetState() == JobState_Pending);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291 copy.pop();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 assert(completedJobs_.size() <= maxCompletedJobs_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 for (CompletedJobs::const_iterator it = completedJobs_.begin();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 it != completedJobs_.end(); ++it)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 assert((*it)->GetState() == JobState_Success ||
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301 (*it)->GetState() == JobState_Failure);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 for (RetryJobs::const_iterator it = retryJobs_.begin();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 it != retryJobs_.end(); ++it)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 assert((*it)->GetState() == JobState_Retry);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 for (JobsIndex::const_iterator it = jobsIndex_.begin();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311 it != jobsIndex_.end(); ++it)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313 JobHandler& job = *it->second;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315 assert(job.GetId() == it->first);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 switch (job.GetState())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
319 case JobState_Pending:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 assert(!IsRetryJob(job) && IsPendingJob(job) && !IsCompletedJob(job));
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
321 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
322
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
323 case JobState_Success:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 case JobState_Failure:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 assert(!IsRetryJob(job) && !IsPendingJob(job) && IsCompletedJob(job));
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328 case JobState_Retry:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
329 assert(IsRetryJob(job) && !IsPendingJob(job) && !IsCompletedJob(job));
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332 case JobState_Running:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333 case JobState_Paused:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334 assert(!IsRetryJob(job) && !IsPendingJob(job) && !IsCompletedJob(job));
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
337 default:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
338 throw OrthancException(ErrorCode_InternalError);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
341 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
342 #endif
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
345 void JobsRegistry::ForgetOldCompletedJobs()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
346 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
347 if (maxCompletedJobs_ != 0)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349 while (completedJobs_.size() > maxCompletedJobs_)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 assert(completedJobs_.front() != NULL);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353 std::string id = completedJobs_.front()->GetId();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354 assert(jobsIndex_.find(id) != jobsIndex_.end());
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 jobsIndex_.erase(id);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 delete(completedJobs_.front());
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358 completedJobs_.pop_front();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 }
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
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
364 void JobsRegistry::SetCompletedJob(JobHandler& job,
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
365 bool success)
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
366 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
367 job.SetState(success ? JobState_Success : JobState_Failure);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
368
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
369 completedJobs_.push_back(&job);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
370 ForgetOldCompletedJobs();
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
371
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
372 someJobComplete_.notify_all();
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
373 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
374
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
375
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376 void JobsRegistry::MarkRunningAsCompleted(JobHandler& job,
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377 bool success)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379 LOG(INFO) << "Job has completed with " << (success ? "success" : "failure")
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 << ": " << job.GetId();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382 CheckInvariants();
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
383
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384 assert(job.GetState() == JobState_Running);
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
385 SetCompletedJob(job, success);
2570
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
386
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 void JobsRegistry::MarkRunningAsRetry(JobHandler& job,
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392 unsigned int timeout)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394 LOG(INFO) << "Job scheduled for retry in " << timeout << "ms: " << job.GetId();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
395
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
397
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
398 assert(job.GetState() == JobState_Running &&
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
399 retryJobs_.find(&job) == retryJobs_.end());
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
400
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401 retryJobs_.insert(&job);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
402 job.SetRetryState(timeout);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
404 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
405 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
406
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408 void JobsRegistry::MarkRunningAsPaused(JobHandler& job)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410 LOG(INFO) << "Job paused: " << job.GetId();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
411
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
412 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
413 assert(job.GetState() == JobState_Running);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
414
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
415 job.SetState(JobState_Paused);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
416
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
417 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
418 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
419
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
420
2570
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
421 bool JobsRegistry::GetStateInternal(JobState& state,
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
422 const std::string& id)
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
423 {
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
424 CheckInvariants();
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
425
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
426 JobsIndex::const_iterator it = jobsIndex_.find(id);
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
427 if (it == jobsIndex_.end())
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
428 {
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
429 return false;
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
430 }
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
431 else
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
432 {
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
433 state = it->second->GetState();
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
434 return true;
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
435 }
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
436 }
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
437
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
438
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
439 JobsRegistry::~JobsRegistry()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
440 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
441 for (JobsIndex::iterator it = jobsIndex_.begin(); it != jobsIndex_.end(); ++it)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
442 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
443 assert(it->second != NULL);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
444 delete it->second;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
445 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
446 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
447
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
448
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
449 void JobsRegistry::SetMaxCompletedJobs(size_t i)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
450 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
451 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
452 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
453
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
454 maxCompletedJobs_ = i;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
455 ForgetOldCompletedJobs();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
456
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
457 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
458 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
459
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
460
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
461 void JobsRegistry::ListJobs(std::set<std::string>& target)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
462 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
463 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
464 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
465
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
466 for (JobsIndex::const_iterator it = jobsIndex_.begin();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
467 it != jobsIndex_.end(); ++it)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
468 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
469 target.insert(it->first);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
470 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
471 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
472
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
473
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
474 bool JobsRegistry::GetJobInfo(JobInfo& target,
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
475 const std::string& id)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
476 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
477 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
478 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
479
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
480 JobsIndex::const_iterator found = jobsIndex_.find(id);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
481
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
482 if (found == jobsIndex_.end())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
483 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
484 return false;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
485 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
486 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
487 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
488 const JobHandler& handler = *found->second;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
489 target = JobInfo(handler.GetId(),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
490 handler.GetPriority(),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
491 handler.GetState(),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
492 handler.GetLastStatus(),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
493 handler.GetCreationTime(),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
494 handler.GetLastStateChangeTime(),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
495 handler.GetRuntime());
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
496 return true;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
497 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
498 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
499
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
500
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
501 void JobsRegistry::Submit(std::string& id,
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
502 IJob* job, // Takes ownership
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
503 int priority)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
504 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
505 std::auto_ptr<JobHandler> handler(new JobHandler(job, priority));
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
506
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
507 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
508 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
509
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
510 id = handler->GetId();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
511
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
512 pendingJobs_.push(handler.get());
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
513 pendingJobAvailable_.notify_one();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
514
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
515 jobsIndex_.insert(std::make_pair(id, handler.release()));
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
516
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
517 LOG(INFO) << "New job submitted with priority " << priority << ": " << id;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
518
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
519 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
520 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
521
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
522
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
523 void JobsRegistry::Submit(IJob* job, // Takes ownership
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
524 int priority)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
525 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
526 std::string id;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
527 Submit(id, job, priority);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
528 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
529
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
530
2570
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
531 bool JobsRegistry::SubmitAndWait(IJob* job, // Takes ownership
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
532 int priority)
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
533 {
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
534 std::string id;
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
535 Submit(id, job, priority);
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
536
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
537 JobState state;
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
538
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
539 {
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
540 boost::mutex::scoped_lock lock(mutex_);
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
541
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
542 while (GetStateInternal(state, id) &&
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
543 state != JobState_Success &&
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
544 state != JobState_Failure)
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
545 {
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
546 someJobComplete_.wait(lock);
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 }
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 return (state == JobState_Success);
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
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
553
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
554 bool JobsRegistry::SetPriority(const std::string& id,
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
555 int priority)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
556 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
557 LOG(INFO) << "Changing priority to " << priority << " for job: " << id;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
558
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
559 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
560 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
561
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
562 JobsIndex::iterator found = jobsIndex_.find(id);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
563
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
564 if (found == jobsIndex_.end())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
565 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
566 LOG(WARNING) << "Unknown job: " << id;
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
567 return false;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
568 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
569 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
570 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
571 found->second->SetPriority(priority);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
572
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
573 if (found->second->GetState() == JobState_Pending)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
574 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
575 // If the job is pending, we need to reconstruct the
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
576 // priority queue, as the heap condition has changed
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
577
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
578 PendingJobs copy;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
579 std::swap(copy, pendingJobs_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
580
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
581 assert(pendingJobs_.empty());
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
582 while (!copy.empty())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
583 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
584 pendingJobs_.push(copy.top());
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
585 copy.pop();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
586 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
587 }
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
588
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
589 CheckInvariants();
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
590 return true;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
591 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
592 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
593
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
594
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
595 void JobsRegistry::RemovePendingJob(const std::string& id)
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
596 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
597 // If the job is pending, we need to reconstruct the priority
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
598 // queue to remove it
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
599 PendingJobs copy;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
600 std::swap(copy, pendingJobs_);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
601
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
602 assert(pendingJobs_.empty());
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
603 while (!copy.empty())
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
604 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
605 if (copy.top()->GetId() != id)
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
606 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
607 pendingJobs_.push(copy.top());
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
608 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
609
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
610 copy.pop();
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
611 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
612 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
613
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
614
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
615 void JobsRegistry::RemoveRetryJob(JobHandler* handler)
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
616 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
617 RetryJobs::iterator item = retryJobs_.find(handler);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
618 assert(item != retryJobs_.end());
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
619 retryJobs_.erase(item);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
620 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
621
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
622
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
623 bool JobsRegistry::Pause(const std::string& id)
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
624 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
625 LOG(INFO) << "Pausing job: " << id;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
626
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
627 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
628 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
629
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
630 JobsIndex::iterator found = jobsIndex_.find(id);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
631
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
632 if (found == jobsIndex_.end())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
633 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
634 LOG(WARNING) << "Unknown job: " << id;
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
635 return false;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
636 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
637 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
638 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
639 switch (found->second->GetState())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
640 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
641 case JobState_Pending:
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
642 RemovePendingJob(id);
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
643 found->second->SetState(JobState_Paused);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
644 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
645
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
646 case JobState_Retry:
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
647 RemoveRetryJob(found->second);
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
648 found->second->SetState(JobState_Paused);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
649 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
650
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
651 case JobState_Paused:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
652 case JobState_Success:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
653 case JobState_Failure:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
654 // Nothing to be done
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
655 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
656
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
657 case JobState_Running:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
658 found->second->SchedulePause();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
659 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
660
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
661 default:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
662 throw OrthancException(ErrorCode_InternalError);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
663 }
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
664
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
665 CheckInvariants();
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
666 return true;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
667 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
668 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
669
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
670
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
671 bool JobsRegistry::Cancel(const std::string& id)
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
672 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
673 LOG(INFO) << "Canceling job: " << id;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
674
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
675 boost::mutex::scoped_lock lock(mutex_);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
676 CheckInvariants();
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
677
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
678 JobsIndex::iterator found = jobsIndex_.find(id);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
679
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
680 if (found == jobsIndex_.end())
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
681 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
682 LOG(WARNING) << "Unknown job: " << id;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
683 return false;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
684 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
685 else
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
686 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
687 switch (found->second->GetState())
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
688 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
689 case JobState_Pending:
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
690 RemovePendingJob(id);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
691 SetCompletedJob(*found->second, false);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
692 found->second->SetLastErrorCode(ErrorCode_CanceledJob);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
693 break;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
694
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
695 case JobState_Retry:
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
696 RemoveRetryJob(found->second);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
697 SetCompletedJob(*found->second, false);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
698 found->second->SetLastErrorCode(ErrorCode_CanceledJob);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
699 break;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
700
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
701 case JobState_Paused:
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
702 SetCompletedJob(*found->second, false);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
703 found->second->SetLastErrorCode(ErrorCode_CanceledJob);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
704 break;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
705
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
706 case JobState_Success:
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
707 case JobState_Failure:
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
708 // Nothing to be done
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
709 break;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
710
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
711 case JobState_Running:
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
712 found->second->ScheduleCancel();
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
713 break;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
714
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
715 default:
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
716 throw OrthancException(ErrorCode_InternalError);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
717 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
718
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
719 CheckInvariants();
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
720 return true;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
721 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
722 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
723
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
724
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
725 bool JobsRegistry::Resume(const std::string& id)
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
726 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
727 LOG(INFO) << "Resuming job: " << id;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
728
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
729 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
730 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
731
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
732 JobsIndex::iterator found = jobsIndex_.find(id);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
733
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
734 if (found == jobsIndex_.end())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
735 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
736 LOG(WARNING) << "Unknown job: " << id;
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
737 return false;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
738 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
739 else if (found->second->GetState() != JobState_Paused)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
740 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
741 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
742 return false;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
743 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
744 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
745 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
746 found->second->SetState(JobState_Pending);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
747 pendingJobs_.push(found->second);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
748 pendingJobAvailable_.notify_one();
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
749 CheckInvariants();
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
750 return true;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
751 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
752 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
753
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
754
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
755 bool JobsRegistry::Resubmit(const std::string& id)
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
756 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
757 LOG(INFO) << "Resubmitting failed job: " << id;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
758
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
759 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
760 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
761
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
762 JobsIndex::iterator found = jobsIndex_.find(id);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
763
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
764 if (found == jobsIndex_.end())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
765 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
766 LOG(WARNING) << "Unknown job: " << id;
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
767 return false;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
768 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
769 else if (found->second->GetState() != JobState_Failure)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
770 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
771 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
772 return false;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
773 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
774 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
775 {
2583
1b6a6d80b6f2 OrthancPeerStoreJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2581
diff changeset
776 found->second->GetJob().SignalResubmit();
1b6a6d80b6f2 OrthancPeerStoreJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2581
diff changeset
777
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
778 bool ok = false;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
779 for (CompletedJobs::iterator it = completedJobs_.begin();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
780 it != completedJobs_.end(); ++it)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
781 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
782 if (*it == found->second)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
783 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
784 ok = true;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
785 completedJobs_.erase(it);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
786 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
787 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
788 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
789
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
790 assert(ok);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
791
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
792 found->second->SetState(JobState_Pending);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
793 pendingJobs_.push(found->second);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
794 pendingJobAvailable_.notify_one();
2573
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
795
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
796 CheckInvariants();
3372c5255333 StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2570
diff changeset
797 return true;
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
798 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
799 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
800
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
801
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
802 void JobsRegistry::ScheduleRetries()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
803 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
804 boost::mutex::scoped_lock lock(mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
805 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
806
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
807 RetryJobs copy;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
808 std::swap(copy, retryJobs_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
809
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
810 const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
811
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
812 assert(retryJobs_.empty());
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
813 for (RetryJobs::iterator it = copy.begin(); it != copy.end(); ++it)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
814 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
815 if ((*it)->IsRetryReady(now))
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) << "Retrying job: " << (*it)->GetId();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
818 (*it)->SetState(JobState_Pending);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
819 pendingJobs_.push(*it);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
820 pendingJobAvailable_.notify_one();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
821 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
822 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
823 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
824 retryJobs_.insert(*it);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
825 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
826 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
827
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
828 CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
829 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
830
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
831
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
832 bool JobsRegistry::GetState(JobState& state,
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
833 const std::string& id)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
834 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
835 boost::mutex::scoped_lock lock(mutex_);
2570
2e879c796ec7 JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2569
diff changeset
836 return GetStateInternal(state, id);
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
837 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
838
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
839
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
840 JobsRegistry::RunningJob::RunningJob(JobsRegistry& registry,
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
841 unsigned int timeout) :
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
842 registry_(registry),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
843 handler_(NULL),
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
844 targetState_(JobState_Failure),
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
845 targetRetryTimeout_(0),
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
846 canceled_(false)
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
847 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
848 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
849 boost::mutex::scoped_lock lock(registry_.mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
850
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
851 while (registry_.pendingJobs_.empty())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
852 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
853 if (timeout == 0)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
854 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
855 registry_.pendingJobAvailable_.wait(lock);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
856 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
857 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
858 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
859 bool success = registry_.pendingJobAvailable_.timed_wait
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
860 (lock, boost::posix_time::milliseconds(timeout));
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
861 if (!success)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
862 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
863 // No pending job
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
864 return;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
865 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
866 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
867 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
868
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
869 handler_ = registry_.pendingJobs_.top();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
870 registry_.pendingJobs_.pop();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
871
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
872 assert(handler_->GetState() == JobState_Pending);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
873 handler_->SetState(JobState_Running);
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
874 handler_->SetLastErrorCode(ErrorCode_Success);
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
875
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
876 job_ = &handler_->GetJob();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
877 id_ = handler_->GetId();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
878 priority_ = handler_->GetPriority();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
879 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
880 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
881
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
882
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
883 JobsRegistry::RunningJob::~RunningJob()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
884 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
885 if (IsValid())
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(registry_.mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
888
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
889 switch (targetState_)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
890 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
891 case JobState_Failure:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
892 registry_.MarkRunningAsCompleted(*handler_, false);
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
893
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
894 if (canceled_)
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
895 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
896 handler_->SetLastErrorCode(ErrorCode_CanceledJob);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
897 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
898
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
899 break;
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_Success:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
902 registry_.MarkRunningAsCompleted(*handler_, true);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
903 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
904
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
905 case JobState_Paused:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
906 registry_.MarkRunningAsPaused(*handler_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
907 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
908
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
909 case JobState_Retry:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
910 registry_.MarkRunningAsRetry(*handler_, targetRetryTimeout_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
911 break;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
912
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
913 default:
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
914 assert(0);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
915 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
916 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
917 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
918
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
919
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
920 bool JobsRegistry::RunningJob::IsValid() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
921 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
922 return (handler_ != NULL &&
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
923 job_ != NULL);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
924 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
925
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
926
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
927 const std::string& JobsRegistry::RunningJob::GetId() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
928 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
929 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
930 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
931 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
932 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
933 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
934 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
935 return id_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
936 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
937 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
938
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
939
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
940 int JobsRegistry::RunningJob::GetPriority() const
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
941 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
942 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
943 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
944 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
945 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
946 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
947 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
948 return priority_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
949 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
950 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
951
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
952
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
953 IJob& JobsRegistry::RunningJob::GetJob()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
954 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
955 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
956 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
957 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
958 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
959 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
960 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
961 return *job_;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
962 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
963 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
964
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
965
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
966 bool JobsRegistry::RunningJob::IsPauseScheduled()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
967 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
968 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
969 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
970 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
971 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
972 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
973 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
974 boost::mutex::scoped_lock lock(registry_.mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
975 registry_.CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
976 assert(handler_->GetState() == JobState_Running);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
977
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
978 return handler_->IsPauseScheduled();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
979 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
980 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
981
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
982
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
983 bool JobsRegistry::RunningJob::IsCancelScheduled()
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 if (!IsValid())
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 throw OrthancException(ErrorCode_BadSequenceOfCalls);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
988 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
989 else
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
990 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
991 boost::mutex::scoped_lock lock(registry_.mutex_);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
992 registry_.CheckInvariants();
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
993 assert(handler_->GetState() == JobState_Running);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
994
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
995 return handler_->IsCancelScheduled();
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
996 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
997 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
998
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
999
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1000 void JobsRegistry::RunningJob::MarkSuccess()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1001 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1002 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1003 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1004 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1005 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1006 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1007 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1008 targetState_ = JobState_Success;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1009 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1010 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1011
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1012
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1013 void JobsRegistry::RunningJob::MarkFailure()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1014 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1015 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1016 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1017 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1018 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1019 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1020 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1021 targetState_ = JobState_Failure;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1022 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1023 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1024
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1025
2581
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1026 void JobsRegistry::RunningJob::MarkCanceled()
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1027 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1028 if (!IsValid())
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1029 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1030 throw OrthancException(ErrorCode_BadSequenceOfCalls);
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1031 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1032 else
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1033 {
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1034 targetState_ = JobState_Failure;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1035 canceled_ = true;
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1036 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1037 }
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1038
8da2cffc2378 JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2573
diff changeset
1039
2569
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1040 void JobsRegistry::RunningJob::MarkPause()
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1041 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1042 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1043 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1044 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1045 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1046 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1047 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1048 targetState_ = JobState_Paused;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1049 }
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
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1053 void JobsRegistry::RunningJob::MarkRetry(unsigned int timeout)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1054 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1055 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1056 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1057 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1058 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1059 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1060 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1061 targetState_ = JobState_Retry;
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1062 targetRetryTimeout_ = timeout;
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
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1067 void JobsRegistry::RunningJob::UpdateStatus(ErrorCode code)
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1068 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1069 if (!IsValid())
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1070 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1071 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1072 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1073 else
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1074 {
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1075 JobStatus status(code, *job_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1076
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1077 boost::mutex::scoped_lock lock(registry_.mutex_);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1078 registry_.CheckInvariants();
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1079 assert(handler_->GetState() == JobState_Running);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1080
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1081 handler_->SetLastStatus(status);
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1082 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1083 }
2af17cd5eb1f reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1084 }