Mercurial > hg > orthanc
comparison Core/JobsEngine/JobsRegistry.cpp @ 2648:e1893d31652a jobs
serialization of JobHandler
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 31 May 2018 18:44:05 +0200 |
parents | 00327e989458 |
children | a3f0f61a14ca |
comparison
equal
deleted
inserted
replaced
2647:73d7d95dd75e | 2648:e1893d31652a |
---|---|
43 class JobsRegistry::JobHandler : public boost::noncopyable | 43 class JobsRegistry::JobHandler : public boost::noncopyable |
44 { | 44 { |
45 private: | 45 private: |
46 std::string id_; | 46 std::string id_; |
47 JobState state_; | 47 JobState state_; |
48 std::string jobType_; | |
48 std::auto_ptr<IJob> job_; | 49 std::auto_ptr<IJob> job_; |
49 int priority_; // "+inf()" means highest priority | 50 int priority_; // "+inf()" means highest priority |
50 boost::posix_time::ptime creationTime_; | 51 boost::posix_time::ptime creationTime_; |
51 boost::posix_time::ptime lastStateChangeTime_; | 52 boost::posix_time::ptime lastStateChangeTime_; |
52 boost::posix_time::time_duration runtime_; | 53 boost::posix_time::time_duration runtime_; |
92 if (job == NULL) | 93 if (job == NULL) |
93 { | 94 { |
94 throw OrthancException(ErrorCode_NullPointer); | 95 throw OrthancException(ErrorCode_NullPointer); |
95 } | 96 } |
96 | 97 |
98 job->GetJobType(jobType_); | |
97 job->Start(); | 99 job->Start(); |
98 | 100 |
99 lastStatus_ = JobStatus(ErrorCode_Success, *job); | 101 lastStatus_ = JobStatus(ErrorCode_Success, *job_); |
100 } | 102 } |
101 | 103 |
102 const std::string& GetId() const | 104 const std::string& GetId() const |
103 { | 105 { |
104 return id_; | 106 return id_; |
228 } | 230 } |
229 | 231 |
230 void SetLastErrorCode(ErrorCode code) | 232 void SetLastErrorCode(ErrorCode code) |
231 { | 233 { |
232 lastStatus_.SetErrorCode(code); | 234 lastStatus_.SetErrorCode(code); |
235 } | |
236 | |
237 void Serialize(Json::Value& target) const | |
238 { | |
239 target["State"] = EnumerationToString(state_); | |
240 target["JobType"] = jobType_; | |
241 target["Priority"] = priority_; | |
242 target["CreationTime"] = boost::posix_time::to_iso_string(creationTime_); | |
243 target["Runtime"] = static_cast<unsigned int>(runtime_.total_milliseconds()); | |
244 | |
245 if (state_ == JobState_Running) | |
246 { | |
247 // WARNING: Cannot directly access the "job_" member, as long | |
248 // as a "RunningJob" instance is running. We do not use a | |
249 // mutex at the "JobHandler" level, as serialization would be | |
250 // blocked while a step in the job is running. Instead, we | |
251 // save a snapshot of the serialized job. | |
252 target["Job"] = lastStatus_.GetSerialized(); | |
253 } | |
254 else | |
255 { | |
256 job_->Serialize(target["Job"]); | |
257 } | |
258 } | |
259 | |
260 JobHandler(IJobUnserializer& unserializer, | |
261 const std::string& id, | |
262 const Json::Value& serialized) : | |
263 id_(id), | |
264 lastStateChangeTime_(boost::posix_time::microsec_clock::universal_time()), | |
265 pauseScheduled_(false), | |
266 cancelScheduled_(false) | |
267 { | |
268 state_ = StringToJobState(IJobUnserializer::GetString(serialized, "State")); | |
269 jobType_ = IJobUnserializer::GetString(serialized, "Type"); | |
270 priority_ = IJobUnserializer::GetInteger(serialized, "Priority"); | |
271 creationTime_ = boost::posix_time::from_iso_string | |
272 (IJobUnserializer::GetString(serialized, "CreationTime")); | |
273 runtime_ = boost::posix_time::milliseconds(IJobUnserializer::GetInteger(serialized, "Runtime")); | |
274 | |
275 retryTime_ = creationTime_; | |
276 | |
277 if (state_ == JobState_Retry || | |
278 state_ == JobState_Running) | |
279 { | |
280 state_ = JobState_Pending; | |
281 } | |
282 | |
283 job_.reset(unserializer.UnserializeJob(jobType_, serialized["Job"])); | |
284 | |
285 std::string s; | |
286 job_->GetJobType(s); | |
287 if (s != jobType_) | |
288 { | |
289 throw OrthancException(ErrorCode_InternalError); | |
290 } | |
291 | |
292 job_->Start(); | |
293 lastStatus_ = JobStatus(ErrorCode_Success, *job_); | |
233 } | 294 } |
234 }; | 295 }; |
235 | 296 |
236 | 297 |
237 bool JobsRegistry::PriorityComparator::operator() (JobHandler*& a, | 298 bool JobsRegistry::PriorityComparator::operator() (JobHandler*& a, |
495 handler.GetLastStatus(), | 556 handler.GetLastStatus(), |
496 handler.GetCreationTime(), | 557 handler.GetCreationTime(), |
497 handler.GetLastStateChangeTime(), | 558 handler.GetLastStateChangeTime(), |
498 handler.GetRuntime()); | 559 handler.GetRuntime()); |
499 return true; | 560 return true; |
561 } | |
562 } | |
563 | |
564 | |
565 void JobsRegistry::Serialize(Json::Value& target) | |
566 { | |
567 boost::mutex::scoped_lock lock(mutex_); | |
568 CheckInvariants(); | |
569 | |
570 target = Json::objectValue; | |
571 | |
572 for (JobsIndex::const_iterator it = jobsIndex_.begin(); | |
573 it != jobsIndex_.end(); ++it) | |
574 { | |
575 Json::Value& v = target[it->first]; | |
576 it->second->Serialize(v); | |
500 } | 577 } |
501 } | 578 } |
502 | 579 |
503 | 580 |
504 void JobsRegistry::Submit(std::string& id, | 581 void JobsRegistry::Submit(std::string& id, |