diff Core/JobsEngine/JobsRegistry.cpp @ 2673:8e0bc055d18c jobs

JobsRegistry::IObserver
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 11 Jun 2018 16:29:33 +0200
parents c5646f766b3e
children ea7aea6f6a95
line wrap: on
line diff
--- a/Core/JobsEngine/JobsRegistry.cpp	Mon Jun 11 15:57:25 2018 +0200
+++ b/Core/JobsEngine/JobsRegistry.cpp	Mon Jun 11 16:29:33 2018 +0200
@@ -292,7 +292,7 @@
       }
       else
       {
-        LOG(WARNING) << "Job backup is not supported for job of type: " << jobType_;
+        LOG(INFO) << "Job backup is not supported for job of type: " << jobType_;
         return false;
       }
     }
@@ -475,6 +475,18 @@
     assert(job.GetState() == JobState_Running);
     SetCompletedJob(job, success);
 
+    if (observer_ != NULL)
+    {
+      if (success)
+      {
+        observer_->SignalJobSuccess(job.GetId());
+      }
+      else
+      {
+        observer_->SignalJobFailure(job.GetId());
+      }
+    }
+
     CheckInvariants();
   }
 
@@ -604,49 +616,56 @@
 
     boost::posix_time::ptime lastChangeTime = handler->GetLastStateChangeTime();
 
-    boost::mutex::scoped_lock lock(mutex_);
-    CheckInvariants();
+    {
+      boost::mutex::scoped_lock lock(mutex_);
+      CheckInvariants();
       
-    id = handler->GetId();
-    int priority = handler->GetPriority();
+      id = handler->GetId();
+      int priority = handler->GetPriority();
 
-    switch (handler->GetState())
-    {
-      case JobState_Pending:
-      case JobState_Retry:
-      case JobState_Running:
-        handler->SetState(JobState_Pending);
-        pendingJobs_.push(handler.get());
-        pendingJobAvailable_.notify_one();
-        break;
+      switch (handler->GetState())
+      {
+        case JobState_Pending:
+        case JobState_Retry:
+        case JobState_Running:
+          handler->SetState(JobState_Pending);
+          pendingJobs_.push(handler.get());
+          pendingJobAvailable_.notify_one();
+          break;
  
-      case JobState_Success:
-        SetCompletedJob(*handler, true);
-        break;
+        case JobState_Success:
+          SetCompletedJob(*handler, true);
+          break;
         
-      case JobState_Failure:
-        SetCompletedJob(*handler, false);
-        break;
+        case JobState_Failure:
+          SetCompletedJob(*handler, false);
+          break;
+
+        case JobState_Paused:
+          break;
+        
+        default:
+          LOG(ERROR) << "A job should not be loaded from state: "
+                     << EnumerationToString(handler->GetState());
+          throw OrthancException(ErrorCode_InternalError);
+      }
 
-      case JobState_Paused:
-        break;
-        
-      default:
-        LOG(ERROR) << "A job should not be loaded from state: "
-                   << EnumerationToString(handler->GetState());
-        throw OrthancException(ErrorCode_InternalError);
+      if (keepLastChangeTime)
+      {
+        handler->SetLastStateChangeTime(lastChangeTime);
+      }
+    
+      jobsIndex_.insert(std::make_pair(id, handler.release()));
+
+      LOG(INFO) << "New job submitted with priority " << priority << ": " << id;
+
+      if (observer_ != NULL)
+      {
+        observer_->SignalJobSubmitted(id);
+      }
+
+      CheckInvariants();
     }
-
-    if (keepLastChangeTime)
-    {
-      handler->SetLastStateChangeTime(lastChangeTime);
-    }
-    
-    jobsIndex_.insert(std::make_pair(id, handler.release()));
-
-    LOG(INFO) << "New job submitted with priority " << priority << ": " << id;
-
-    CheckInvariants();
   }
 
 
@@ -974,6 +993,20 @@
     return GetStateInternal(state, id);
   }
 
+
+  void JobsRegistry::SetObserver(JobsRegistry::IObserver& observer)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+    observer_ = &observer;
+  }
+
+  
+  void JobsRegistry::ResetObserver()
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+    observer_ = NULL;
+  }
+
   
   JobsRegistry::RunningJob::RunningJob(JobsRegistry& registry,
                                        unsigned int timeout) :
@@ -1245,7 +1278,8 @@
 
 
   JobsRegistry::JobsRegistry(IJobUnserializer& unserializer,
-                             const Json::Value& s)
+                             const Json::Value& s) :
+    observer_(NULL)
   {
     if (SerializationToolbox::ReadString(s, TYPE) != JOBS_REGISTRY ||
         !s.isMember(JOBS) ||