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 ()
55 _scheduler = new boost::thread (boost::bind (&JobManager::scheduler, this));
58 JobManager::~JobManager ()
61 boost::mutex::scoped_lock lm (_mutex);
66 /* Ideally this would be a DCPOMATIC_ASSERT(_scheduler->joinable()) but we
67 can't throw exceptions from a destructor.
69 if (_scheduler->joinable ()) {
78 JobManager::add (shared_ptr<Job> j)
81 boost::mutex::scoped_lock lm (_mutex);
85 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
90 list<shared_ptr<Job> >
91 JobManager::get () const
93 boost::mutex::scoped_lock lm (_mutex);
98 JobManager::work_to_do () const
100 boost::mutex::scoped_lock lm (_mutex);
101 list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
102 while (i != _jobs.end() && (*i)->finished()) {
106 return i != _jobs.end ();
110 JobManager::errors () const
112 boost::mutex::scoped_lock lm (_mutex);
113 for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
114 if ((*i)->finished_in_error ()) {
123 JobManager::scheduler ()
127 optional<string> active_job;
130 boost::mutex::scoped_lock lm (_mutex);
135 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
137 if (!i->finished ()) {
138 active_job = i->json_name ();
142 /* Something is already happening */
148 /* Only start one job at once */
154 if (active_job != _last_active_job) {
155 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, active_job));
156 _last_active_job = active_job;
164 JobManager::instance ()
166 if (_instance == 0) {
167 _instance = new JobManager ();
182 JobManager::analyse_audio (
183 shared_ptr<const Film> film,
184 shared_ptr<const Playlist> playlist,
186 boost::signals2::connection& connection,
187 function<void()> ready
191 boost::mutex::scoped_lock lm (_mutex);
193 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
194 shared_ptr<AnalyseAudioJob> a = dynamic_pointer_cast<AnalyseAudioJob> (i);
195 if (a && a->playlist () == playlist) {
196 i->when_finished (connection, ready);
202 shared_ptr<AnalyseAudioJob> job;
205 boost::mutex::scoped_lock lm (_mutex);
207 job.reset (new AnalyseAudioJob (film, playlist, from_zero));
208 connection = job->Finished.connect (ready);
209 _jobs.push_back (job);
212 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
216 JobManager::increase_priority (shared_ptr<Job> job)
218 bool changed = false;
221 boost::mutex::scoped_lock lm (_mutex);
222 list<shared_ptr<Job> >::iterator last = _jobs.end ();
223 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
224 if (*i == job && last != _jobs.end()) {
239 JobManager::priority_changed ()
242 boost::mutex::scoped_lock lm (_mutex);
245 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
249 } else if (i->paused_by_priority ()) {
255 i->pause_by_priority ();
261 emit (boost::bind (boost::ref (JobsReordered)));
265 JobManager::decrease_priority (shared_ptr<Job> job)
267 bool changed = false;
269 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
270 list<shared_ptr<Job> >::iterator next = i;
272 if (*i == job && next != _jobs.end()) {