Merge branch 'master' of ssh://git.carlh.net/home/carl/git/dcpomatic
[dcpomatic.git] / src / lib / job_manager.cc
1 /*
2     Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 /** @file  src/job_manager.cc
22  *  @brief A simple scheduler for jobs.
23  */
24
25 #include "job_manager.h"
26 #include "job.h"
27 #include "cross.h"
28 #include "analyse_audio_job.h"
29 #include "film.h"
30 #include <boost/thread.hpp>
31 #include <boost/foreach.hpp>
32 #include <iostream>
33
34 using std::string;
35 using std::list;
36 using std::cout;
37 using boost::shared_ptr;
38 using boost::weak_ptr;
39 using boost::function;
40 using boost::dynamic_pointer_cast;
41 using boost::optional;
42
43 JobManager* JobManager::_instance = 0;
44
45 JobManager::JobManager ()
46         : _terminate (false)
47         , _scheduler (0)
48 {
49
50 }
51
52 void
53 JobManager::start ()
54 {
55         _scheduler = new boost::thread (boost::bind (&JobManager::scheduler, this));
56 #ifdef DCPOMATIC_LINUX
57         pthread_setname_np (_scheduler->native_handle(), "job-scheduler");
58 #endif
59 }
60
61 JobManager::~JobManager ()
62 {
63         {
64                 boost::mutex::scoped_lock lm (_mutex);
65                 _terminate = true;
66         }
67
68         if (_scheduler) {
69                 /* Ideally this would be a DCPOMATIC_ASSERT(_scheduler->joinable()) but we
70                    can't throw exceptions from a destructor.
71                 */
72                 if (_scheduler->joinable ()) {
73                         _scheduler->join ();
74                 }
75         }
76
77         delete _scheduler;
78 }
79
80 shared_ptr<Job>
81 JobManager::add (shared_ptr<Job> j)
82 {
83         {
84                 boost::mutex::scoped_lock lm (_mutex);
85                 _jobs.push_back (j);
86         }
87
88         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
89
90         return j;
91 }
92
93 shared_ptr<Job>
94 JobManager::add_after (shared_ptr<Job> after, shared_ptr<Job> j)
95 {
96         {
97                 boost::mutex::scoped_lock lm (_mutex);
98                 list<shared_ptr<Job> >::iterator i = find (_jobs.begin(), _jobs.end(), after);
99                 DCPOMATIC_ASSERT (i != _jobs.end());
100                 _jobs.insert (i, j);
101         }
102
103         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
104
105         return j;
106 }
107
108 list<shared_ptr<Job> >
109 JobManager::get () const
110 {
111         boost::mutex::scoped_lock lm (_mutex);
112         return _jobs;
113 }
114
115 bool
116 JobManager::work_to_do () const
117 {
118         boost::mutex::scoped_lock lm (_mutex);
119         list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
120         while (i != _jobs.end() && (*i)->finished()) {
121                 ++i;
122         }
123
124         return i != _jobs.end ();
125 }
126
127 bool
128 JobManager::errors () const
129 {
130         boost::mutex::scoped_lock lm (_mutex);
131         for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
132                 if ((*i)->finished_in_error ()) {
133                         return true;
134                 }
135         }
136
137         return false;
138 }
139
140 void
141 JobManager::scheduler ()
142 {
143         while (true) {
144
145                 optional<string> active_job;
146
147                 {
148                         boost::mutex::scoped_lock lm (_mutex);
149                         if (_terminate) {
150                                 return;
151                         }
152
153                         BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
154
155                                 if (!i->finished ()) {
156                                         active_job = i->json_name ();
157                                 }
158
159                                 if (i->running ()) {
160                                         /* Something is already happening */
161                                         break;
162                                 }
163
164                                 if (i->is_new()) {
165                                         i->start ();
166                                         /* Only start one job at once */
167                                         break;
168                                 }
169                         }
170                 }
171
172                 if (active_job != _last_active_job) {
173                         emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, active_job));
174                         _last_active_job = active_job;
175                 }
176
177                 dcpomatic_sleep (1);
178         }
179 }
180
181 JobManager *
182 JobManager::instance ()
183 {
184         if (_instance == 0) {
185                 _instance = new JobManager ();
186                 _instance->start ();
187         }
188
189         return _instance;
190 }
191
192 void
193 JobManager::drop ()
194 {
195         delete _instance;
196         _instance = 0;
197 }
198
199 void
200 JobManager::analyse_audio (
201         shared_ptr<const Film> film,
202         shared_ptr<const Playlist> playlist,
203         bool from_zero,
204         boost::signals2::connection& connection,
205         function<void()> ready
206         )
207 {
208         {
209                 boost::mutex::scoped_lock lm (_mutex);
210
211                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
212                         shared_ptr<AnalyseAudioJob> a = dynamic_pointer_cast<AnalyseAudioJob> (i);
213                         if (a && a->playlist () == playlist) {
214                                 i->when_finished (connection, ready);
215                                 return;
216                         }
217                 }
218         }
219
220         shared_ptr<AnalyseAudioJob> job;
221
222         {
223                 boost::mutex::scoped_lock lm (_mutex);
224
225                 job.reset (new AnalyseAudioJob (film, playlist, from_zero));
226                 connection = job->Finished.connect (ready);
227                 _jobs.push_back (job);
228         }
229
230         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
231 }
232
233 void
234 JobManager::increase_priority (shared_ptr<Job> job)
235 {
236         bool changed = false;
237
238         {
239                 boost::mutex::scoped_lock lm (_mutex);
240                 list<shared_ptr<Job> >::iterator last = _jobs.end ();
241                 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
242                         if (*i == job && last != _jobs.end()) {
243                                 swap (*i, *last);
244                                 changed = true;
245                                 break;
246                         }
247                         last = i;
248                 }
249         }
250
251         if (changed) {
252                 priority_changed ();
253         }
254 }
255
256 void
257 JobManager::priority_changed ()
258 {
259         {
260                 boost::mutex::scoped_lock lm (_mutex);
261
262                 bool first = true;
263                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
264                         if (first) {
265                                 if (i->is_new ()) {
266                                         i->start ();
267                                 } else if (i->paused_by_priority ()) {
268                                         i->resume ();
269                                 }
270                                 first = false;
271                         } else {
272                                 if (i->running ()) {
273                                         i->pause_by_priority ();
274                                 }
275                         }
276                 }
277         }
278
279         emit (boost::bind (boost::ref (JobsReordered)));
280 }
281
282 void
283 JobManager::decrease_priority (shared_ptr<Job> job)
284 {
285         bool changed = false;
286
287         for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
288                 list<shared_ptr<Job> >::iterator next = i;
289                 ++next;
290                 if (*i == job && next != _jobs.end()) {
291                         swap (*i, *next);
292                         changed = true;
293                         break;
294                 }
295         }
296
297         if (changed) {
298                 priority_changed ();
299         }
300 }