358820c6a3c35a0ff88d3a295f2c3566435a58e1
[dcpomatic.git] / src / lib / job_manager.cc
1 /*
2     Copyright (C) 2012-2020 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 "analyse_subtitles_job.h"
30 #include "film.h"
31 #include <boost/thread.hpp>
32 #include <boost/foreach.hpp>
33 #include <iostream>
34
35 using std::string;
36 using std::list;
37 using std::cout;
38 using std::shared_ptr;
39 using std::weak_ptr;
40 using boost::function;
41 using std::dynamic_pointer_cast;
42 using boost::optional;
43 using boost::bind;
44
45 JobManager* JobManager::_instance = 0;
46
47 JobManager::JobManager ()
48         : _terminate (false)
49         , _paused (false)
50 {
51
52 }
53
54 void
55 JobManager::start ()
56 {
57         _scheduler = boost::thread (boost::bind(&JobManager::scheduler, this));
58 #ifdef DCPOMATIC_LINUX
59         pthread_setname_np (_scheduler.native_handle(), "job-scheduler");
60 #endif
61 }
62
63 JobManager::~JobManager ()
64 {
65         boost::this_thread::disable_interruption dis;
66
67         BOOST_FOREACH (boost::signals2::connection& i, _connections) {
68                 i.disconnect ();
69         }
70
71         {
72                 boost::mutex::scoped_lock lm (_mutex);
73                 _terminate = true;
74                 _empty_condition.notify_all ();
75         }
76
77         try {
78                 _scheduler.join();
79         } catch (...) {}
80 }
81
82 shared_ptr<Job>
83 JobManager::add (shared_ptr<Job> j)
84 {
85         {
86                 boost::mutex::scoped_lock lm (_mutex);
87                 _jobs.push_back (j);
88                 _empty_condition.notify_all ();
89         }
90
91         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
92
93         return j;
94 }
95
96 shared_ptr<Job>
97 JobManager::add_after (shared_ptr<Job> after, shared_ptr<Job> j)
98 {
99         {
100                 boost::mutex::scoped_lock lm (_mutex);
101                 list<shared_ptr<Job> >::iterator i = find (_jobs.begin(), _jobs.end(), after);
102                 DCPOMATIC_ASSERT (i != _jobs.end());
103                 _jobs.insert (i, j);
104                 _empty_condition.notify_all ();
105         }
106
107         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
108
109         return j;
110 }
111
112 list<shared_ptr<Job> >
113 JobManager::get () const
114 {
115         boost::mutex::scoped_lock lm (_mutex);
116         return _jobs;
117 }
118
119 bool
120 JobManager::work_to_do () const
121 {
122         boost::mutex::scoped_lock lm (_mutex);
123         list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
124         while (i != _jobs.end() && (*i)->finished()) {
125                 ++i;
126         }
127
128         return i != _jobs.end ();
129 }
130
131 bool
132 JobManager::errors () const
133 {
134         boost::mutex::scoped_lock lm (_mutex);
135         BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
136                 if (i->finished_in_error ()) {
137                         return true;
138                 }
139         }
140
141         return false;
142 }
143
144 void
145 JobManager::scheduler ()
146 {
147         while (true) {
148
149                 boost::mutex::scoped_lock lm (_mutex);
150
151                 while (true) {
152                         bool have_new = false;
153                         bool have_running = false;
154                         BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
155                                 if (i->running()) {
156                                         have_running = true;
157                                 }
158                                 if (i->is_new()) {
159                                         have_new = true;
160                                 }
161                         }
162
163                         if ((!have_running && have_new) || _terminate) {
164                                 break;
165                         }
166
167                         _empty_condition.wait (lm);
168                 }
169
170                 if (_terminate) {
171                         break;
172                 }
173
174                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
175                         if (i->is_new()) {
176                                 _connections.push_back (i->FinishedImmediate.connect(bind(&JobManager::job_finished, this)));
177                                 i->start ();
178                                 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, i->json_name()));
179                                 _last_active_job = i->json_name ();
180                                 /* Only start one job at once */
181                                 break;
182                         }
183                 }
184         }
185 }
186
187 void
188 JobManager::job_finished ()
189 {
190         {
191                 boost::mutex::scoped_lock lm (_mutex);
192                 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, optional<string>()));
193                 _last_active_job = optional<string>();
194         }
195
196         _empty_condition.notify_all ();
197 }
198
199 JobManager *
200 JobManager::instance ()
201 {
202         if (_instance == 0) {
203                 _instance = new JobManager ();
204                 _instance->start ();
205         }
206
207         return _instance;
208 }
209
210 void
211 JobManager::drop ()
212 {
213         delete _instance;
214         _instance = 0;
215 }
216
217 void
218 JobManager::analyse_audio (
219         shared_ptr<const Film> film,
220         shared_ptr<const Playlist> playlist,
221         bool from_zero,
222         boost::signals2::connection& connection,
223         function<void()> ready
224         )
225 {
226         {
227                 boost::mutex::scoped_lock lm (_mutex);
228
229                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
230                         shared_ptr<AnalyseAudioJob> a = dynamic_pointer_cast<AnalyseAudioJob> (i);
231                         if (a && a->path() == film->audio_analysis_path(playlist) && !i->finished_cancelled()) {
232                                 i->when_finished (connection, ready);
233                                 return;
234                         }
235                 }
236         }
237
238         shared_ptr<AnalyseAudioJob> job;
239
240         {
241                 boost::mutex::scoped_lock lm (_mutex);
242
243                 job.reset (new AnalyseAudioJob (film, playlist, from_zero));
244                 connection = job->Finished.connect (ready);
245                 _jobs.push_back (job);
246                 _empty_condition.notify_all ();
247         }
248
249         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
250 }
251
252
253 void
254 JobManager::analyse_subtitles (
255         shared_ptr<const Film> film,
256         shared_ptr<Content> content,
257         boost::signals2::connection& connection,
258         function<void()> ready
259         )
260 {
261         {
262                 boost::mutex::scoped_lock lm (_mutex);
263
264                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
265                         shared_ptr<AnalyseSubtitlesJob> a = dynamic_pointer_cast<AnalyseSubtitlesJob> (i);
266                         if (a && a->path() == film->subtitle_analysis_path(content)) {
267                                 i->when_finished (connection, ready);
268                                 return;
269                         }
270                 }
271         }
272
273         shared_ptr<AnalyseSubtitlesJob> job;
274
275         {
276                 boost::mutex::scoped_lock lm (_mutex);
277
278                 job.reset (new AnalyseSubtitlesJob(film, content));
279                 connection = job->Finished.connect (ready);
280                 _jobs.push_back (job);
281                 _empty_condition.notify_all ();
282         }
283
284         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
285 }
286
287
288 void
289 JobManager::increase_priority (shared_ptr<Job> job)
290 {
291         bool changed = false;
292
293         {
294                 boost::mutex::scoped_lock lm (_mutex);
295                 list<shared_ptr<Job> >::iterator last = _jobs.end ();
296                 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
297                         if (*i == job && last != _jobs.end()) {
298                                 swap (*i, *last);
299                                 changed = true;
300                                 break;
301                         }
302                         last = i;
303                 }
304         }
305
306         if (changed) {
307                 priority_changed ();
308         }
309 }
310
311 void
312 JobManager::priority_changed ()
313 {
314         {
315                 boost::mutex::scoped_lock lm (_mutex);
316
317                 bool first = true;
318                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
319                         if (first) {
320                                 if (i->is_new ()) {
321                                         i->start ();
322                                 } else if (i->paused_by_priority ()) {
323                                         i->resume ();
324                                 }
325                                 first = false;
326                         } else {
327                                 if (i->running ()) {
328                                         i->pause_by_priority ();
329                                 }
330                         }
331                 }
332         }
333
334         emit (boost::bind (boost::ref (JobsReordered)));
335 }
336
337 void
338 JobManager::decrease_priority (shared_ptr<Job> job)
339 {
340         bool changed = false;
341
342         {
343                 boost::mutex::scoped_lock lm (_mutex);
344                 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
345                         list<shared_ptr<Job> >::iterator next = i;
346                         ++next;
347                         if (*i == job && next != _jobs.end()) {
348                                 swap (*i, *next);
349                                 changed = true;
350                                 break;
351                         }
352                 }
353         }
354
355         if (changed) {
356                 priority_changed ();
357         }
358 }
359
360 void
361 JobManager::pause ()
362 {
363         boost::mutex::scoped_lock lm (_mutex);
364
365         if (_paused) {
366                 return;
367         }
368
369         BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
370                 if (i->pause_by_user()) {
371                         _paused_job = i;
372                 }
373         }
374
375         _paused = true;
376 }
377
378 void
379 JobManager::resume ()
380 {
381         boost::mutex::scoped_lock lm (_mutex);
382         if (!_paused) {
383                 return;
384         }
385
386         if (_paused_job) {
387                 _paused_job->resume ();
388         }
389
390         _paused_job.reset ();
391         _paused = false;
392 }