2 Copyright (C) 2012-2021 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/>.
22 /** @file src/job_manager.cc
23 * @brief A simple scheduler for jobs.
27 #include "analyse_audio_job.h"
28 #include "analyse_subtitles_job.h"
32 #include "job_manager.h"
33 #include <boost/thread.hpp>
36 using std::dynamic_pointer_cast;
38 using std::make_shared;
39 using std::shared_ptr;
43 using boost::function;
44 using boost::optional;
47 JobManager* JobManager::_instance = nullptr;
50 JobManager::JobManager ()
59 _scheduler = boost::thread (boost::bind(&JobManager::scheduler, this));
60 #ifdef DCPOMATIC_LINUX
61 pthread_setname_np (_scheduler.native_handle(), "job-scheduler");
66 JobManager::~JobManager ()
68 boost::this_thread::disable_interruption dis;
70 for (auto& i: _connections) {
75 boost::mutex::scoped_lock lm (_mutex);
77 _empty_condition.notify_all ();
87 JobManager::add (shared_ptr<Job> j)
90 boost::mutex::scoped_lock lm (_mutex);
92 _empty_condition.notify_all ();
95 emit (boost::bind(boost::ref(JobAdded), weak_ptr<Job>(j)));
102 JobManager::add_after (shared_ptr<Job> after, shared_ptr<Job> j)
105 boost::mutex::scoped_lock lm (_mutex);
106 auto i = find (_jobs.begin(), _jobs.end(), after);
107 DCPOMATIC_ASSERT (i != _jobs.end());
109 _empty_condition.notify_all ();
112 emit (boost::bind(boost::ref(JobAdded), weak_ptr<Job>(j)));
118 list<shared_ptr<Job>>
119 JobManager::get () const
121 boost::mutex::scoped_lock lm (_mutex);
127 JobManager::work_to_do () const
129 boost::mutex::scoped_lock lm (_mutex);
130 auto i = _jobs.begin();
131 while (i != _jobs.end() && (*i)->finished()) {
135 return i != _jobs.end ();
140 JobManager::errors () const
142 boost::mutex::scoped_lock lm (_mutex);
143 for (auto i: _jobs) {
144 if (i->finished_in_error()) {
154 JobManager::scheduler ()
158 boost::mutex::scoped_lock lm (_mutex);
161 bool have_new = false;
162 bool have_running = false;
163 for (auto i: _jobs) {
172 if ((!have_running && have_new) || _terminate) {
176 _empty_condition.wait (lm);
183 for (auto i: _jobs) {
185 _connections.push_back (i->FinishedImmediate.connect(bind(&JobManager::job_finished, this)));
187 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, i->json_name()));
188 _last_active_job = i->json_name ();
189 /* Only start one job at once */
198 JobManager::job_finished ()
201 boost::mutex::scoped_lock lm (_mutex);
202 emit (boost::bind(boost::ref (ActiveJobsChanged), _last_active_job, optional<string>()));
203 _last_active_job = optional<string>();
206 _empty_condition.notify_all ();
211 JobManager::instance ()
214 _instance = new JobManager ();
231 JobManager::analyse_audio (
232 shared_ptr<const Film> film,
233 shared_ptr<const Playlist> playlist,
235 boost::signals2::connection& connection,
236 function<void()> ready
240 boost::mutex::scoped_lock lm (_mutex);
242 for (auto i: _jobs) {
243 auto a = dynamic_pointer_cast<AnalyseAudioJob> (i);
244 if (a && a->path() == film->audio_analysis_path(playlist) && !i->finished_cancelled()) {
245 i->when_finished (connection, ready);
251 shared_ptr<AnalyseAudioJob> job;
254 boost::mutex::scoped_lock lm (_mutex);
256 job = make_shared<AnalyseAudioJob> (film, playlist, from_zero);
257 connection = job->Finished.connect (ready);
258 _jobs.push_back (job);
259 _empty_condition.notify_all ();
262 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
267 JobManager::analyse_subtitles (
268 shared_ptr<const Film> film,
269 shared_ptr<Content> content,
270 boost::signals2::connection& connection,
271 function<void()> ready
275 boost::mutex::scoped_lock lm (_mutex);
277 for (auto i: _jobs) {
278 auto a = dynamic_pointer_cast<AnalyseSubtitlesJob> (i);
279 if (a && a->path() == film->subtitle_analysis_path(content)) {
280 i->when_finished (connection, ready);
286 shared_ptr<AnalyseSubtitlesJob> job;
289 boost::mutex::scoped_lock lm (_mutex);
291 job = make_shared<AnalyseSubtitlesJob>(film, content);
292 connection = job->Finished.connect (ready);
293 _jobs.push_back (job);
294 _empty_condition.notify_all ();
297 emit (boost::bind(boost::ref(JobAdded), weak_ptr<Job>(job)));
302 JobManager::increase_priority (shared_ptr<Job> job)
304 bool changed = false;
307 boost::mutex::scoped_lock lm (_mutex);
308 auto last = _jobs.end ();
309 for (auto i = _jobs.begin(); i != _jobs.end(); ++i) {
310 if (*i == job && last != _jobs.end()) {
326 JobManager::priority_changed ()
329 boost::mutex::scoped_lock lm (_mutex);
332 for (auto i: _jobs) {
336 } else if (i->paused_by_priority ()) {
342 i->pause_by_priority ();
348 emit (boost::bind(boost::ref(JobsReordered)));
353 JobManager::decrease_priority (shared_ptr<Job> job)
355 bool changed = false;
358 boost::mutex::scoped_lock lm (_mutex);
359 for (auto i = _jobs.begin(); i != _jobs.end(); ++i) {
362 if (*i == job && next != _jobs.end()) {
379 boost::mutex::scoped_lock lm (_mutex);
385 for (auto i: _jobs) {
386 if (i->pause_by_user()) {
396 JobManager::resume ()
398 boost::mutex::scoped_lock lm (_mutex);
404 _paused_job->resume ();
407 _paused_job.reset ();