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_FOREACH (boost::signals2::connection& i, _connections) {
70 boost::mutex::scoped_lock lm (_mutex);
72 _empty_condition.notify_all ();
75 if (_scheduler.joinable()) {
85 JobManager::add (shared_ptr<Job> j)
88 boost::mutex::scoped_lock lm (_mutex);
90 _empty_condition.notify_all ();
93 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
99 JobManager::add_after (shared_ptr<Job> after, shared_ptr<Job> j)
102 boost::mutex::scoped_lock lm (_mutex);
103 list<shared_ptr<Job> >::iterator i = find (_jobs.begin(), _jobs.end(), after);
104 DCPOMATIC_ASSERT (i != _jobs.end());
106 _empty_condition.notify_all ();
109 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
114 list<shared_ptr<Job> >
115 JobManager::get () const
117 boost::mutex::scoped_lock lm (_mutex);
122 JobManager::work_to_do () const
124 boost::mutex::scoped_lock lm (_mutex);
125 list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
126 while (i != _jobs.end() && (*i)->finished()) {
130 return i != _jobs.end ();
134 JobManager::errors () const
136 boost::mutex::scoped_lock lm (_mutex);
137 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
138 if (i->finished_in_error ()) {
147 JobManager::scheduler ()
151 boost::mutex::scoped_lock lm (_mutex);
154 bool have_new = false;
155 bool have_running = false;
156 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
165 if ((!have_running && have_new) || _terminate) {
169 _empty_condition.wait (lm);
176 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
178 _connections.push_back (i->FinishedImmediate.connect(bind(&JobManager::job_finished, this)));
180 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, i->json_name()));
181 _last_active_job = i->json_name ();
182 /* Only start one job at once */
190 JobManager::job_finished ()
193 boost::mutex::scoped_lock lm (_mutex);
194 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, optional<string>()));
195 _last_active_job = optional<string>();
198 _empty_condition.notify_all ();
202 JobManager::instance ()
204 if (_instance == 0) {
205 _instance = new JobManager ();
220 JobManager::analyse_audio (
221 shared_ptr<const Film> film,
222 shared_ptr<const Playlist> playlist,
224 boost::signals2::connection& connection,
225 function<void()> ready
229 boost::mutex::scoped_lock lm (_mutex);
231 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
232 shared_ptr<AnalyseAudioJob> a = dynamic_pointer_cast<AnalyseAudioJob> (i);
233 if (a && a->path() == film->audio_analysis_path(playlist)) {
234 i->when_finished (connection, ready);
240 shared_ptr<AnalyseAudioJob> job;
243 boost::mutex::scoped_lock lm (_mutex);
245 job.reset (new AnalyseAudioJob (film, playlist, from_zero));
246 connection = job->Finished.connect (ready);
247 _jobs.push_back (job);
248 _empty_condition.notify_all ();
251 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
256 JobManager::analyse_subtitles (
257 shared_ptr<const Film> film,
258 shared_ptr<Content> content,
259 boost::signals2::connection& connection,
260 function<void()> ready
264 boost::mutex::scoped_lock lm (_mutex);
266 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
267 shared_ptr<AnalyseSubtitlesJob> a = dynamic_pointer_cast<AnalyseSubtitlesJob> (i);
268 if (a && a->path() == film->subtitle_analysis_path(content)) {
269 i->when_finished (connection, ready);
275 shared_ptr<AnalyseSubtitlesJob> job;
278 boost::mutex::scoped_lock lm (_mutex);
280 job.reset (new AnalyseSubtitlesJob(film, content));
281 connection = job->Finished.connect (ready);
282 _jobs.push_back (job);
283 _empty_condition.notify_all ();
286 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
291 JobManager::increase_priority (shared_ptr<Job> job)
293 bool changed = false;
296 boost::mutex::scoped_lock lm (_mutex);
297 list<shared_ptr<Job> >::iterator last = _jobs.end ();
298 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
299 if (*i == job && last != _jobs.end()) {
314 JobManager::priority_changed ()
317 boost::mutex::scoped_lock lm (_mutex);
320 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
324 } else if (i->paused_by_priority ()) {
330 i->pause_by_priority ();
336 emit (boost::bind (boost::ref (JobsReordered)));
340 JobManager::decrease_priority (shared_ptr<Job> job)
342 bool changed = false;
345 boost::mutex::scoped_lock lm (_mutex);
346 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
347 list<shared_ptr<Job> >::iterator next = i;
349 if (*i == job && next != _jobs.end()) {
365 boost::mutex::scoped_lock lm (_mutex);
371 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
372 if (i->pause_by_user()) {
381 JobManager::resume ()
383 boost::mutex::scoped_lock lm (_mutex);
389 _paused_job->resume ();
392 _paused_job.reset ();