Improve error reporting, in particular from dcpomatic_create.
[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> film)
45         : _film (film)
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
88         } catch (OpenFileError& e) {
89
90                 set_error (
91                         String::compose (_("Could not open %1"), e.file().string()),
92                         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())
93                         );
94
95         } catch (boost::filesystem::filesystem_error& e) {
96
97                 if (e.code() == boost::system::errc::no_such_file_or_directory) {
98                         set_error (
99                                 String::compose (_("Could not open %1"), e.path1().string ()),
100                                 String::compose (_("DCP-o-matic could not open the file %1.  Perhaps it does not exist or is in an unexpected format."), e.path1().string())
101                                 );
102                 } else {
103                         set_error (
104                                 e.what (),
105                                 string (_("It is not known what caused this error.")) + "  " + REPORT_PROBLEM
106                                 );
107                 }
108
109         } catch (boost::thread_interrupted &) {
110
111                 set_state (FINISHED_CANCELLED);
112                 /* This is the only one that is not FINISHED_ERROR; need to return */
113                 return;
114
115         } catch (std::bad_alloc& e) {
116
117                 set_error (_("Out of memory"), _("There was not enough memory to do this."));
118
119         } catch (std::exception& e) {
120
121                 set_error (
122                         e.what (),
123                         string (_("It is not known what caused this error.")) + "  " + REPORT_PROBLEM
124                         );
125
126         } catch (...) {
127
128                 set_error (
129                         _("Unknown error"),
130                         string (_("It is not known what caused this error.")) + "  " + REPORT_PROBLEM
131                         );
132         }
133
134         set_progress (1);
135         set_state (FINISHED_ERROR);
136 }
137
138 /** @return true if this job is new (ie has not started running) */
139 bool
140 Job::is_new () const
141 {
142         boost::mutex::scoped_lock lm (_state_mutex);
143         return _state == NEW;
144 }
145
146 /** @return true if the job is running */
147 bool
148 Job::running () const
149 {
150         boost::mutex::scoped_lock lm (_state_mutex);
151         return _state == RUNNING;
152 }
153
154 /** @return true if the job has finished (either successfully or unsuccessfully) */
155 bool
156 Job::finished () const
157 {
158         boost::mutex::scoped_lock lm (_state_mutex);
159         return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
160 }
161
162 /** @return true if the job has finished successfully */
163 bool
164 Job::finished_ok () const
165 {
166         boost::mutex::scoped_lock lm (_state_mutex);
167         return _state == FINISHED_OK;
168 }
169
170 /** @return true if the job has finished unsuccessfully */
171 bool
172 Job::finished_in_error () const
173 {
174         boost::mutex::scoped_lock lm (_state_mutex);
175         return _state == FINISHED_ERROR;
176 }
177
178 bool
179 Job::finished_cancelled () const
180 {
181         boost::mutex::scoped_lock lm (_state_mutex);
182         return _state == FINISHED_CANCELLED;
183 }
184
185 bool
186 Job::paused () const
187 {
188         boost::mutex::scoped_lock lm (_state_mutex);
189         return _state == PAUSED;
190 }
191
192 /** Set the state of this job.
193  *  @param s New state.
194  */
195 void
196 Job::set_state (State s)
197 {
198         bool finished = false;
199
200         {
201                 boost::mutex::scoped_lock lm (_state_mutex);
202                 _state = s;
203
204                 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
205                         _ran_for = elapsed_time ();
206                         finished = true;
207                         _sub_name.clear ();
208                 }
209         }
210
211         if (finished) {
212                 emit (boost::bind (boost::ref (Finished)));
213         }
214 }
215
216 /** @return DCPTime (in seconds) that this sub-job has been running */
217 int
218 Job::elapsed_time () const
219 {
220         if (_start_time == 0) {
221                 return 0;
222         }
223
224         return time (0) - _start_time;
225 }
226
227 /** Set the progress of the current part of the job.
228  *  @param p Progress (from 0 to 1)
229  */
230 void
231 Job::set_progress (float p, bool force)
232 {
233         if (!force && fabs (p - progress().get_value_or(0)) < 0.01) {
234                 /* Calm excessive progress reporting */
235                 return;
236         }
237
238         boost::mutex::scoped_lock lm (_progress_mutex);
239         _progress = p;
240         boost::this_thread::interruption_point ();
241
242         boost::mutex::scoped_lock lm2 (_state_mutex);
243         while (_state == PAUSED) {
244                 _pause_changed.wait (lm2);
245         }
246
247         lm.unlock ();
248         lm2.unlock ();
249
250         emit (boost::bind (boost::ref (Progress)));
251 }
252
253 /** @return fractional progress of the current sub-job, if known */
254 optional<float>
255 Job::progress () const
256 {
257         boost::mutex::scoped_lock lm (_progress_mutex);
258         return _progress;
259 }
260
261 void
262 Job::sub (string n)
263 {
264         {
265                 boost::mutex::scoped_lock lm (_progress_mutex);
266                 _sub_name = n;
267         }
268
269         set_progress (0, true);
270 }
271
272 string
273 Job::error_details () const
274 {
275         boost::mutex::scoped_lock lm (_state_mutex);
276         return _error_details;
277 }
278
279 /** @return A summary of any error that the job has generated */
280 string
281 Job::error_summary () const
282 {
283         boost::mutex::scoped_lock lm (_state_mutex);
284         return _error_summary;
285 }
286
287 /** Set the current error string.
288  *  @param e New error string.
289  */
290 void
291 Job::set_error (string s, string d)
292 {
293         LOG_ERROR_NC (s);
294         LOG_ERROR_NC (d);
295
296         _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), Log::TYPE_ERROR);
297         boost::mutex::scoped_lock lm (_state_mutex);
298         _error_summary = s;
299         _error_details = d;
300 }
301
302 /** Say that this job's progress will be unknown until further notice */
303 void
304 Job::set_progress_unknown ()
305 {
306         boost::mutex::scoped_lock lm (_progress_mutex);
307         _progress.reset ();
308         lm.unlock ();
309
310         emit (boost::bind (boost::ref (Progress)));
311 }
312
313 /** @return Human-readable status of this job */
314 string
315 Job::status () const
316 {
317         optional<float> p = progress ();
318         int const t = elapsed_time ();
319         int const r = remaining_time ();
320
321         SafeStringStream s;
322         if (!finished () && p) {
323                 int pc = rint (p.get() * 100);
324                 if (pc == 100) {
325                         /* 100% makes it sound like we've finished when we haven't */
326                         pc = 99;
327                 }
328
329                 s << pc << N_("%");
330
331                 if (t > 10 && r > 0) {
332                         /// TRANSLATORS: remaining here follows an amount of time that is remaining
333                         /// on an operation.
334                         s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
335                 }
336         } else if (finished_ok ()) {
337                 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
338         } else if (finished_in_error ()) {
339                 s << String::compose (_("Error: %1"), error_summary ());
340         } else if (finished_cancelled ()) {
341                 s << _("Cancelled");
342         }
343
344         return s.str ();
345 }
346
347 string
348 Job::json_status () const
349 {
350         boost::mutex::scoped_lock lm (_state_mutex);
351
352         switch (_state) {
353         case NEW:
354                 return N_("new");
355         case RUNNING:
356                 return N_("running");
357         case PAUSED:
358                 return N_("paused");
359         case FINISHED_OK:
360                 return N_("finished_ok");
361         case FINISHED_ERROR:
362                 return N_("finished_error");
363         case FINISHED_CANCELLED:
364                 return N_("finished_cancelled");
365         }
366
367         return "";
368 }
369
370 /** @return An estimate of the remaining time for this sub-job, in seconds */
371 int
372 Job::remaining_time () const
373 {
374         if (progress().get_value_or(0) == 0) {
375                 return elapsed_time ();
376         }
377
378         return elapsed_time() / progress().get() - elapsed_time();
379 }
380
381 void
382 Job::cancel ()
383 {
384         if (!_thread) {
385                 return;
386         }
387
388         if (paused ()) {
389                 resume ();
390         }
391
392         _thread->interrupt ();
393         _thread->join ();
394 }
395
396 void
397 Job::pause ()
398 {
399         if (running ()) {
400                 set_state (PAUSED);
401                 _pause_changed.notify_all ();
402         }
403 }
404
405 void
406 Job::resume ()
407 {
408         if (paused ()) {
409                 set_state (RUNNING);
410                 _pause_changed.notify_all ();
411         }
412 }