2 Copyright (C) 2012-2020 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"
29 #include "analyse_subtitles_job.h"
31 #include <boost/thread.hpp>
32 #include <boost/foreach.hpp>
38 using boost::shared_ptr;
39 using boost::weak_ptr;
40 using boost::function;
41 using boost::dynamic_pointer_cast;
42 using boost::optional;
45 JobManager* JobManager::_instance = 0;
47 JobManager::JobManager ()
57 _scheduler = boost::thread (boost::bind(&JobManager::scheduler, this));
58 #ifdef DCPOMATIC_LINUX
59 pthread_setname_np (_scheduler.native_handle(), "job-scheduler");
63 JobManager::~JobManager ()
65 boost::this_thread::disable_interruption dis;
67 BOOST_FOREACH (boost::signals2::connection& i, _connections) {
72 boost::mutex::scoped_lock lm (_mutex);
74 _empty_condition.notify_all ();
83 JobManager::add (shared_ptr<Job> j)
86 boost::mutex::scoped_lock lm (_mutex);
88 _empty_condition.notify_all ();
91 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
97 JobManager::add_after (shared_ptr<Job> after, shared_ptr<Job> j)
100 boost::mutex::scoped_lock lm (_mutex);
101 list<shared_ptr<Job> >::iterator i = find (_jobs.begin(), _jobs.end(), after);
102 DCPOMATIC_ASSERT (i != _jobs.end());
104 _empty_condition.notify_all ();
107 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
112 list<shared_ptr<Job> >
113 JobManager::get () const
115 boost::mutex::scoped_lock lm (_mutex);
120 JobManager::work_to_do () const
122 boost::mutex::scoped_lock lm (_mutex);
123 list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
124 while (i != _jobs.end() && (*i)->finished()) {
128 return i != _jobs.end ();
132 JobManager::errors () const
134 boost::mutex::scoped_lock lm (_mutex);
135 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
136 if (i->finished_in_error ()) {
145 JobManager::scheduler ()
149 boost::mutex::scoped_lock lm (_mutex);
152 bool have_new = false;
153 bool have_running = false;
154 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
163 if ((!have_running && have_new) || _terminate) {
167 _empty_condition.wait (lm);
174 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
176 _connections.push_back (i->FinishedImmediate.connect(bind(&JobManager::job_finished, this)));
178 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, i->json_name()));
179 _last_active_job = i->json_name ();
180 /* Only start one job at once */
188 JobManager::job_finished ()
191 boost::mutex::scoped_lock lm (_mutex);
192 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, optional<string>()));
193 _last_active_job = optional<string>();
196 _empty_condition.notify_all ();
200 JobManager::instance ()
202 if (_instance == 0) {
203 _instance = new JobManager ();
218 JobManager::analyse_audio (
219 shared_ptr<const Film> film,
220 shared_ptr<const Playlist> playlist,
222 boost::signals2::connection& connection,
223 function<void()> ready
227 boost::mutex::scoped_lock lm (_mutex);
229 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
230 shared_ptr<AnalyseAudioJob> a = dynamic_pointer_cast<AnalyseAudioJob> (i);
231 if (a && a->path() == film->audio_analysis_path(playlist) && !i->finished_cancelled()) {
232 i->when_finished (connection, ready);
238 shared_ptr<AnalyseAudioJob> job;
241 boost::mutex::scoped_lock lm (_mutex);
243 job.reset (new AnalyseAudioJob (film, playlist, from_zero));
244 connection = job->Finished.connect (ready);
245 _jobs.push_back (job);
246 _empty_condition.notify_all ();
249 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
254 JobManager::analyse_subtitles (
255 shared_ptr<const Film> film,
256 shared_ptr<Content> content,
257 boost::signals2::connection& connection,
258 function<void()> ready
262 boost::mutex::scoped_lock lm (_mutex);
264 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
265 shared_ptr<AnalyseSubtitlesJob> a = dynamic_pointer_cast<AnalyseSubtitlesJob> (i);
266 if (a && a->path() == film->subtitle_analysis_path(content)) {
267 i->when_finished (connection, ready);
273 shared_ptr<AnalyseSubtitlesJob> job;
276 boost::mutex::scoped_lock lm (_mutex);
278 job.reset (new AnalyseSubtitlesJob(film, content));
279 connection = job->Finished.connect (ready);
280 _jobs.push_back (job);
281 _empty_condition.notify_all ();
284 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
289 JobManager::increase_priority (shared_ptr<Job> job)
291 bool changed = false;
294 boost::mutex::scoped_lock lm (_mutex);
295 list<shared_ptr<Job> >::iterator last = _jobs.end ();
296 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
297 if (*i == job && last != _jobs.end()) {
312 JobManager::priority_changed ()
315 boost::mutex::scoped_lock lm (_mutex);
318 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
322 } else if (i->paused_by_priority ()) {
328 i->pause_by_priority ();
334 emit (boost::bind (boost::ref (JobsReordered)));
338 JobManager::decrease_priority (shared_ptr<Job> job)
340 bool changed = false;
343 boost::mutex::scoped_lock lm (_mutex);
344 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
345 list<shared_ptr<Job> >::iterator next = i;
347 if (*i == job && next != _jobs.end()) {
363 boost::mutex::scoped_lock lm (_mutex);
369 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
370 if (i->pause_by_user()) {
379 JobManager::resume ()
381 boost::mutex::scoped_lock lm (_mutex);
387 _paused_job->resume ();
390 _paused_job.reset ();