2 Copyright (C) 2012-2018 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/>.
21 /** @file src/job_manager.cc
22 * @brief A simple scheduler for jobs.
25 #include "job_manager.h"
28 #include "analyse_audio_job.h"
30 #include <boost/thread.hpp>
31 #include <boost/foreach.hpp>
37 using boost::shared_ptr;
38 using boost::weak_ptr;
39 using boost::function;
40 using boost::dynamic_pointer_cast;
41 using boost::optional;
43 JobManager* JobManager::_instance = 0;
45 JobManager::JobManager ()
56 _scheduler = new boost::thread (boost::bind (&JobManager::scheduler, this));
57 #ifdef DCPOMATIC_LINUX
58 pthread_setname_np (_scheduler->native_handle(), "job-scheduler");
62 JobManager::~JobManager ()
65 boost::mutex::scoped_lock lm (_mutex);
70 /* Ideally this would be a DCPOMATIC_ASSERT(_scheduler->joinable()) but we
71 can't throw exceptions from a destructor.
73 if (_scheduler->joinable ()) {
82 JobManager::add (shared_ptr<Job> j)
85 boost::mutex::scoped_lock lm (_mutex);
89 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
95 JobManager::add_after (shared_ptr<Job> after, shared_ptr<Job> j)
98 boost::mutex::scoped_lock lm (_mutex);
99 list<shared_ptr<Job> >::iterator i = find (_jobs.begin(), _jobs.end(), after);
100 DCPOMATIC_ASSERT (i != _jobs.end());
104 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
109 list<shared_ptr<Job> >
110 JobManager::get () const
112 boost::mutex::scoped_lock lm (_mutex);
117 JobManager::work_to_do () const
119 boost::mutex::scoped_lock lm (_mutex);
120 list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
121 while (i != _jobs.end() && (*i)->finished()) {
125 return i != _jobs.end ();
129 JobManager::errors () const
131 boost::mutex::scoped_lock lm (_mutex);
132 for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
133 if ((*i)->finished_in_error ()) {
142 JobManager::scheduler ()
146 optional<string> active_job;
149 boost::mutex::scoped_lock lm (_mutex);
155 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
157 if (!i->finished ()) {
158 active_job = i->json_name ();
162 /* Something is already happening */
168 /* Only start one job at once */
175 if (active_job != _last_active_job) {
176 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, active_job));
177 _last_active_job = active_job;
185 JobManager::instance ()
187 if (_instance == 0) {
188 _instance = new JobManager ();
203 JobManager::analyse_audio (
204 shared_ptr<const Film> film,
205 shared_ptr<const Playlist> playlist,
207 boost::signals2::connection& connection,
208 function<void()> ready
212 boost::mutex::scoped_lock lm (_mutex);
214 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
215 shared_ptr<AnalyseAudioJob> a = dynamic_pointer_cast<AnalyseAudioJob> (i);
216 if (a && a->playlist () == playlist) {
217 i->when_finished (connection, ready);
223 shared_ptr<AnalyseAudioJob> job;
226 boost::mutex::scoped_lock lm (_mutex);
228 job.reset (new AnalyseAudioJob (film, playlist, from_zero));
229 connection = job->Finished.connect (ready);
230 _jobs.push_back (job);
233 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
237 JobManager::increase_priority (shared_ptr<Job> job)
239 bool changed = false;
242 boost::mutex::scoped_lock lm (_mutex);
243 list<shared_ptr<Job> >::iterator last = _jobs.end ();
244 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
245 if (*i == job && last != _jobs.end()) {
260 JobManager::priority_changed ()
263 boost::mutex::scoped_lock lm (_mutex);
266 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
270 } else if (i->paused_by_priority ()) {
276 i->pause_by_priority ();
282 emit (boost::bind (boost::ref (JobsReordered)));
286 JobManager::decrease_priority (shared_ptr<Job> job)
288 bool changed = false;
291 boost::mutex::scoped_lock lm (_mutex);
292 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
293 list<shared_ptr<Job> >::iterator next = i;
295 if (*i == job && next != _jobs.end()) {
311 boost::mutex::scoped_lock lm (_mutex);
317 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
318 if (i->pause_by_user()) {
327 JobManager::resume ()
329 boost::mutex::scoped_lock lm (_mutex);
335 _paused_job->resume ();
338 _paused_job.reset ();