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"
34 #include <boost/thread.hpp>
37 using std::dynamic_pointer_cast;
40 using std::make_shared;
41 using std::shared_ptr;
45 using boost::optional;
48 JobManager* JobManager::_instance = nullptr;
51 JobManager::JobManager ()
60 _scheduler = boost::thread (boost::bind(&JobManager::scheduler, this));
61 #ifdef DCPOMATIC_LINUX
62 pthread_setname_np (_scheduler.native_handle(), "job-scheduler");
67 JobManager::~JobManager ()
69 boost::this_thread::disable_interruption dis;
71 for (auto& i: _connections) {
76 boost::mutex::scoped_lock lm (_mutex);
78 _schedule_condition.notify_all();
88 JobManager::add (shared_ptr<Job> j)
91 boost::mutex::scoped_lock lm (_mutex);
93 _schedule_condition.notify_all();
96 emit (boost::bind(boost::ref(JobAdded), weak_ptr<Job>(j)));
103 JobManager::add_after (shared_ptr<Job> after, shared_ptr<Job> j)
106 boost::mutex::scoped_lock lm (_mutex);
107 auto i = find (_jobs.begin(), _jobs.end(), after);
108 DCPOMATIC_ASSERT (i != _jobs.end());
110 _schedule_condition.notify_all();
113 emit (boost::bind(boost::ref(JobAdded), weak_ptr<Job>(j)));
119 list<shared_ptr<Job>>
120 JobManager::get () const
122 boost::mutex::scoped_lock lm (_mutex);
128 JobManager::work_to_do () const
130 boost::mutex::scoped_lock lm (_mutex);
131 auto i = _jobs.begin();
132 while (i != _jobs.end() && (*i)->finished()) {
136 return i != _jobs.end ();
141 JobManager::errors () const
143 boost::mutex::scoped_lock lm (_mutex);
144 for (auto i: _jobs) {
145 if (i->finished_in_error()) {
155 JobManager::scheduler ()
157 start_of_thread ("JobManager");
161 boost::mutex::scoped_lock lm (_mutex);
167 bool have_running = false;
168 for (auto i: _jobs) {
169 if ((have_running || _paused) && i->running()) {
170 /* We already have a running job, or are totally paused, so this job should not be running */
171 i->pause_by_priority();
172 } else if (!have_running && !_paused && (i->is_new() || i->paused_by_priority())) {
173 /* We don't have a running job, and we should have one, so start/resume this */
175 _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 ();
183 } else if (!have_running && i->running()) {
188 _schedule_condition.wait(lm);
194 JobManager::job_finished ()
197 boost::mutex::scoped_lock lm (_mutex);
198 emit (boost::bind(boost::ref (ActiveJobsChanged), _last_active_job, optional<string>()));
199 _last_active_job = optional<string>();
202 _schedule_condition.notify_all();
207 JobManager::instance ()
210 _instance = new JobManager ();
227 JobManager::analyse_audio (
228 shared_ptr<const Film> film,
229 shared_ptr<const Playlist> playlist,
231 boost::signals2::connection& connection,
232 function<void (Job::Result)> ready
236 boost::mutex::scoped_lock lm (_mutex);
238 for (auto i: _jobs) {
239 auto a = dynamic_pointer_cast<AnalyseAudioJob> (i);
240 if (a && a->path() == film->audio_analysis_path(playlist) && !i->finished_cancelled()) {
241 i->when_finished (connection, ready);
247 shared_ptr<AnalyseAudioJob> job;
250 boost::mutex::scoped_lock lm (_mutex);
252 job = make_shared<AnalyseAudioJob> (film, playlist, from_zero);
253 connection = job->Finished.connect (ready);
254 _jobs.push_back (job);
255 _schedule_condition.notify_all ();
258 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
263 JobManager::analyse_subtitles (
264 shared_ptr<const Film> film,
265 shared_ptr<Content> content,
266 boost::signals2::connection& connection,
267 function<void (Job::Result)> ready
271 boost::mutex::scoped_lock lm (_mutex);
273 for (auto i: _jobs) {
274 auto a = dynamic_pointer_cast<AnalyseSubtitlesJob> (i);
275 if (a && a->path() == film->subtitle_analysis_path(content) && !i->finished_cancelled()) {
276 i->when_finished (connection, ready);
282 shared_ptr<AnalyseSubtitlesJob> job;
285 boost::mutex::scoped_lock lm (_mutex);
287 job = make_shared<AnalyseSubtitlesJob>(film, content);
288 connection = job->Finished.connect (ready);
289 _jobs.push_back (job);
290 _schedule_condition.notify_all ();
293 emit (boost::bind(boost::ref(JobAdded), weak_ptr<Job>(job)));
298 JobManager::increase_priority (shared_ptr<Job> job)
301 boost::mutex::scoped_lock lm (_mutex);
302 auto iter = std::find(_jobs.begin(), _jobs.end(), job);
303 if (iter == _jobs.begin() || iter == _jobs.end()) {
306 swap(*iter, *std::prev(iter));
309 _schedule_condition.notify_all();
310 emit(boost::bind(boost::ref(JobsReordered)));
315 JobManager::decrease_priority (shared_ptr<Job> job)
318 boost::mutex::scoped_lock lm (_mutex);
319 auto iter = std::find(_jobs.begin(), _jobs.end(), job);
320 if (iter == _jobs.end() || std::next(iter) == _jobs.end()) {
323 swap(*iter, *std::next(iter));
326 _schedule_condition.notify_all();
327 emit(boost::bind(boost::ref(JobsReordered)));
331 /** Pause all job processing */
335 boost::mutex::scoped_lock lm (_mutex);
337 _schedule_condition.notify_all();
341 /** Resume processing jobs after a previous pause() */
343 JobManager::resume ()
345 boost::mutex::scoped_lock lm (_mutex);
347 _schedule_condition.notify_all();