Mercurial > hg > orthanc
comparison UnitTestsSources/MultiThreadingTests.cpp @ 2556:91e944c8389b jobs
IJob
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 03 May 2018 10:27:39 +0200 |
parents | 878b59270859 |
children | b4516a6f214b |
comparison
equal
deleted
inserted
replaced
2555:50d80af0719e | 2556:91e944c8389b |
---|---|
257 if (t.joinable()) | 257 if (t.joinable()) |
258 { | 258 { |
259 t.join(); | 259 t.join(); |
260 } | 260 } |
261 } | 261 } |
262 | |
263 | |
264 | |
265 | |
266 | |
267 #if !defined(ORTHANC_SANDBOXED) | |
268 # error The macro ORTHANC_SANDBOXED must be defined | |
269 #endif | |
270 | |
271 #if ORTHANC_SANDBOXED == 1 | |
272 # error The job engine cannot be used in sandboxed environments | |
273 #endif | |
274 | |
275 #include <boost/date_time/posix_time/posix_time.hpp> | |
276 | |
277 namespace Orthanc | |
278 { | |
279 enum JobState | |
280 { | |
281 JobState_Pending, | |
282 JobState_Running, | |
283 JobState_Success, | |
284 JobState_Failure, | |
285 JobState_Paused, | |
286 JobState_Retry | |
287 }; | |
288 | |
289 enum JobStepStatus | |
290 { | |
291 JobStepStatus_Success, | |
292 JobStepStatus_Error, | |
293 JobStepStatus_Continue, | |
294 JobStepStatus_Retry | |
295 }; | |
296 | |
297 | |
298 class IJobStepResult : public boost::noncopyable | |
299 { | |
300 private: | |
301 JobStepStatus status_; | |
302 | |
303 public: | |
304 explicit IJobStepResult(JobStepStatus status) : | |
305 status_(status) | |
306 { | |
307 } | |
308 | |
309 virtual ~IJobStepResult() | |
310 { | |
311 } | |
312 | |
313 JobStepStatus GetStatus() const | |
314 { | |
315 return status_; | |
316 } | |
317 }; | |
318 | |
319 | |
320 class RetryResult : public IJobStepResult | |
321 { | |
322 private: | |
323 unsigned int timeout_; // Retry after "timeout_" milliseconds | |
324 | |
325 public: | |
326 RetryResult(unsigned int timeout) : | |
327 IJobStepResult(JobStepStatus_Retry), | |
328 timeout_(timeout) | |
329 { | |
330 } | |
331 | |
332 unsigned int GetTimeout() const | |
333 { | |
334 return timeout_; | |
335 } | |
336 }; | |
337 | |
338 | |
339 class IJob : public boost::noncopyable | |
340 { | |
341 public: | |
342 virtual ~IJob() | |
343 { | |
344 } | |
345 | |
346 virtual IJobStepResult* ExecuteStep() = 0; | |
347 | |
348 virtual void ReleaseResources() = 0; // For pausing jobs | |
349 | |
350 virtual float GetProgress() = 0; | |
351 | |
352 virtual void FormatStatus(Json::Value& value) = 0; | |
353 }; | |
354 | |
355 | |
356 class JobsMonitor : public boost::noncopyable | |
357 { | |
358 private: | |
359 class JobHandler : public boost::noncopyable | |
360 { | |
361 private: | |
362 std::string id_; | |
363 JobState state_; | |
364 std::auto_ptr<IJob> job_; | |
365 int priority_; // "+inf()" means highest priority | |
366 boost::posix_time::ptime creationTime_; | |
367 boost::posix_time::ptime lastUpdateTime_; | |
368 uint64_t runtime_; // In milliseconds | |
369 | |
370 public: | |
371 JobHandler(IJob* job, | |
372 int priority) : | |
373 id_(Toolbox::GenerateUuid()), | |
374 state_(JobState_Pending), | |
375 job_(job), | |
376 priority_(priority), | |
377 creationTime_(boost::posix_time::microsec_clock::universal_time()), | |
378 lastUpdateTime_(creationTime_), | |
379 runtime_(0) | |
380 { | |
381 if (job == NULL) | |
382 { | |
383 throw OrthancException(ErrorCode_NullPointer); | |
384 } | |
385 } | |
386 | |
387 const std::string& GetId() const | |
388 { | |
389 return id_; | |
390 } | |
391 }; | |
392 | |
393 public: | |
394 void Submit(std::string& id, | |
395 IJob* job, | |
396 int priority) | |
397 { | |
398 std::auto_ptr<JobHandler> handler(new JobHandler(job, priority)); | |
399 id = handler->GetId(); | |
400 } | |
401 | |
402 void SetPriority(const std::string& id, | |
403 int priority) | |
404 { | |
405 // TODO | |
406 } | |
407 | |
408 void Pause(const std::string& id) | |
409 { | |
410 // TODO | |
411 } | |
412 | |
413 void Resume(const std::string& id) | |
414 { | |
415 // TODO | |
416 } | |
417 | |
418 void Resubmit(const std::string& id) | |
419 { | |
420 // TODO | |
421 } | |
422 | |
423 class JobToRun : public boost::noncopyable | |
424 { | |
425 private: | |
426 JobHandler* handler_; | |
427 | |
428 public: | |
429 JobToRun(JobsMonitor& that, | |
430 unsigned int timeout) : | |
431 handler_(NULL) | |
432 { | |
433 } | |
434 | |
435 bool IsValid() const | |
436 { | |
437 return handler_ != NULL; | |
438 } | |
439 | |
440 | |
441 }; | |
442 }; | |
443 } |