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;
39 using std::make_shared;
40 using std::shared_ptr;
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 ()
156 start_of_thread ("JobManager");
160 boost::mutex::scoped_lock lm (_mutex);
163 bool have_new = false;
164 bool have_running = false;
165 for (auto i: _jobs) {
174 if ((!have_running && have_new) || _terminate) {
178 _empty_condition.wait (lm);
185 for (auto i: _jobs) {
187 _connections.push_back (i->FinishedImmediate.connect(bind(&JobManager::job_finished, this)));
189 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, i->json_name()));
190 _last_active_job = i->json_name ();
191 /* Only start one job at once */
200 JobManager::job_finished ()
203 boost::mutex::scoped_lock lm (_mutex);
204 emit (boost::bind(boost::ref (ActiveJobsChanged), _last_active_job, optional<string>()));
205 _last_active_job = optional<string>();
208 _empty_condition.notify_all ();
213 JobManager::instance ()
216 _instance = new JobManager ();
233 JobManager::analyse_audio (
234 shared_ptr<const Film> film,
235 shared_ptr<const Playlist> playlist,
237 boost::signals2::connection& connection,
238 function<void()> ready
242 boost::mutex::scoped_lock lm (_mutex);
244 for (auto i: _jobs) {
245 auto a = dynamic_pointer_cast<AnalyseAudioJob> (i);
246 if (a && a->path() == film->audio_analysis_path(playlist) && !i->finished_cancelled()) {
247 i->when_finished (connection, ready);
253 shared_ptr<AnalyseAudioJob> job;
256 boost::mutex::scoped_lock lm (_mutex);
258 job = make_shared<AnalyseAudioJob> (film, playlist, from_zero);
259 connection = job->Finished.connect (ready);
260 _jobs.push_back (job);
261 _empty_condition.notify_all ();
264 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
269 JobManager::analyse_subtitles (
270 shared_ptr<const Film> film,
271 shared_ptr<Content> content,
272 boost::signals2::connection& connection,
273 function<void()> ready
277 boost::mutex::scoped_lock lm (_mutex);
279 for (auto i: _jobs) {
280 auto a = dynamic_pointer_cast<AnalyseSubtitlesJob> (i);
281 if (a && a->path() == film->subtitle_analysis_path(content)) {
282 i->when_finished (connection, ready);
288 shared_ptr<AnalyseSubtitlesJob> job;
291 boost::mutex::scoped_lock lm (_mutex);
293 job = make_shared<AnalyseSubtitlesJob>(film, content);
294 connection = job->Finished.connect (ready);
295 _jobs.push_back (job);
296 _empty_condition.notify_all ();
299 emit (boost::bind(boost::ref(JobAdded), weak_ptr<Job>(job)));
304 JobManager::increase_priority (shared_ptr<Job> job)
306 bool changed = false;
309 boost::mutex::scoped_lock lm (_mutex);
310 auto last = _jobs.end ();
311 for (auto i = _jobs.begin(); i != _jobs.end(); ++i) {
312 if (*i == job && last != _jobs.end()) {
328 JobManager::priority_changed ()
331 boost::mutex::scoped_lock lm (_mutex);
334 for (auto i: _jobs) {
338 } else if (i->paused_by_priority ()) {
344 i->pause_by_priority ();
350 emit (boost::bind(boost::ref(JobsReordered)));
355 JobManager::decrease_priority (shared_ptr<Job> job)
357 bool changed = false;
360 boost::mutex::scoped_lock lm (_mutex);
361 for (auto i = _jobs.begin(); i != _jobs.end(); ++i) {
364 if (*i == job && next != _jobs.end()) {
381 boost::mutex::scoped_lock lm (_mutex);
387 for (auto i: _jobs) {
388 if (i->pause_by_user()) {
398 JobManager::resume ()
400 boost::mutex::scoped_lock lm (_mutex);
406 _paused_job->resume ();
409 _paused_job.reset ();