2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file src/job_manager.cc
21 * @brief A simple scheduler for jobs.
24 #include "job_manager.h"
27 #include "analyse_audio_job.h"
29 #include <boost/thread.hpp>
30 #include <boost/foreach.hpp>
36 using boost::shared_ptr;
37 using boost::weak_ptr;
38 using boost::function;
39 using boost::dynamic_pointer_cast;
40 using boost::optional;
42 JobManager* JobManager::_instance = 0;
44 JobManager::JobManager ()
54 _scheduler = new boost::thread (boost::bind (&JobManager::scheduler, this));
57 JobManager::~JobManager ()
60 boost::mutex::scoped_lock lm (_mutex);
65 DCPOMATIC_ASSERT (_scheduler->joinable ());
73 JobManager::add (shared_ptr<Job> j)
76 boost::mutex::scoped_lock lm (_mutex);
80 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
85 list<shared_ptr<Job> >
86 JobManager::get () const
88 boost::mutex::scoped_lock lm (_mutex);
93 JobManager::work_to_do () const
95 boost::mutex::scoped_lock lm (_mutex);
96 list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
97 while (i != _jobs.end() && (*i)->finished()) {
101 return i != _jobs.end ();
105 JobManager::errors () const
107 boost::mutex::scoped_lock lm (_mutex);
108 for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
109 if ((*i)->finished_in_error ()) {
118 JobManager::scheduler ()
122 optional<string> active_job;
125 boost::mutex::scoped_lock lm (_mutex);
130 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
132 if (!i->finished ()) {
133 active_job = i->json_name ();
137 /* Something is already happening */
143 /* Only start one job at once */
149 if (active_job != _last_active_job) {
150 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, active_job));
151 _last_active_job = active_job;
159 JobManager::instance ()
161 if (_instance == 0) {
162 _instance = new JobManager ();
177 JobManager::analyse_audio (
178 shared_ptr<const Film> film,
179 shared_ptr<const Playlist> playlist,
180 boost::signals2::connection& connection,
181 function<void()> ready
185 boost::mutex::scoped_lock lm (_mutex);
187 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
188 shared_ptr<AnalyseAudioJob> a = dynamic_pointer_cast<AnalyseAudioJob> (i);
189 if (a && a->playlist () == playlist) {
190 i->when_finished (connection, ready);
196 shared_ptr<AnalyseAudioJob> job;
199 boost::mutex::scoped_lock lm (_mutex);
201 job.reset (new AnalyseAudioJob (film, playlist));
202 connection = job->Finished.connect (ready);
203 _jobs.push_back (job);
206 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));