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 _empty_condition.notify_all ();
88 JobManager::add (shared_ptr<Job> j)
91 boost::mutex::scoped_lock lm (_mutex);
93 _empty_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 _empty_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 && i->running()) {
170 i->pause_by_priority();
171 } else if (!have_running && (i->is_new() || i->paused_by_priority())) {
173 _connections.push_back (i->FinishedImmediate.connect(bind(&JobManager::job_finished, this)));
178 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, i->json_name()));
179 _last_active_job = i->json_name ();
181 } else if (!have_running && i->running()) {
186 _empty_condition.wait (lm);
192 JobManager::job_finished ()
195 boost::mutex::scoped_lock lm (_mutex);
196 emit (boost::bind(boost::ref (ActiveJobsChanged), _last_active_job, optional<string>()));
197 _last_active_job = optional<string>();
200 _empty_condition.notify_all ();
205 JobManager::instance ()
208 _instance = new JobManager ();
225 JobManager::analyse_audio (
226 shared_ptr<const Film> film,
227 shared_ptr<const Playlist> playlist,
229 boost::signals2::connection& connection,
230 function<void()> ready
234 boost::mutex::scoped_lock lm (_mutex);
236 for (auto i: _jobs) {
237 auto a = dynamic_pointer_cast<AnalyseAudioJob> (i);
238 if (a && a->path() == film->audio_analysis_path(playlist) && !i->finished_cancelled()) {
239 i->when_finished (connection, ready);
245 shared_ptr<AnalyseAudioJob> job;
248 boost::mutex::scoped_lock lm (_mutex);
250 job = make_shared<AnalyseAudioJob> (film, playlist, from_zero);
251 connection = job->Finished.connect (ready);
252 _jobs.push_back (job);
253 _empty_condition.notify_all ();
256 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
261 JobManager::analyse_subtitles (
262 shared_ptr<const Film> film,
263 shared_ptr<Content> content,
264 boost::signals2::connection& connection,
265 function<void()> ready
269 boost::mutex::scoped_lock lm (_mutex);
271 for (auto i: _jobs) {
272 auto a = dynamic_pointer_cast<AnalyseSubtitlesJob> (i);
273 if (a && a->path() == film->subtitle_analysis_path(content)) {
274 i->when_finished (connection, ready);
280 shared_ptr<AnalyseSubtitlesJob> job;
283 boost::mutex::scoped_lock lm (_mutex);
285 job = make_shared<AnalyseSubtitlesJob>(film, content);
286 connection = job->Finished.connect (ready);
287 _jobs.push_back (job);
288 _empty_condition.notify_all ();
291 emit (boost::bind(boost::ref(JobAdded), weak_ptr<Job>(job)));
296 JobManager::increase_priority (shared_ptr<Job> job)
299 boost::mutex::scoped_lock lm (_mutex);
300 auto iter = std::find(_jobs.begin(), _jobs.end(), job);
301 if (iter == _jobs.begin() || iter == _jobs.end()) {
304 swap(*iter, *std::prev(iter));
307 _empty_condition.notify_all();
308 emit(boost::bind(boost::ref(JobsReordered)));
313 JobManager::decrease_priority (shared_ptr<Job> job)
316 boost::mutex::scoped_lock lm (_mutex);
317 auto iter = std::find(_jobs.begin(), _jobs.end(), job);
318 if (iter == _jobs.end() || std::next(iter) == _jobs.end()) {
321 swap(*iter, *std::next(iter));
324 _empty_condition.notify_all();
325 emit(boost::bind(boost::ref(JobsReordered)));
332 boost::mutex::scoped_lock lm (_mutex);
338 for (auto i: _jobs) {
339 if (i->pause_by_user()) {
349 JobManager::resume ()
351 boost::mutex::scoped_lock lm (_mutex);
357 _paused_job->resume ();
360 _paused_job.reset ();