Try to not start jobs if a dependant fails.
[dcpomatic.git] / src / lib / job_manager.cc
index dd7c62c318a499b68ab1c8a2734115620097882e..562c887de9b57479c75f702928ac2ae6aec5d313 100644 (file)
@@ -25,6 +25,7 @@
 #include <boost/thread.hpp>
 #include "job_manager.h"
 #include "job.h"
+#include "cross.h"
 
 using namespace std;
 using namespace boost;
@@ -36,19 +37,28 @@ JobManager::JobManager ()
        boost::thread (boost::bind (&JobManager::scheduler, this));
 }
 
-void
+shared_ptr<Job>
 JobManager::add (shared_ptr<Job> j)
 {
        boost::mutex::scoped_lock lm (_mutex);
-       
        _jobs.push_back (j);
+       return j;
+}
+
+void
+JobManager::add_after (shared_ptr<Job> after, shared_ptr<Job> j)
+{
+       boost::mutex::scoped_lock lm (_mutex);
+       list<shared_ptr<Job> >::iterator i = find (_jobs.begin(), _jobs.end(), after);
+       assert (i != _jobs.end ());
+       ++i;
+       _jobs.insert (i, j);
 }
 
 list<shared_ptr<Job> >
 JobManager::get () const
 {
        boost::mutex::scoped_lock lm (_mutex);
-       
        return _jobs;
 }
 
@@ -64,29 +74,40 @@ JobManager::work_to_do () const
        return i != _jobs.end ();
 }
 
+bool
+JobManager::errors () const
+{
+       boost::mutex::scoped_lock lm (_mutex);
+       for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
+               if ((*i)->finished_in_error ()) {
+                       return true;
+               }
+       }
+
+       return false;
+}      
+
+
 void
 JobManager::scheduler ()
 {
        while (1) {
                {
                        boost::mutex::scoped_lock lm (_mutex);
-                       int running = 0;
-                       shared_ptr<Job> first_new;
                        for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
-                               if ((*i)->running ()) {
-                                       ++running;
-                               } else if (!(*i)->finished () && first_new == 0) {
-                                       first_new = *i;
-                               }
-
-                               if (running == 0 && first_new) {
-                                       first_new->start ();
-                                       break;
+                               if ((*i)->is_new()) {
+                                       shared_ptr<Job> r = (*i)->required ();
+                                       if (!r || r->finished_ok ()) {
+                                               (*i)->start ();
+
+                                               /* Only start one job at once */
+                                               break;
+                                       }
                                }
                        }
                }
 
-               sleep (1);
+               dvdomatic_sleep (1);
        }
 }