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);
72 JobManager::add (shared_ptr<Job> j)
75 boost::mutex::scoped_lock lm (_mutex);
79 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
84 list<shared_ptr<Job> >
85 JobManager::get () const
87 boost::mutex::scoped_lock lm (_mutex);
92 JobManager::work_to_do () const
94 boost::mutex::scoped_lock lm (_mutex);
95 list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
96 while (i != _jobs.end() && (*i)->finished()) {
100 return i != _jobs.end ();
104 JobManager::errors () const
106 boost::mutex::scoped_lock lm (_mutex);
107 for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
108 if ((*i)->finished_in_error ()) {
117 JobManager::scheduler ()
121 optional<string> active_job;
124 boost::mutex::scoped_lock lm (_mutex);
129 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
131 if (!i->finished ()) {
132 active_job = i->json_name ();
136 /* Something is already happening */
142 /* Only start one job at once */
148 if (active_job != _last_active_job) {
149 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, active_job));
150 _last_active_job = active_job;
158 JobManager::instance ()
160 if (_instance == 0) {
161 _instance = new JobManager ();
176 JobManager::analyse_audio (
177 shared_ptr<const Film> film,
178 shared_ptr<const Playlist> playlist,
179 boost::signals2::connection& connection,
180 function<void()> ready
184 boost::mutex::scoped_lock lm (_mutex);
186 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
187 shared_ptr<AnalyseAudioJob> a = dynamic_pointer_cast<AnalyseAudioJob> (i);
188 if (a && a->playlist () == playlist) {
189 i->when_finished (connection, ready);
195 shared_ptr<AnalyseAudioJob> job;
198 boost::mutex::scoped_lock lm (_mutex);
200 job.reset (new AnalyseAudioJob (film, playlist));
201 connection = job->Finished.connect (ready);
202 _jobs.push_back (job);
205 emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));