annotate OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp @ 5134:6aa41d86b948

fix ModificationJob state machine
author Alain Mazy <am@osimis.io>
date Tue, 10 Jan 2023 11:46:00 +0100
parents f2dcdbe05884
children 252385892197
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
1 /**
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
7 *
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
8 * This program is free software: you can redistribute it and/or
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public License
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
10 * as published by the Free Software Foundation, either version 3 of
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
11 * the License, or (at your option) any later version.
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
12 *
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
16 * Lesser General Public License for more details.
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
17 *
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
19 * License along with this program. If not, see
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
20 * <http://www.gnu.org/licenses/>.
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
21 **/
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
22
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
23
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
24 #include "ThreadedSetOfInstancesJob.h"
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
25
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
26 #include "../../../OrthancFramework/Sources/Logging.h"
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
27 #include "../../../OrthancFramework/Sources/OrthancException.h"
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
28 #include "../../../OrthancFramework/Sources/SerializationToolbox.h"
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
29 #include "../ServerContext.h"
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
30
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
31 #include <boost/lexical_cast.hpp>
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
32 #include <cassert>
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
33
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
34 namespace Orthanc
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
35 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
36 static const char* EXIT_WORKER_MESSAGE = "exit";
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
37
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
38 ThreadedSetOfInstancesJob::ThreadedSetOfInstancesJob(ServerContext& context,
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
39 bool hasPostProcessing,
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
40 bool keepSource,
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
41 size_t workersCount) :
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
42 processedInstancesCount_(0),
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
43 hasPostProcessing_(hasPostProcessing),
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
44 started_(false),
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
45 stopRequested_(false),
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
46 permissive_(false),
5134
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
47 currentStep_(ThreadedJobStep_ProcessingInstances),
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
48 workersCount_(workersCount),
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
49 context_(context),
5134
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
50 keepSource_(keepSource),
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
51 errorCode_(ErrorCode_Success)
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
52 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
53 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
54
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
55
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
56 ThreadedSetOfInstancesJob::~ThreadedSetOfInstancesJob()
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
57 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
58 // no need to lock mutex here since we access variables used only by the "master" thread
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
59
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
60 StopWorkers();
5134
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
61 WaitWorkersComplete();
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
62 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
63
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
64
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
65 void ThreadedSetOfInstancesJob::InitWorkers(size_t workersCount)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
66 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
67 // no need to lock mutex here since we access variables used only by the "master" thread
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
68
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
69 for (size_t i = 0; i < workersCount; i++)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
70 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
71 instancesWorkers_.push_back(boost::shared_ptr<boost::thread>(new boost::thread(InstanceWorkerThread, this)));
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
72 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
73 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
74
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
75
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
76 void ThreadedSetOfInstancesJob::WaitWorkersComplete()
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
77 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
78 // no need to lock mutex here since we access variables used only by the "master" thread
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
79
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
80 // send a dummy "exit" message to all workers such that they stop waiting for messages on the queue
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
81 for (size_t i = 0; i < instancesWorkers_.size(); i++)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
82 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
83 instancesToProcess_.Enqueue(new SingleValueObject<std::string>(EXIT_WORKER_MESSAGE));
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
84 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
85
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
86 for (size_t i = 0; i < instancesWorkers_.size(); i++)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
87 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
88 if (instancesWorkers_[i]->joinable())
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
89 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
90 instancesWorkers_[i]->join();
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
91 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
92 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
93
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
94 instancesWorkers_.clear();
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
95 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
96
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
97
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
98 void ThreadedSetOfInstancesJob::StopWorkers()
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
99 {
5134
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
100 boost::recursive_mutex::scoped_lock lock(mutex_);
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
101
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
102 stopRequested_ = true;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
103 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
104
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
105
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
106 void ThreadedSetOfInstancesJob::Stop(JobStopReason reason)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
107 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
108 // no need to lock mutex here since we access variables used or set only by the "master" thread
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
109
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
110 if (reason == JobStopReason_Canceled ||
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
111 reason == JobStopReason_Failure ||
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
112 reason == JobStopReason_Retry)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
113 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
114 // deallocate resources
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
115 StopWorkers();
5134
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
116 WaitWorkersComplete();
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
117 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
118 else if (reason == JobStopReason_Paused)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
119 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
120 // keep resources allocated.
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
121 // note that, right now, since all instances are queued from the start, this kind of jobs is not paused while in ProcessingInstances state
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
122 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
123 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
124
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
125
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
126 JobStepResult ThreadedSetOfInstancesJob::Step(const std::string& jobId)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
127 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
128 // no need to lock mutex here since we access variables used or set only by the "master" thread
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
129
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
130 if (!started_)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
131 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
132 throw OrthancException(ErrorCode_InternalError);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
133 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
134
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
135 if (GetInstancesCount() == 0)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
136 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
137 // No instances to handle: We're done
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
138 return JobStepResult::Success();
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
139 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
140
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
141 try
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
142 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
143 if (currentStep_ == ThreadedJobStep_ProcessingInstances)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
144 {
5134
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
145 // create the workers and enqueue all instances
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
146 for (std::set<std::string>::const_iterator it = instances_.begin(); it != instances_.end(); ++it)
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
147 {
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
148 instancesToProcess_.Enqueue(new SingleValueObject<std::string>(*it));
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
149 }
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
150
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
151 InitWorkers(workersCount_);
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
152 // wait until all instances are processed by the workers
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
153 WaitWorkersComplete();
5134
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
154
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
155 // check job has really completed !!! it might have been interrupted because of an error
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
156 if ((processedInstancesCount_ != instances_.size())
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
157 || (!IsPermissive() && failedInstances_.size() > 0))
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
158 {
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
159 return JobStepResult::Failure(GetErrorCode(), NULL);
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
160 }
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
161
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
162 currentStep_ = ThreadedJobStep_PostProcessingInstances;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
163 return JobStepResult::Continue();
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
164 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
165 else if (currentStep_ == ThreadedJobStep_PostProcessingInstances)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
166 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
167 if (HasPostProcessingStep())
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
168 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
169 PostProcessInstances();
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
170 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
171
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
172 currentStep_ = ThreadedJobStep_Cleanup;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
173 return JobStepResult::Continue();
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
174 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
175 else if (currentStep_ == ThreadedJobStep_Cleanup)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
176 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
177 // clean after the post processing step
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
178 if (HasCleanupStep())
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
179 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
180 for (std::set<std::string>::const_iterator it = instances_.begin(); it != instances_.end(); ++it)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
181 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
182 Json::Value tmp;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
183 context_.DeleteResource(tmp, *it, ResourceType_Instance);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
184 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
185 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
186
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
187 currentStep_ = ThreadedJobStep_Done;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
188 return JobStepResult::Success();
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
189 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
190 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
191 catch (OrthancException& e)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
192 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
193 if (permissive_)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
194 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
195 LOG(WARNING) << "Ignoring an error in a permissive job: " << e.What();
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
196 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
197 else
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
198 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
199 return JobStepResult::Failure(e);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
200 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
201 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
202
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
203 return JobStepResult::Continue();
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
204 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
205
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
206
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
207 bool ThreadedSetOfInstancesJob::HasPostProcessingStep() const
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
208 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
209 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
210
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
211 return hasPostProcessing_;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
212 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
213
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
214
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
215 bool ThreadedSetOfInstancesJob::HasCleanupStep() const
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
216 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
217 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
218
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
219 return !keepSource_;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
220 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
221
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
222
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
223 void ThreadedSetOfInstancesJob::InstanceWorkerThread(ThreadedSetOfInstancesJob* that)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
224 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
225 while (true)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
226 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
227 std::unique_ptr<SingleValueObject<std::string> > instanceId(dynamic_cast<SingleValueObject<std::string>*>(that->instancesToProcess_.Dequeue(0)));
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
228 if (that->stopRequested_ // no lock(mutex) to access this variable, this is safe since it's just reading a boolean
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
229 || instanceId->GetValue() == EXIT_WORKER_MESSAGE)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
230 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
231 return;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
232 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
233
5134
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
234 try
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
235 {
5134
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
236 bool processed = that->HandleInstance(instanceId->GetValue());
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
237
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
238 {
5134
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
239 boost::recursive_mutex::scoped_lock lock(that->mutex_);
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
240
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
241 that->processedInstancesCount_++;
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
242 if (!processed)
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
243 {
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
244 that->failedInstances_.insert(instanceId->GetValue());
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
245 }
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
246 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
247 }
5134
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
248 catch (const Orthanc::OrthancException& e)
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
249 {
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
250 if (that->IsPermissive())
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
251 {
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
252 LOG(WARNING) << "Ignoring an error in a permissive job: " << e.What();
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
253 }
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
254 else
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
255 {
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
256 LOG(ERROR) << "Error in a non-permissive job: " << e.What();
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
257 that->SetErrorCode(e.GetErrorCode());
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
258 that->StopWorkers();
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
259 }
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
260 }
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
261 catch (...)
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
262 {
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
263 LOG(ERROR) << "Native exception while executing a job";
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
264 that->SetErrorCode(e.GetErrorCode());
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
265 that->StopWorkers();
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
266 }
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
267
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
268 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
269 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
270
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
271
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
272 bool ThreadedSetOfInstancesJob::GetOutput(std::string &output,
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
273 MimeType &mime,
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
274 std::string& filename,
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
275 const std::string &key)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
276 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
277 return false;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
278 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
279
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
280
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
281 size_t ThreadedSetOfInstancesJob::GetInstancesCount() const
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
282 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
283 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
284
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
285 return instances_.size();
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
286 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
287
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
288
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
289 void ThreadedSetOfInstancesJob::GetFailedInstances(std::set<std::string>& target) const
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
290 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
291 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
292
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
293 target = failedInstances_;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
294 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
295
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
296
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
297 void ThreadedSetOfInstancesJob::GetInstances(std::set<std::string>& target) const
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
298 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
299 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
300
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
301 target = instances_;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
302 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
303
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
304
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
305 bool ThreadedSetOfInstancesJob::IsFailedInstance(const std::string &instance) const
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
306 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
307 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
308
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
309 return failedInstances_.find(instance) != failedInstances_.end();
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
310 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
311
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
312
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
313 void ThreadedSetOfInstancesJob::Start()
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
314 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
315 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
316
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
317 started_ = true;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
318 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
319
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
320
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
321 void ThreadedSetOfInstancesJob::Reset()
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
322 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
323 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
324
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
325 if (started_)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
326 {
5134
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
327 currentStep_ = ThreadedJobStep_ProcessingInstances;
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
328 stopRequested_ = false;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
329 processedInstancesCount_ = 0;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
330 failedInstances_.clear();
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
331 instancesToProcess_.Clear();
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
332 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
333 else
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
334 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
335 throw OrthancException(ErrorCode_BadSequenceOfCalls);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
336 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
337 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
338
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
339
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
340 static const char* KEY_FAILED_INSTANCES = "FailedInstances";
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
341 static const char* KEY_PARENT_RESOURCES = "ParentResources";
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
342 static const char* KEY_DESCRIPTION = "Description";
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
343 static const char* KEY_PERMISSIVE = "Permissive";
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
344 static const char* KEY_CURRENT_STEP = "CurrentStep";
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
345 static const char* KEY_TYPE = "Type";
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
346 static const char* KEY_INSTANCES = "Instances";
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
347 static const char* KEY_INSTANCES_COUNT = "InstancesCount";
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
348 static const char* KEY_FAILED_INSTANCES_COUNT = "FailedInstancesCount";
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
349 static const char* KEY_KEEP_SOURCE = "KeepSource";
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
350 static const char* KEY_WORKERS_COUNT = "WorkersCount";
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
351
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
352
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
353 void ThreadedSetOfInstancesJob::GetPublicContent(Json::Value& target)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
354 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
355 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
356
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
357 target[KEY_DESCRIPTION] = GetDescription();
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
358 target[KEY_INSTANCES_COUNT] = static_cast<uint32_t>(GetInstancesCount());
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
359 target[KEY_FAILED_INSTANCES_COUNT] = static_cast<uint32_t>(failedInstances_.size());
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
360
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
361 if (!parentResources_.empty())
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
362 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
363 SerializationToolbox::WriteSetOfStrings(target, parentResources_, KEY_PARENT_RESOURCES);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
364 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
365 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
366
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
367
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
368 bool ThreadedSetOfInstancesJob::Serialize(Json::Value& target)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
369 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
370 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
371
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
372 target = Json::objectValue;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
373
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
374 std::string type;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
375 GetJobType(type);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
376 target[KEY_TYPE] = type;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
377
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
378 target[KEY_PERMISSIVE] = permissive_;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
379 target[KEY_CURRENT_STEP] = static_cast<unsigned int>(currentStep_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
380 target[KEY_DESCRIPTION] = description_;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
381 target[KEY_KEEP_SOURCE] = keepSource_;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
382 target[KEY_WORKERS_COUNT] = static_cast<unsigned int>(workersCount_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
383
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
384 SerializationToolbox::WriteSetOfStrings(target, instances_, KEY_INSTANCES);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
385 SerializationToolbox::WriteSetOfStrings(target, failedInstances_, KEY_FAILED_INSTANCES);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
386 SerializationToolbox::WriteSetOfStrings(target, parentResources_, KEY_PARENT_RESOURCES);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
387
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
388 return true;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
389 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
390
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
391
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
392 ThreadedSetOfInstancesJob::ThreadedSetOfInstancesJob(ServerContext& context,
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
393 const Json::Value& source,
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
394 bool hasPostProcessing,
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
395 bool defaultKeepSource) :
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
396 processedInstancesCount_(0),
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
397 hasPostProcessing_(hasPostProcessing),
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
398 started_(false),
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
399 stopRequested_(false),
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
400 permissive_(false),
5134
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
401 currentStep_(ThreadedJobStep_ProcessingInstances),
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
402 workersCount_(1),
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
403 context_(context),
5134
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
404 keepSource_(defaultKeepSource),
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
405 errorCode_(ErrorCode_Success)
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
406 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
407 SerializationToolbox::ReadSetOfStrings(failedInstances_, source, KEY_FAILED_INSTANCES);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
408
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
409 if (source.isMember(KEY_PARENT_RESOURCES))
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
410 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
411 // Backward compatibility with Orthanc <= 1.5.6
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
412 SerializationToolbox::ReadSetOfStrings(parentResources_, source, KEY_PARENT_RESOURCES);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
413 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
414
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
415 if (source.isMember(KEY_KEEP_SOURCE))
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
416 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
417 keepSource_ = SerializationToolbox::ReadBoolean(source, KEY_KEEP_SOURCE);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
418 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
419
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
420 if (source.isMember(KEY_WORKERS_COUNT))
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
421 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
422 workersCount_ = SerializationToolbox::ReadUnsignedInteger(source, KEY_WORKERS_COUNT);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
423 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
424
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
425 if (source.isMember(KEY_INSTANCES))
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
426 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
427 SerializationToolbox::ReadSetOfStrings(instances_, source, KEY_INSTANCES);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
428 }
5134
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
429
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
430 if (source.isMember(KEY_CURRENT_STEP))
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
431 {
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
432 currentStep_ = static_cast<ThreadedJobStep>(SerializationToolbox::ReadUnsignedInteger(source, KEY_CURRENT_STEP));
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
433 }
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
434 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
435
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
436
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
437 void ThreadedSetOfInstancesJob::SetKeepSource(bool keep)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
438 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
439 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
440
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
441 if (IsStarted())
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
442 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
443 throw OrthancException(ErrorCode_BadSequenceOfCalls);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
444 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
445
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
446 keepSource_ = keep;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
447 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
448
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
449
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
450 float ThreadedSetOfInstancesJob::GetProgress()
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
451 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
452 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
453
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
454 if (GetInstancesCount() == 0)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
455 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
456 return 1;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
457 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
458 else
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
459 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
460 size_t totalProgress = GetInstancesCount();
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
461 size_t currentProgress = processedInstancesCount_;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
462
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
463 if (HasPostProcessingStep())
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
464 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
465 ++totalProgress;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
466 if (currentStep_ > ThreadedJobStep_PostProcessingInstances)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
467 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
468 ++currentProgress;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
469 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
470 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
471
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
472 if (HasCleanupStep())
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
473 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
474 ++totalProgress;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
475 if (currentStep_ > ThreadedJobStep_Cleanup)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
476 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
477 ++currentProgress;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
478 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
479 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
480
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
481 return (static_cast<float>(currentProgress) /
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
482 static_cast<float>(totalProgress));
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
483 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
484 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
485
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
486
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
487 ThreadedSetOfInstancesJob::ThreadedJobStep ThreadedSetOfInstancesJob::GetCurrentStep() const
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
488 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
489 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
490
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
491 return currentStep_;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
492 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
493
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
494
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
495 void ThreadedSetOfInstancesJob::SetDescription(const std::string &description)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
496 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
497 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
498
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
499 description_ = description;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
500 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
501
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
502
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
503 const std::string& ThreadedSetOfInstancesJob::GetDescription() const
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
504 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
505 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
506
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
507 return description_;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
508 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
509
5134
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
510 void ThreadedSetOfInstancesJob::SetErrorCode(ErrorCode errorCode)
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
511 {
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
512 boost::recursive_mutex::scoped_lock lock(mutex_);
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
513
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
514 errorCode_ = errorCode;
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
515 }
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
516
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
517 ErrorCode ThreadedSetOfInstancesJob::GetErrorCode() const
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
518 {
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
519 boost::recursive_mutex::scoped_lock lock(mutex_);
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
520
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
521 return errorCode_;
6aa41d86b948 fix ModificationJob state machine
Alain Mazy <am@osimis.io>
parents: 5130
diff changeset
522 }
5130
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
523
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
524 bool ThreadedSetOfInstancesJob::IsPermissive() const
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
525 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
526 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
527
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
528 return permissive_;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
529 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
530
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
531
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
532 void ThreadedSetOfInstancesJob::SetPermissive(bool permissive)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
533 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
534 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
535
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
536 if (started_)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
537 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
538 throw OrthancException(ErrorCode_BadSequenceOfCalls);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
539 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
540 else
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
541 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
542 permissive_ = permissive;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
543 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
544 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
545
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
546
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
547 bool ThreadedSetOfInstancesJob::IsStarted() const
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
548 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
549 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
550
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
551 return started_;
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
552 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
553
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
554
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
555 void ThreadedSetOfInstancesJob::AddInstances(const std::list<std::string>& instances)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
556 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
557 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
558
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
559 for (std::list<std::string>::const_iterator
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
560 it = instances.begin(); it != instances.end(); ++it)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
561 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
562 instances_.insert(*it);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
563 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
564 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
565
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
566
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
567 void ThreadedSetOfInstancesJob::AddParentResource(const std::string &resource)
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
568 {
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
569 boost::recursive_mutex::scoped_lock lock(mutex_);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
570
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
571 parentResources_.insert(resource);
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
572 }
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
573
f2dcdbe05884 ResourceModification jobs can now use multiple threads
Alain Mazy <am@osimis.io>
parents:
diff changeset
574 }