Speed up content discovery:
[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 using boost::bind;
43
44 JobManager* JobManager::_instance = 0;
45
46 JobManager::JobManager ()
47         : _terminate (false)
48         , _paused (false)
49         , _scheduler (0)
50 {
51
52 }
53
54 void
55 JobManager::start ()
56 {
57         _scheduler = new 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_FOREACH (boost::signals2::connection& i, _connections) {
66                 i.disconnect ();
67         }
68
69         {
70                 boost::mutex::scoped_lock lm (_mutex);
71                 _terminate = true;
72         }
73
74         if (_scheduler) {
75                 /* Ideally this would be a DCPOMATIC_ASSERT(_scheduler->joinable()) but we
76                    can't throw exceptions from a destructor.
77                 */
78                 if (_scheduler->joinable ()) {
79                         _scheduler->join ();
80                 }
81         }
82
83         delete _scheduler;
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 shared_ptr<Job>
101 JobManager::add_after (shared_ptr<Job> after, shared_ptr<Job> j)
102 {
103         {
104                 boost::mutex::scoped_lock lm (_mutex);
105                 list<shared_ptr<Job> >::iterator i = find (_jobs.begin(), _jobs.end(), after);
106                 DCPOMATIC_ASSERT (i != _jobs.end());
107                 _jobs.insert (i, j);
108                 _empty_condition.notify_all ();
109         }
110
111         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (j)));
112
113         return j;
114 }
115
116 list<shared_ptr<Job> >
117 JobManager::get () const
118 {
119         boost::mutex::scoped_lock lm (_mutex);
120         return _jobs;
121 }
122
123 bool
124 JobManager::work_to_do () const
125 {
126         boost::mutex::scoped_lock lm (_mutex);
127         list<shared_ptr<Job> >::const_iterator i = _jobs.begin();
128         while (i != _jobs.end() && (*i)->finished()) {
129                 ++i;
130         }
131
132         return i != _jobs.end ();
133 }
134
135 bool
136 JobManager::errors () const
137 {
138         boost::mutex::scoped_lock lm (_mutex);
139         for (list<shared_ptr<Job> >::const_iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
140                 if ((*i)->finished_in_error ()) {
141                         return true;
142                 }
143         }
144
145         return false;
146 }
147
148 void
149 JobManager::scheduler ()
150 {
151         while (true) {
152
153                 boost::mutex::scoped_lock lm (_mutex);
154
155                 optional<string> active_job;
156
157                 while (true) {
158                         bool have_new = false;
159                         bool have_running = false;
160                         BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
161                                 if (i->running()) {
162                                         have_running = true;
163                                         active_job = i->json_name();
164                                 }
165                                 if (i->is_new()) {
166                                         have_new = true;
167                                 }
168                         }
169
170                         if ((!have_running && have_new) || _terminate) {
171                                 break;
172                         }
173
174                         _empty_condition.wait (lm);
175                 }
176
177                 if (_terminate) {
178                         break;
179                 }
180
181                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
182                         if (i->is_new()) {
183                                 _connections.push_back (i->FinishedImmediate.connect(bind(&JobManager::job_finished, this)));
184                                 i->start ();
185                                 /* Only start one job at once */
186                                 break;
187                         }
188                 }
189
190                 lm.unlock ();
191
192                 if (active_job != _last_active_job) {
193                         emit (boost::bind (boost::ref (ActiveJobsChanged), _last_active_job, active_job));
194                         _last_active_job = active_job;
195                 }
196         }
197 }
198
199 void
200 JobManager::job_finished ()
201 {
202         _empty_condition.notify_all ();
203 }
204
205 JobManager *
206 JobManager::instance ()
207 {
208         if (_instance == 0) {
209                 _instance = new JobManager ();
210                 _instance->start ();
211         }
212
213         return _instance;
214 }
215
216 void
217 JobManager::drop ()
218 {
219         delete _instance;
220         _instance = 0;
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                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
236                         shared_ptr<AnalyseAudioJob> a = dynamic_pointer_cast<AnalyseAudioJob> (i);
237                         if (a && a->playlist () == playlist) {
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.reset (new AnalyseAudioJob (film, playlist, from_zero));
250                 connection = job->Finished.connect (ready);
251                 _jobs.push_back (job);
252         }
253
254         emit (boost::bind (boost::ref (JobAdded), weak_ptr<Job> (job)));
255 }
256
257 void
258 JobManager::increase_priority (shared_ptr<Job> job)
259 {
260         bool changed = false;
261
262         {
263                 boost::mutex::scoped_lock lm (_mutex);
264                 list<shared_ptr<Job> >::iterator last = _jobs.end ();
265                 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
266                         if (*i == job && last != _jobs.end()) {
267                                 swap (*i, *last);
268                                 changed = true;
269                                 break;
270                         }
271                         last = i;
272                 }
273         }
274
275         if (changed) {
276                 priority_changed ();
277         }
278 }
279
280 void
281 JobManager::priority_changed ()
282 {
283         {
284                 boost::mutex::scoped_lock lm (_mutex);
285
286                 bool first = true;
287                 BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
288                         if (first) {
289                                 if (i->is_new ()) {
290                                         i->start ();
291                                 } else if (i->paused_by_priority ()) {
292                                         i->resume ();
293                                 }
294                                 first = false;
295                         } else {
296                                 if (i->running ()) {
297                                         i->pause_by_priority ();
298                                 }
299                         }
300                 }
301         }
302
303         emit (boost::bind (boost::ref (JobsReordered)));
304 }
305
306 void
307 JobManager::decrease_priority (shared_ptr<Job> job)
308 {
309         bool changed = false;
310
311         {
312                 boost::mutex::scoped_lock lm (_mutex);
313                 for (list<shared_ptr<Job> >::iterator i = _jobs.begin(); i != _jobs.end(); ++i) {
314                         list<shared_ptr<Job> >::iterator next = i;
315                         ++next;
316                         if (*i == job && next != _jobs.end()) {
317                                 swap (*i, *next);
318                                 changed = true;
319                                 break;
320                         }
321                 }
322         }
323
324         if (changed) {
325                 priority_changed ();
326         }
327 }
328
329 void
330 JobManager::pause ()
331 {
332         boost::mutex::scoped_lock lm (_mutex);
333
334         if (_paused) {
335                 return;
336         }
337
338         BOOST_FOREACH (shared_ptr<Job> i, _jobs) {
339                 if (i->pause_by_user()) {
340                         _paused_job = i;
341                 }
342         }
343
344         _paused = true;
345 }
346
347 void
348 JobManager::resume ()
349 {
350         boost::mutex::scoped_lock lm (_mutex);
351         if (!_paused) {
352                 return;
353         }
354
355         if (_paused_job) {
356                 _paused_job->resume ();
357         }
358
359         _paused_job.reset ();
360         _paused = false;
361 }