diff Core/JobsEngine/JobsRegistry.cpp @ 4022:a2e4edc7b9aa more-changes

wip: adding job changes
author Alain Mazy <alain@mazy.be>
date Tue, 09 Jun 2020 08:46:52 +0200
parents 2a170a8f1faf
children 1d2b31fc782f
line wrap: on
line diff
--- a/Core/JobsEngine/JobsRegistry.cpp	Mon Jun 08 16:10:09 2020 +0200
+++ b/Core/JobsEngine/JobsRegistry.cpp	Tue Jun 09 08:46:52 2020 +0200
@@ -495,15 +495,20 @@
       job.SetLastErrorCode(ErrorCode_CanceledJob);
     }
 
-    if (observer_ != NULL)
     {
-      if (reason == CompletedReason_Success)
+      boost::shared_lock<boost::shared_mutex> lock(observersMutex_);
+
+      for (Observers::iterator it = observers_.begin(); it != observers_.end(); ++it)
       {
-        observer_->SignalJobSuccess(job.GetId());
-      }
-      else
-      {
-        observer_->SignalJobFailure(job.GetId());
+        if (reason == CompletedReason_Success)
+        {
+          (*it)->SignalJobSuccess(job.GetId());
+        }
+        else
+        {
+          (*it)->SignalJobFailure(job.GetId());
+        }
+
       }
     }
 
@@ -711,9 +716,13 @@
 
       LOG(INFO) << "New job submitted with priority " << priority << ": " << id;
 
-      if (observer_ != NULL)
       {
-        observer_->SignalJobSubmitted(id);
+        boost::shared_lock<boost::shared_mutex> lock(observersMutex_);
+
+        for (Observers::iterator it = observers_.begin(); it != observers_.end(); ++it)
+        {
+          (*it)->SignalJobSubmitted(id);
+        }
       }
 
       // WARNING: The following call might make "handler" invalid if
@@ -1100,17 +1109,21 @@
   }
 
 
-  void JobsRegistry::SetObserver(JobsRegistry::IObserver& observer)
+  void JobsRegistry::AddObserver(JobsRegistry::IObserver& observer)
   {
-    boost::mutex::scoped_lock lock(mutex_);
-    observer_ = &observer;
+    boost::unique_lock<boost::shared_mutex> lock(observersMutex_);
+    observers_.insert(&observer);
   }
 
   
-  void JobsRegistry::ResetObserver()
+  void JobsRegistry::ResetObserver(JobsRegistry::IObserver& observer)
   {
-    boost::mutex::scoped_lock lock(mutex_);
-    observer_ = NULL;
+    boost::unique_lock<boost::shared_mutex> lock(observersMutex_);
+    Observers::iterator it = observers_.find(&observer);
+    if (it != observers_.end())
+    {
+      observers_.erase(it);
+    }
   }
 
   
@@ -1381,8 +1394,7 @@
   JobsRegistry::JobsRegistry(IJobUnserializer& unserializer,
                              const Json::Value& s,
                              size_t maxCompletedJobs) :
-    maxCompletedJobs_(maxCompletedJobs),
-    observer_(NULL)
+    maxCompletedJobs_(maxCompletedJobs)
   {
     if (SerializationToolbox::ReadString(s, TYPE) != JOBS_REGISTRY ||
         !s.isMember(JOBS) ||