Add pause/resume to the batch converter (#1248).
[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         , _paused (false)
48         , _scheduler (0)
49 {
50
51 }
52
53 void
54 JobManager::start ()
55 {
56         _scheduler = new boost::thread (boost::bind (&JobManager::scheduler, this));
57 #ifdef DCPOMATIC_LINUX
58         pthread_setname_np (_scheduler->native_handle(), "job-scheduler");
59 #endif
60 }
61
62 JobManager::~JobManager ()
63 {
64         {
65                 boost::mutex::scoped_lock lm (_mutex);
66                 _terminate = true;
67         }
68
69         if (_scheduler) {
70                 /* Ideally this would be a DCPOMATIC_ASSERT(_scheduler->joinable()) but we
71                    can't throw exceptions from a destructor.
72                 */
73                 if (_scheduler->joinable ()) {
74                         _scheduler->join ();
75                 }
76         }
77
78         delete _scheduler;
79 }
80
81 shared_ptr<Job>
82 JobManager::add (shared_ptr<Job> j)
83 {
84         {
85                 boost::mutex::scoped_lock lm (_mutex);
86                 _jobs.push_back (j);
87         }
88
89         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
90
91         return j;
92 }
93
94 shared_ptr<Job>
95 JobManager::add_after (shared_ptr<Job> after, shared_ptr<Job> j)
96 {
97         {
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());
101                 _jobs.insert (i, j);
102         }
103
104         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
105
106         return j;
107 }
108
109 list<shared_ptr<Job> >
110 JobManager::get () const
111 {
112         boost::mutex::scoped_lock lm (_mutex);
113         return _jobs;
114 }
115
116 bool
117 JobManager::work_to_do () const
118 {
119         boost::mutex::scoped_lock lm (_mutex);
120         list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
121         while (i != _jobs.end() && (*i)->finished()) {
122                 ++i;
123         }
124
125         return i != _jobs.end ();
126 }
127
128 bool
129 JobManager::errors () const
130 {
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 ()) {
134                         return true;
135                 }
136         }
137
138         return false;
139 }
140
141 void
142 JobManager::scheduler ()
143 {
144         while (true) {
145
146                 optional<string> active_job;
147
148                 {
149                         boost::mutex::scoped_lock lm (_mutex);
150                         if (_terminate) {
151                                 return;
152                         }
153
154                         if (!_paused) {
155                                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
156
157                                         if (!i->finished ()) {
158                                                 active_job = i->json_name ();
159                                         }
160
161                                         if (i->running ()) {
162                                                 /* Something is already happening */
163                                                 break;
164                                         }
165
166                                         if (i->is_new()) {
167                                                 i->start ();
168                                                 /* Only start one job at once */
169                                                 break;
170                                         }
171                                 }
172                         }
173                 }
174
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;
178                 }
179
180                 dcpomatic_sleep (1);
181         }
182 }
183
184 JobManager *
185 JobManager::instance ()
186 {
187         if (_instance == 0) {
188                 _instance = new JobManager ();
189                 _instance->start ();
190         }
191
192         return _instance;
193 }
194
195 void
196 JobManager::drop ()
197 {
198         delete _instance;
199         _instance = 0;
200 }
201
202 void
203 JobManager::analyse_audio (
204         shared_ptr<const Film> film,
205         shared_ptr<const Playlist> playlist,
206         bool from_zero,
207         boost::signals2::connection& connection,
208         function<void()> ready
209         )
210 {
211         {
212                 boost::mutex::scoped_lock lm (_mutex);
213
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);
218                                 return;
219                         }
220                 }
221         }
222
223         shared_ptr<AnalyseAudioJob> job;
224
225         {
226                 boost::mutex::scoped_lock lm (_mutex);
227
228                 job.reset (new AnalyseAudioJob (film, playlist, from_zero));
229                 connection = job->Finished.connect (ready);
230                 _jobs.push_back (job);
231         }
232
233         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
234 }
235
236 void
237 JobManager::increase_priority (shared_ptr<Job> job)
238 {
239         bool changed = false;
240
241         {
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()) {
246                                 swap (*i, *last);
247                                 changed = true;
248                                 break;
249                         }
250                         last = i;
251                 }
252         }
253
254         if (changed) {
255                 priority_changed ();
256         }
257 }
258
259 void
260 JobManager::priority_changed ()
261 {
262         {
263                 boost::mutex::scoped_lock lm (_mutex);
264
265                 bool first = true;
266                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
267                         if (first) {
268                                 if (i->is_new ()) {
269                                         i->start ();
270                                 } else if (i->paused_by_priority ()) {
271                                         i->resume ();
272                                 }
273                                 first = false;
274                         } else {
275                                 if (i->running ()) {
276                                         i->pause_by_priority ();
277                                 }
278                         }
279                 }
280         }
281
282         emit (boost::bind (boost::ref (JobsReordered)));
283 }
284
285 void
286 JobManager::decrease_priority (shared_ptr<Job> job)
287 {
288         bool changed = false;
289
290         {
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;
294                         ++next;
295                         if (*i == job && next != _jobs.end()) {
296                                 swap (*i, *next);
297                                 changed = true;
298                                 break;
299                         }
300                 }
301         }
302
303         if (changed) {
304                 priority_changed ();
305         }
306 }
307
308 void
309 JobManager::pause ()
310 {
311         boost::mutex::scoped_lock lm (_mutex);
312
313         if (_paused) {
314                 return;
315         }
316
317         BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
318                 if (i->pause_by_user()) {
319                         _paused_job = i;
320                 }
321         }
322
323         _paused = true;
324 }
325
326 void
327 JobManager::resume ()
328 {
329         boost::mutex::scoped_lock lm (_mutex);
330         if (!_paused) {
331                 return;
332         }
333
334         if (_paused_job) {
335                 _paused_job->resume ();
336         }
337
338         _paused_job.reset ();
339         _paused = false;
340 }