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);
166 bool have_running = false;
167 for (auto i: _jobs) {
168 if (have_running && i->running()) {
169 i->pause_by_priority();
170 } else if (!have_running && (i->is_new() || i->paused_by_priority())) {
172 _connections.push_back (i->FinishedImmediate.connect(bind(&JobManager::job_finished, this)));
177 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, i->json_name()));
178 _last_active_job = i->json_name ();
180 } else if (!have_running && i->running()) {
185 _empty_condition.wait (lm);
191 JobManager::job_finished ()
194 boost::mutex::scoped_lock lm (_mutex);
195 emit (boost::bind(boost::ref (ActiveJobsChanged), _last_active_job, optional<string>()));
196 _last_active_job = optional<string>();
199 _empty_condition.notify_all ();
204 JobManager::instance ()
207 _instance = new JobManager ();
224 JobManager::analyse_audio (
225 shared_ptr<const Film> film,
226 shared_ptr<const Playlist> playlist,
228 boost::signals2::connection& connection,
229 function<void()> ready
233 boost::mutex::scoped_lock lm (_mutex);
235 for (auto i: _jobs) {
236 auto a = dynamic_pointer_cast<AnalyseAudioJob> (i);
237 if (a && a->path() == film->audio_analysis_path(playlist) && !i->finished_cancelled()) {
238 i->when_finished (connection, ready);
244 shared_ptr<AnalyseAudioJob> job;
247 boost::mutex::scoped_lock lm (_mutex);
249 job = make_shared<AnalyseAudioJob> (film, playlist, from_zero);
250 connection = job->Finished.connect (ready);
251 _jobs.push_back (job);
252 _empty_condition.notify_all ();
255 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
260 JobManager::analyse_subtitles (
261 shared_ptr<const Film> film,
262 shared_ptr<Content> content,
263 boost::signals2::connection& connection,
264 function<void()> ready
268 boost::mutex::scoped_lock lm (_mutex);
270 for (auto i: _jobs) {
271 auto a = dynamic_pointer_cast<AnalyseSubtitlesJob> (i);
272 if (a && a->path() == film->subtitle_analysis_path(content)) {
273 i->when_finished (connection, ready);
279 shared_ptr<AnalyseSubtitlesJob> job;
282 boost::mutex::scoped_lock lm (_mutex);
284 job = make_shared<AnalyseSubtitlesJob>(film, content);
285 connection = job->Finished.connect (ready);
286 _jobs.push_back (job);
287 _empty_condition.notify_all ();
290 emit (boost::bind(boost::ref(JobAdded), weak_ptr<Job>(job)));
295 JobManager::increase_priority (shared_ptr<Job> job)
297 bool changed = false;
300 boost::mutex::scoped_lock lm (_mutex);
301 auto last = _jobs.end ();
302 for (auto i = _jobs.begin(); i != _jobs.end(); ++i) {
303 if (*i == job && last != _jobs.end()) {
313 _empty_condition.notify_all ();
314 emit (boost::bind(boost::ref(JobsReordered)));
320 JobManager::decrease_priority (shared_ptr<Job> job)
322 bool changed = false;
325 boost::mutex::scoped_lock lm (_mutex);
326 for (auto i = _jobs.begin(); i != _jobs.end(); ++i) {
329 if (*i == job && next != _jobs.end()) {
338 _empty_condition.notify_all ();
339 emit (boost::bind(boost::ref(JobsReordered)));
347 boost::mutex::scoped_lock lm (_mutex);
353 for (auto i: _jobs) {
354 if (i->pause_by_user()) {
364 JobManager::resume ()
366 boost::mutex::scoped_lock lm (_mutex);
372 _paused_job->resume ();
375 _paused_job.reset ();