Rename UISignaller -> SignalManager.
[dcpomatic.git] / src / lib / job.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file src/job.cc
21  *  @brief A parent class to represent long-running tasks which are run in their own thread.
22  */
23
24 #include <boost/thread.hpp>
25 #include <boost/filesystem.hpp>
26 #include <dcp/exceptions.h>
27 #include "job.h"
28 #include "util.h"
29 #include "cross.h"
30 #include "exceptions.h"
31 #include "film.h"
32 #include "log.h"
33
34 #include "i18n.h"
35
36 using std::string;
37 using std::list;
38 using std::cout;
39 using boost::shared_ptr;
40 using boost::optional;
41
42 #define LOG_ERROR_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_ERROR);
43
44 Job::Job (shared_ptr<const Film> f)
45         : _film (f)
46         , _thread (0)
47         , _state (NEW)
48         , _start_time (0)
49         , _progress (0)
50         , _ran_for (0)
51 {
52
53 }
54
55 /** Start the job in a separate thread, returning immediately */
56 void
57 Job::start ()
58 {
59         set_state (RUNNING);
60         _start_time = time (0);
61         _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
62 }
63
64 /** A wrapper for the ::run() method to catch exceptions */
65 void
66 Job::run_wrapper ()
67 {
68         try {
69
70                 run ();
71
72         } catch (dcp::FileError& e) {
73
74                 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
75
76                 try {
77                         boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
78                         if (s.available < pow (1024, 3)) {
79                                 m += N_("\n\n");
80                                 m += _("The drive that the film is stored on is low in disc space.  Free some more space and try again.");
81                         }
82                 } catch (...) {
83
84                 }
85
86                 set_error (e.what(), m);
87                 set_progress (1);
88                 set_state (FINISHED_ERROR);
89                 
90         } catch (OpenFileError& e) {
91
92                 set_error (
93                         String::compose (_("Could not open %1"), e.file().string()),
94                         String::compose (_("DCP-o-matic could not open the file %1.  Perhaps it does not exist or is in an unexpected format."), e.file().string())
95                         );
96
97                 set_progress (1);
98                 set_state (FINISHED_ERROR);
99
100         } catch (boost::thread_interrupted &) {
101
102                 set_state (FINISHED_CANCELLED);
103
104         } catch (std::bad_alloc& e) {
105
106                 set_error (_("Out of memory"), _("There was not enough memory to do this."));
107                 set_progress (1);
108                 set_state (FINISHED_ERROR);
109                 
110         } catch (std::exception& e) {
111
112                 set_error (
113                         e.what (),
114                         string (_("It is not known what caused this error.")) + "  " + REPORT_PROBLEM
115                         );
116
117                 set_progress (1);
118                 set_state (FINISHED_ERROR);
119                 
120         } catch (...) {
121
122                 set_error (
123                         _("Unknown error"),
124                         string (_("It is not known what caused this error.")) + "  " + REPORT_PROBLEM
125                         );
126
127                 set_progress (1);
128                 set_state (FINISHED_ERROR);
129         }
130 }
131
132 /** @return true if this job is new (ie has not started running) */
133 bool
134 Job::is_new () const
135 {
136         boost::mutex::scoped_lock lm (_state_mutex);
137         return _state == NEW;
138 }
139
140 /** @return true if the job is running */
141 bool
142 Job::running () const
143 {
144         boost::mutex::scoped_lock lm (_state_mutex);
145         return _state == RUNNING;
146 }
147
148 /** @return true if the job has finished (either successfully or unsuccessfully) */
149 bool
150 Job::finished () const
151 {
152         boost::mutex::scoped_lock lm (_state_mutex);
153         return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
154 }
155
156 /** @return true if the job has finished successfully */
157 bool
158 Job::finished_ok () const
159 {
160         boost::mutex::scoped_lock lm (_state_mutex);
161         return _state == FINISHED_OK;
162 }
163
164 /** @return true if the job has finished unsuccessfully */
165 bool
166 Job::finished_in_error () const
167 {
168         boost::mutex::scoped_lock lm (_state_mutex);
169         return _state == FINISHED_ERROR;
170 }
171
172 bool
173 Job::finished_cancelled () const
174 {
175         boost::mutex::scoped_lock lm (_state_mutex);
176         return _state == FINISHED_CANCELLED;
177 }
178
179 bool
180 Job::paused () const
181 {
182         boost::mutex::scoped_lock lm (_state_mutex);
183         return _state == PAUSED;
184 }
185         
186 /** Set the state of this job.
187  *  @param s New state.
188  */
189 void
190 Job::set_state (State s)
191 {
192         bool finished = false;
193
194         {
195                 boost::mutex::scoped_lock lm (_state_mutex);
196                 _state = s;
197
198                 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
199                         _ran_for = elapsed_time ();
200                         finished = true;
201                         _sub_name.clear ();
202                 }
203         }
204
205         if (finished) {
206                 emit (boost::bind (boost::ref (Finished)));
207         }       
208 }
209
210 /** @return DCPTime (in seconds) that this sub-job has been running */
211 int
212 Job::elapsed_time () const
213 {
214         if (_start_time == 0) {
215                 return 0;
216         }
217         
218         return time (0) - _start_time;
219 }
220
221 /** Set the progress of the current part of the job.
222  *  @param p Progress (from 0 to 1)
223  */
224 void
225 Job::set_progress (float p, bool force)
226 {
227         if (!force && fabs (p - progress().get_value_or(0)) < 0.01) {
228                 /* Calm excessive progress reporting */
229                 return;
230         }
231
232         boost::mutex::scoped_lock lm (_progress_mutex);
233         _progress = p;
234         boost::this_thread::interruption_point ();
235
236         boost::mutex::scoped_lock lm2 (_state_mutex);
237         while (_state == PAUSED) {
238                 _pause_changed.wait (lm2);
239         }
240
241         emit (boost::bind (boost::ref (Progress)));
242 }
243
244 /** @return fractional progress of the current sub-job, if known */
245 optional<float>
246 Job::progress () const
247 {
248         boost::mutex::scoped_lock lm (_progress_mutex);
249         return _progress;
250 }
251
252 void
253 Job::sub (string n)
254 {
255         {
256                 boost::mutex::scoped_lock lm (_progress_mutex);
257                 _sub_name = n;
258         }
259         
260         set_progress (0, true);
261 }
262
263 string
264 Job::error_details () const
265 {
266         boost::mutex::scoped_lock lm (_state_mutex);
267         return _error_details;
268 }
269
270 /** @return A summary of any error that the job has generated */
271 string
272 Job::error_summary () const
273 {
274         boost::mutex::scoped_lock lm (_state_mutex);
275         return _error_summary;
276 }
277
278 /** Set the current error string.
279  *  @param e New error string.
280  */
281 void
282 Job::set_error (string s, string d)
283 {
284         LOG_ERROR_NC (s);
285         LOG_ERROR_NC (d);
286         
287         _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), Log::TYPE_ERROR);
288         boost::mutex::scoped_lock lm (_state_mutex);
289         _error_summary = s;
290         _error_details = d;
291 }
292
293 /** Say that this job's progress will be unknown until further notice */
294 void
295 Job::set_progress_unknown ()
296 {
297         boost::mutex::scoped_lock lm (_progress_mutex);
298         _progress.reset ();
299         lm.unlock ();
300
301         emit (boost::bind (boost::ref (Progress)));
302 }
303
304 /** @return Human-readable status of this job */
305 string
306 Job::status () const
307 {
308         optional<float> p = progress ();
309         int const t = elapsed_time ();
310         int const r = remaining_time ();
311
312         SafeStringStream s;
313         if (!finished () && p) {
314                 int pc = rint (p.get() * 100);
315                 if (pc == 100) {
316                         /* 100% makes it sound like we've finished when we haven't */
317                         pc = 99;
318                 }
319                 
320                 s << pc << N_("%");
321                 
322                 if (t > 10 && r > 0) {
323                         /// TRANSLATORS: remaining here follows an amount of time that is remaining
324                         /// on an operation.
325                         s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
326                 }
327         } else if (finished_ok ()) {
328                 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
329         } else if (finished_in_error ()) {
330                 s << String::compose (_("Error (%1)"), error_summary ());
331         } else if (finished_cancelled ()) {
332                 s << _("Cancelled");
333         }
334
335         return s.str ();
336 }
337
338 string
339 Job::json_status () const
340 {
341         boost::mutex::scoped_lock lm (_state_mutex);
342
343         switch (_state) {
344         case NEW:
345                 return N_("new");
346         case RUNNING:
347                 return N_("running");
348         case PAUSED:
349                 return N_("paused");
350         case FINISHED_OK:
351                 return N_("finished_ok");
352         case FINISHED_ERROR:
353                 return N_("finished_error");
354         case FINISHED_CANCELLED:
355                 return N_("finished_cancelled");
356         }
357
358         return "";
359 }
360
361 /** @return An estimate of the remaining time for this sub-job, in seconds */
362 int
363 Job::remaining_time () const
364 {
365         if (progress().get_value_or(0) == 0) {
366                 return elapsed_time ();
367         }
368         
369         return elapsed_time() / progress().get() - elapsed_time();
370 }
371
372 void
373 Job::cancel ()
374 {
375         if (!_thread) {
376                 return;
377         }
378
379         _thread->interrupt ();
380         _thread->join ();
381 }
382
383 void
384 Job::pause ()
385 {
386         if (running ()) {
387                 set_state (PAUSED);
388                 _pause_changed.notify_all ();
389         }
390 }
391
392 void
393 Job::resume ()
394 {
395         if (paused ()) {
396                 set_state (RUNNING);
397                 _pause_changed.notify_all ();
398         }
399 }