2 Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 /** @file src/job_manager.cc
22 * @brief A simple scheduler for jobs.
25 #include "job_manager.h"
28 #include "analyse_audio_job.h"
30 #include <boost/thread.hpp>
31 #include <boost/foreach.hpp>
37 using boost::shared_ptr;
38 using boost::weak_ptr;
39 using boost::function;
40 using boost::dynamic_pointer_cast;
41 using boost::optional;
44 JobManager* JobManager::_instance = 0;
46 JobManager::JobManager ()
56 _scheduler = boost::thread (boost::bind(&JobManager::scheduler, this));
57 #ifdef DCPOMATIC_LINUX
58 pthread_setname_np (_scheduler.native_handle(), "job-scheduler");
62 JobManager::~JobManager ()
64 BOOST_FOREACH (boost::signals2::connection& i, _connections) {
69 boost::mutex::scoped_lock lm (_mutex);
71 _empty_condition.notify_all ();
74 if (_scheduler.joinable()) {
84 JobManager::add (shared_ptr<Job> j)
87 boost::mutex::scoped_lock lm (_mutex);
89 _empty_condition.notify_all ();
92 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
98 JobManager::add_after (shared_ptr<Job> after, shared_ptr<Job> j)
101 boost::mutex::scoped_lock lm (_mutex);
102 list<shared_ptr<Job> >::iterator i = find (_jobs.begin(), _jobs.end(), after);
103 DCPOMATIC_ASSERT (i != _jobs.end());
105 _empty_condition.notify_all ();
108 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
113 list<shared_ptr<Job> >
114 JobManager::get () const
116 boost::mutex::scoped_lock lm (_mutex);
121 JobManager::work_to_do () const
123 boost::mutex::scoped_lock lm (_mutex);
124 list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
125 while (i != _jobs.end() && (*i)->finished()) {
129 return i != _jobs.end ();
133 JobManager::errors () const
135 boost::mutex::scoped_lock lm (_mutex);
136 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
137 if (i->finished_in_error ()) {
146 JobManager::scheduler ()
150 boost::mutex::scoped_lock lm (_mutex);
153 bool have_new = false;
154 bool have_running = false;
155 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
164 if ((!have_running && have_new) || _terminate) {
168 _empty_condition.wait (lm);
175 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
177 _connections.push_back (i->FinishedImmediate.connect(bind(&JobManager::job_finished, this)));
179 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, i->json_name()));
180 _last_active_job = i->json_name ();
181 /* Only start one job at once */
189 JobManager::job_finished ()
192 boost::mutex::scoped_lock lm (_mutex);
193 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, optional<string>()));
194 _last_active_job = optional<string>();
197 _empty_condition.notify_all ();
201 JobManager::instance ()
203 if (_instance == 0) {
204 _instance = new JobManager ();
219 JobManager::analyse_audio (
220 shared_ptr<const Film> film,
221 shared_ptr<const Playlist> playlist,
223 boost::signals2::connection& connection,
224 function<void()> ready
228 boost::mutex::scoped_lock lm (_mutex);
230 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
231 shared_ptr<AnalyseAudioJob> a = dynamic_pointer_cast<AnalyseAudioJob> (i);
232 if (a && a->path() == film->audio_analysis_path(playlist)) {
233 i->when_finished (connection, ready);
239 shared_ptr<AnalyseAudioJob> job;
242 boost::mutex::scoped_lock lm (_mutex);
244 job.reset (new AnalyseAudioJob (film, playlist, from_zero));
245 connection = job->Finished.connect (ready);
246 _jobs.push_back (job);
247 _empty_condition.notify_all ();
250 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
254 JobManager::increase_priority (shared_ptr<Job> job)
256 bool changed = false;
259 boost::mutex::scoped_lock lm (_mutex);
260 list<shared_ptr<Job> >::iterator last = _jobs.end ();
261 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
262 if (*i == job && last != _jobs.end()) {
277 JobManager::priority_changed ()
280 boost::mutex::scoped_lock lm (_mutex);
283 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
287 } else if (i->paused_by_priority ()) {
293 i->pause_by_priority ();
299 emit (boost::bind (boost::ref (JobsReordered)));
303 JobManager::decrease_priority (shared_ptr<Job> job)
305 bool changed = false;
308 boost::mutex::scoped_lock lm (_mutex);
309 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
310 list<shared_ptr<Job> >::iterator next = i;
312 if (*i == job && next != _jobs.end()) {
328 boost::mutex::scoped_lock lm (_mutex);
334 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
335 if (i->pause_by_user()) {
344 JobManager::resume ()
346 boost::mutex::scoped_lock lm (_mutex);
352 _paused_job->resume ();
355 _paused_job.reset ();