Simplify and fix job scheduler, especially with respect to the priority system.
[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                 if (_terminate) {
163                         break;
164                 }
165
166                 bool have_running = false;
167                 for (auto i: _jobs) {
168                         if (have_running && i->running()) {
169                                 i->pause_by_priority();
170                         } else if (!have_running && (i->is_new() || i->paused_by_priority())) {
171                                 if (i->is_new()) {
172                                         _connections.push_back (i->FinishedImmediate.connect(bind(&JobManager::job_finished, this)));
173                                         i->start ();
174                                 } else {
175                                         i->resume ();
176                                 }
177                                 emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, i->json_name()));
178                                 _last_active_job = i->json_name ();
179                                 have_running = true;
180                         } else if (!have_running && i->running()) {
181                                 have_running = true;
182                         }
183                 }
184
185                 _empty_condition.wait (lm);
186         }
187 }
188
189
190 void
191 JobManager::job_finished ()
192 {
193         {
194                 boost::mutex::scoped_lock lm (_mutex);
195                 emit (boost::bind(boost::ref (ActiveJobsChanged), _last_active_job, optional<string>()));
196                 _last_active_job = optional<string>();
197         }
198
199         _empty_condition.notify_all ();
200 }
201
202
203 JobManager *
204 JobManager::instance ()
205 {
206         if (!_instance) {
207                 _instance = new JobManager ();
208                 _instance->start ();
209         }
210
211         return _instance;
212 }
213
214
215 void
216 JobManager::drop ()
217 {
218         delete _instance;
219         _instance = nullptr;
220 }
221
222
223 void
224 JobManager::analyse_audio (
225         shared_ptr<const Film> film,
226         shared_ptr<const Playlist> playlist,
227         bool from_zero,
228         boost::signals2::connection& connection,
229         function<void()> ready
230         )
231 {
232         {
233                 boost::mutex::scoped_lock lm (_mutex);
234
235                 for (auto i: _jobs) {
236                         auto a = dynamic_pointer_cast<AnalyseAudioJob> (i);
237                         if (a && a->path() == film->audio_analysis_path(playlist) && !i->finished_cancelled()) {
238                                 i->when_finished (connection, ready);
239                                 return;
240                         }
241                 }
242         }
243
244         shared_ptr<AnalyseAudioJob> job;
245
246         {
247                 boost::mutex::scoped_lock lm (_mutex);
248
249                 job = make_shared<AnalyseAudioJob> (film, playlist, from_zero);
250                 connection = job->Finished.connect (ready);
251                 _jobs.push_back (job);
252                 _empty_condition.notify_all ();
253         }
254
255         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
256 }
257
258
259 void
260 JobManager::analyse_subtitles (
261         shared_ptr<const Film> film,
262         shared_ptr<Content> content,
263         boost::signals2::connection& connection,
264         function<void()> ready
265         )
266 {
267         {
268                 boost::mutex::scoped_lock lm (_mutex);
269
270                 for (auto i: _jobs) {
271                         auto a = dynamic_pointer_cast<AnalyseSubtitlesJob> (i);
272                         if (a && a->path() == film->subtitle_analysis_path(content)) {
273                                 i->when_finished (connection, ready);
274                                 return;
275                         }
276                 }
277         }
278
279         shared_ptr<AnalyseSubtitlesJob> job;
280
281         {
282                 boost::mutex::scoped_lock lm (_mutex);
283
284                 job = make_shared<AnalyseSubtitlesJob>(film, content);
285                 connection = job->Finished.connect (ready);
286                 _jobs.push_back (job);
287                 _empty_condition.notify_all ();
288         }
289
290         emit (boost::bind(boost::ref(JobAdded), weak_ptr<Job>(job)));
291 }
292
293
294 void
295 JobManager::increase_priority (shared_ptr<Job> job)
296 {
297         bool changed = false;
298
299         {
300                 boost::mutex::scoped_lock lm (_mutex);
301                 auto last = _jobs.end ();
302                 for (auto i = _jobs.begin(); i != _jobs.end(); ++i) {
303                         if (*i == job && last != _jobs.end()) {
304                                 swap (*i, *last);
305                                 changed = true;
306                                 break;
307                         }
308                         last = i;
309                 }
310         }
311
312         if (changed) {
313                 _empty_condition.notify_all ();
314                 emit (boost::bind(boost::ref(JobsReordered)));
315         }
316 }
317
318
319 void
320 JobManager::decrease_priority (shared_ptr<Job> job)
321 {
322         bool changed = false;
323
324         {
325                 boost::mutex::scoped_lock lm (_mutex);
326                 for (auto i = _jobs.begin(); i != _jobs.end(); ++i) {
327                         auto next = i;
328                         ++next;
329                         if (*i == job && next != _jobs.end()) {
330                                 swap (*i, *next);
331                                 changed = true;
332                                 break;
333                         }
334                 }
335         }
336
337         if (changed) {
338                 _empty_condition.notify_all ();
339                 emit (boost::bind(boost::ref(JobsReordered)));
340         }
341 }
342
343
344 void
345 JobManager::pause ()
346 {
347         boost::mutex::scoped_lock lm (_mutex);
348
349         if (_paused) {
350                 return;
351         }
352
353         for (auto i: _jobs) {
354                 if (i->pause_by_user()) {
355                         _paused_job = i;
356                 }
357         }
358
359         _paused = true;
360 }
361
362
363 void
364 JobManager::resume ()
365 {
366         boost::mutex::scoped_lock lm (_mutex);
367         if (!_paused) {
368                 return;
369         }
370
371         if (_paused_job) {
372                 _paused_job->resume ();
373         }
374
375         _paused_job.reset ();
376         _paused = false;
377 }