comparison Core/JobsEngine/JobsRegistry.cpp @ 2669:eaf10085ffa1 jobs

no passwords in public content of jobs
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 09 Jun 2018 14:15:32 +0200
parents d26dd081df97
children c5646f766b3e
comparison
equal deleted inserted replaced
2668:d26dd081df97 2669:eaf10085ffa1
48 static const char* JOB = "Job"; 48 static const char* JOB = "Job";
49 static const char* JOBS = "Jobs"; 49 static const char* JOBS = "Jobs";
50 static const char* JOBS_REGISTRY = "JobsRegistry"; 50 static const char* JOBS_REGISTRY = "JobsRegistry";
51 static const char* MAX_COMPLETED_JOBS = "MaxCompletedJobs"; 51 static const char* MAX_COMPLETED_JOBS = "MaxCompletedJobs";
52 static const char* CREATION_TIME = "CreationTime"; 52 static const char* CREATION_TIME = "CreationTime";
53 static const char* LAST_CHANGE_TIME = "LastChangeTime";
53 static const char* RUNTIME = "Runtime"; 54 static const char* RUNTIME = "Runtime";
54 55
55 56
56 class JobsRegistry::JobHandler : public boost::noncopyable 57 class JobsRegistry::JobHandler : public boost::noncopyable
57 { 58 {
224 const boost::posix_time::ptime& GetLastStateChangeTime() const 225 const boost::posix_time::ptime& GetLastStateChangeTime() const
225 { 226 {
226 return lastStateChangeTime_; 227 return lastStateChangeTime_;
227 } 228 }
228 229
230 void SetLastStateChangeTime(const boost::posix_time::ptime& time)
231 {
232 lastStateChangeTime_ = time;
233 }
234
229 const boost::posix_time::time_duration& GetRuntime() const 235 const boost::posix_time::time_duration& GetRuntime() const
230 { 236 {
231 return runtime_; 237 return runtime_;
232 } 238 }
233 239
280 { 286 {
281 target[ID] = id_; 287 target[ID] = id_;
282 target[STATE] = EnumerationToString(state_); 288 target[STATE] = EnumerationToString(state_);
283 target[PRIORITY] = priority_; 289 target[PRIORITY] = priority_;
284 target[CREATION_TIME] = boost::posix_time::to_iso_string(creationTime_); 290 target[CREATION_TIME] = boost::posix_time::to_iso_string(creationTime_);
291 target[LAST_CHANGE_TIME] = boost::posix_time::to_iso_string(lastStateChangeTime_);
285 target[RUNTIME] = static_cast<unsigned int>(runtime_.total_milliseconds()); 292 target[RUNTIME] = static_cast<unsigned int>(runtime_.total_milliseconds());
286 return true; 293 return true;
287 } 294 }
288 else 295 else
289 { 296 {
292 } 299 }
293 } 300 }
294 301
295 JobHandler(IJobUnserializer& unserializer, 302 JobHandler(IJobUnserializer& unserializer,
296 const Json::Value& serialized) : 303 const Json::Value& serialized) :
297 lastStateChangeTime_(boost::posix_time::microsec_clock::universal_time()),
298 pauseScheduled_(false), 304 pauseScheduled_(false),
299 cancelScheduled_(false) 305 cancelScheduled_(false)
300 { 306 {
301 id_ = SerializationToolbox::ReadString(serialized, ID); 307 id_ = SerializationToolbox::ReadString(serialized, ID);
302 state_ = StringToJobState(SerializationToolbox::ReadString(serialized, STATE)); 308 state_ = StringToJobState(SerializationToolbox::ReadString(serialized, STATE));
303 priority_ = SerializationToolbox::ReadInteger(serialized, PRIORITY); 309 priority_ = SerializationToolbox::ReadInteger(serialized, PRIORITY);
304 creationTime_ = boost::posix_time::from_iso_string 310 creationTime_ = boost::posix_time::from_iso_string
305 (SerializationToolbox::ReadString(serialized, CREATION_TIME)); 311 (SerializationToolbox::ReadString(serialized, CREATION_TIME));
312 lastStateChangeTime_ = boost::posix_time::from_iso_string
313 (SerializationToolbox::ReadString(serialized, LAST_CHANGE_TIME));
306 runtime_ = boost::posix_time::milliseconds 314 runtime_ = boost::posix_time::milliseconds
307 (SerializationToolbox::ReadInteger(serialized, RUNTIME)); 315 (SerializationToolbox::ReadInteger(serialized, RUNTIME));
308 316
309 retryTime_ = creationTime_; 317 retryTime_ = creationTime_;
310
311 if (state_ == JobState_Retry ||
312 state_ == JobState_Running)
313 {
314 state_ = JobState_Pending;
315 }
316 318
317 job_.reset(unserializer.UnserializeJob(serialized[JOB])); 319 job_.reset(unserializer.UnserializeJob(serialized[JOB]));
318 job_->GetJobType(jobType_); 320 job_->GetJobType(jobType_);
319 job_->Start(); 321 job_->Start();
320 322
589 } 591 }
590 } 592 }
591 593
592 594
593 void JobsRegistry::SubmitInternal(std::string& id, 595 void JobsRegistry::SubmitInternal(std::string& id,
594 JobHandler* handlerRaw) 596 JobHandler* handlerRaw,
597 bool keepLastChangeTime)
595 { 598 {
596 if (handlerRaw == NULL) 599 if (handlerRaw == NULL)
597 { 600 {
598 throw OrthancException(ErrorCode_NullPointer); 601 throw OrthancException(ErrorCode_NullPointer);
599 } 602 }
600 603
601 std::auto_ptr<JobHandler> handler(handlerRaw); 604 std::auto_ptr<JobHandler> handler(handlerRaw);
605
606 boost::posix_time::ptime lastChangeTime = handler->GetLastStateChangeTime();
602 607
603 boost::mutex::scoped_lock lock(mutex_); 608 boost::mutex::scoped_lock lock(mutex_);
604 CheckInvariants(); 609 CheckInvariants();
605 610
606 id = handler->GetId(); 611 id = handler->GetId();
631 LOG(ERROR) << "A job should not be loaded from state: " 636 LOG(ERROR) << "A job should not be loaded from state: "
632 << EnumerationToString(handler->GetState()); 637 << EnumerationToString(handler->GetState());
633 throw OrthancException(ErrorCode_InternalError); 638 throw OrthancException(ErrorCode_InternalError);
634 } 639 }
635 640
641 if (keepLastChangeTime)
642 {
643 handler->SetLastStateChangeTime(lastChangeTime);
644 }
645
636 jobsIndex_.insert(std::make_pair(id, handler.release())); 646 jobsIndex_.insert(std::make_pair(id, handler.release()));
637 647
638 LOG(INFO) << "New job submitted with priority " << priority << ": " << id; 648 LOG(INFO) << "New job submitted with priority " << priority << ": " << id;
639 649
640 CheckInvariants(); 650 CheckInvariants();
643 653
644 void JobsRegistry::Submit(std::string& id, 654 void JobsRegistry::Submit(std::string& id,
645 IJob* job, // Takes ownership 655 IJob* job, // Takes ownership
646 int priority) 656 int priority)
647 { 657 {
648 SubmitInternal(id, new JobHandler(job, priority)); 658 SubmitInternal(id, new JobHandler(job, priority), false);
649 } 659 }
650 660
651 661
652 void JobsRegistry::Submit(IJob* job, // Takes ownership 662 void JobsRegistry::Submit(IJob* job, // Takes ownership
653 int priority) 663 int priority)
654 { 664 {
655 std::string id; 665 std::string id;
656 SubmitInternal(id, new JobHandler(job, priority)); 666 SubmitInternal(id, new JobHandler(job, priority), false);
657 } 667 }
658 668
659 669
660 bool JobsRegistry::SubmitAndWait(IJob* job, // Takes ownership 670 bool JobsRegistry::SubmitAndWait(IJob* job, // Takes ownership
661 int priority) 671 int priority)
1248 maxCompletedJobs_ = SerializationToolbox::ReadUnsignedInteger(s, MAX_COMPLETED_JOBS); 1258 maxCompletedJobs_ = SerializationToolbox::ReadUnsignedInteger(s, MAX_COMPLETED_JOBS);
1249 1259
1250 for (Json::Value::ArrayIndex i = 0; i < s[JOBS].size(); i++) 1260 for (Json::Value::ArrayIndex i = 0; i < s[JOBS].size(); i++)
1251 { 1261 {
1252 std::auto_ptr<JobHandler> job(new JobHandler(unserializer, s[JOBS][i])); 1262 std::auto_ptr<JobHandler> job(new JobHandler(unserializer, s[JOBS][i]));
1253 1263
1254 std::string id; 1264 std::string id;
1255 SubmitInternal(id, job.release()); 1265 SubmitInternal(id, job.release(), true);
1256 } 1266 }
1257 } 1267 }
1258 } 1268 }