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