Make out-of-memory error message a bit more informative.
[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 "job.h"
25 #include "util.h"
26 #include "cross.h"
27 #include "exceptions.h"
28 #include "film.h"
29 #include "log.h"
30 #include "compose.hpp"
31 #include <dcp/exceptions.h>
32 #include <boost/thread.hpp>
33 #include <boost/filesystem.hpp>
34 #include <iostream>
35
36 #include "i18n.h"
37
38 using std::string;
39 using std::list;
40 using std::cout;
41 using boost::shared_ptr;
42 using boost::optional;
43 using boost::function;
44
45 #define LOG_ERROR_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_ERROR);
46
47 Job::Job (shared_ptr<const Film> film)
48         : _film (film)
49         , _thread (0)
50         , _state (NEW)
51         , _start_time (0)
52         , _progress (0)
53         , _ran_for (0)
54 {
55
56 }
57
58 Job::~Job ()
59 {
60         if (_thread) {
61                 _thread->interrupt ();
62                 _thread->join ();
63         }
64
65         delete _thread;
66 }
67
68 /** Start the job in a separate thread, returning immediately */
69 void
70 Job::start ()
71 {
72         set_state (RUNNING);
73         _start_time = time (0);
74         _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
75 }
76
77 /** A wrapper for the ::run() method to catch exceptions */
78 void
79 Job::run_wrapper ()
80 {
81         try {
82
83                 run ();
84
85         } catch (dcp::FileError& e) {
86
87                 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
88
89                 try {
90                         boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
91                         if (s.available < pow (1024, 3)) {
92                                 m += N_("\n\n");
93                                 m += _("The drive that the film is stored on is low in disc space.  Free some more space and try again.");
94                         }
95                 } catch (...) {
96
97                 }
98
99                 set_error (e.what(), m);
100                 set_progress (1);
101                 set_state (FINISHED_ERROR);
102
103         } catch (OpenFileError& e) {
104
105                 set_error (
106                         String::compose (_("Could not open %1"), e.file().string()),
107                         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())
108                         );
109
110                 set_progress (1);
111                 set_state (FINISHED_ERROR);
112
113         } catch (boost::filesystem::filesystem_error& e) {
114
115                 if (e.code() == boost::system::errc::no_such_file_or_directory) {
116                         set_error (
117                                 String::compose (_("Could not open %1"), e.path1().string ()),
118                                 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())
119                                 );
120                 } else {
121                         set_error (
122                                 e.what (),
123                                 string (_("It is not known what caused this error.")) + "  " + REPORT_PROBLEM
124                                 );
125                 }
126
127                 set_progress (1);
128                 set_state (FINISHED_ERROR);
129
130         } catch (boost::thread_interrupted &) {
131
132                 set_state (FINISHED_CANCELLED);
133
134         } catch (std::bad_alloc& e) {
135
136                 set_error (_("Out of memory"), _("There was not enough memory to do this.  If you are running a 32-bit operating system try reducing the number of encoding threads in the General tab of Preferences."));
137                 set_progress (1);
138                 set_state (FINISHED_ERROR);
139
140         } catch (std::exception& e) {
141
142                 set_error (
143                         e.what (),
144                         string (_("It is not known what caused this error.")) + "  " + REPORT_PROBLEM
145                         );
146
147                 set_progress (1);
148                 set_state (FINISHED_ERROR);
149
150         } catch (...) {
151
152                 set_error (
153                         _("Unknown error"),
154                         string (_("It is not known what caused this error.")) + "  " + REPORT_PROBLEM
155                         );
156
157                 set_progress (1);
158                 set_state (FINISHED_ERROR);
159         }
160 }
161
162 /** @return true if this job is new (ie has not started running) */
163 bool
164 Job::is_new () const
165 {
166         boost::mutex::scoped_lock lm (_state_mutex);
167         return _state == NEW;
168 }
169
170 /** @return true if the job is running */
171 bool
172 Job::running () const
173 {
174         boost::mutex::scoped_lock lm (_state_mutex);
175         return _state == RUNNING;
176 }
177
178 /** @return true if the job has finished (either successfully or unsuccessfully) */
179 bool
180 Job::finished () const
181 {
182         boost::mutex::scoped_lock lm (_state_mutex);
183         return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
184 }
185
186 /** @return true if the job has finished successfully */
187 bool
188 Job::finished_ok () const
189 {
190         boost::mutex::scoped_lock lm (_state_mutex);
191         return _state == FINISHED_OK;
192 }
193
194 /** @return true if the job has finished unsuccessfully */
195 bool
196 Job::finished_in_error () const
197 {
198         boost::mutex::scoped_lock lm (_state_mutex);
199         return _state == FINISHED_ERROR;
200 }
201
202 bool
203 Job::finished_cancelled () const
204 {
205         boost::mutex::scoped_lock lm (_state_mutex);
206         return _state == FINISHED_CANCELLED;
207 }
208
209 bool
210 Job::paused () const
211 {
212         boost::mutex::scoped_lock lm (_state_mutex);
213         return _state == PAUSED;
214 }
215
216 /** Set the state of this job.
217  *  @param s New state.
218  */
219 void
220 Job::set_state (State s)
221 {
222         bool finished = false;
223
224         {
225                 boost::mutex::scoped_lock lm (_state_mutex);
226                 _state = s;
227
228                 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
229                         _ran_for = elapsed_time ();
230                         finished = true;
231                         _sub_name.clear ();
232                 }
233         }
234
235         if (finished) {
236                 emit (boost::bind (boost::ref (Finished)));
237         }
238 }
239
240 /** @return DCPTime (in seconds) that this sub-job has been running */
241 int
242 Job::elapsed_time () const
243 {
244         if (_start_time == 0) {
245                 return 0;
246         }
247
248         return time (0) - _start_time;
249 }
250
251 /** Set the progress of the current part of the job.
252  *  @param p Progress (from 0 to 1)
253  */
254 void
255 Job::set_progress (float p, bool force)
256 {
257         if (!force && fabs (p - progress().get_value_or(0)) < 0.01) {
258                 /* Calm excessive progress reporting */
259                 return;
260         }
261
262         set_progress_common (p);
263 }
264
265 void
266 Job::set_progress_common (optional<float> p)
267 {
268         boost::mutex::scoped_lock lm (_progress_mutex);
269         _progress = p;
270         boost::this_thread::interruption_point ();
271
272         boost::mutex::scoped_lock lm2 (_state_mutex);
273         while (_state == PAUSED) {
274                 _pause_changed.wait (lm2);
275         }
276
277         lm.unlock ();
278         lm2.unlock ();
279
280         emit (boost::bind (boost::ref (Progress)));
281 }
282
283 /** @return fractional progress of the current sub-job, if known */
284 optional<float>
285 Job::progress () const
286 {
287         boost::mutex::scoped_lock lm (_progress_mutex);
288         return _progress;
289 }
290
291 void
292 Job::sub (string n)
293 {
294         {
295                 boost::mutex::scoped_lock lm (_progress_mutex);
296                 _sub_name = n;
297         }
298
299         set_progress (0, true);
300 }
301
302 string
303 Job::error_details () const
304 {
305         boost::mutex::scoped_lock lm (_state_mutex);
306         return _error_details;
307 }
308
309 /** @return A summary of any error that the job has generated */
310 string
311 Job::error_summary () const
312 {
313         boost::mutex::scoped_lock lm (_state_mutex);
314         return _error_summary;
315 }
316
317 /** Set the current error string.
318  *  @param e New error string.
319  */
320 void
321 Job::set_error (string s, string d)
322 {
323         LOG_ERROR_NC (s);
324         LOG_ERROR_NC (d);
325
326         _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), Log::TYPE_ERROR);
327         boost::mutex::scoped_lock lm (_state_mutex);
328         _error_summary = s;
329         _error_details = d;
330 }
331
332 /** Say that this job's progress will be unknown until further notice */
333 void
334 Job::set_progress_unknown ()
335 {
336         set_progress_common (optional<float> ());
337 }
338
339 /** @return Human-readable status of this job */
340 string
341 Job::status () const
342 {
343         optional<float> p = progress ();
344         int const t = elapsed_time ();
345         int const r = remaining_time ();
346
347         SafeStringStream s;
348         if (!finished () && p) {
349                 int pc = lrintf (p.get() * 100);
350                 if (pc == 100) {
351                         /* 100% makes it sound like we've finished when we haven't */
352                         pc = 99;
353                 }
354
355                 s << pc << N_("%");
356
357                 if (t > 10 && r > 0) {
358                         /// TRANSLATORS: remaining here follows an amount of time that is remaining
359                         /// on an operation.
360                         s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
361                 }
362         } else if (finished_ok ()) {
363                 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
364         } else if (finished_in_error ()) {
365                 s << String::compose (_("Error: %1"), error_summary ());
366         } else if (finished_cancelled ()) {
367                 s << _("Cancelled");
368         }
369
370         return s.str ();
371 }
372
373 string
374 Job::json_status () const
375 {
376         boost::mutex::scoped_lock lm (_state_mutex);
377
378         switch (_state) {
379         case NEW:
380                 return N_("new");
381         case RUNNING:
382                 return N_("running");
383         case PAUSED:
384                 return N_("paused");
385         case FINISHED_OK:
386                 return N_("finished_ok");
387         case FINISHED_ERROR:
388                 return N_("finished_error");
389         case FINISHED_CANCELLED:
390                 return N_("finished_cancelled");
391         }
392
393         return "";
394 }
395
396 /** @return An estimate of the remaining time for this sub-job, in seconds */
397 int
398 Job::remaining_time () const
399 {
400         if (progress().get_value_or(0) == 0) {
401                 return elapsed_time ();
402         }
403
404         return elapsed_time() / progress().get() - elapsed_time();
405 }
406
407 void
408 Job::cancel ()
409 {
410         if (!_thread) {
411                 return;
412         }
413
414         if (paused ()) {
415                 resume ();
416         }
417
418         _thread->interrupt ();
419         _thread->join ();
420 }
421
422 void
423 Job::pause ()
424 {
425         if (running ()) {
426                 set_state (PAUSED);
427                 _pause_changed.notify_all ();
428         }
429 }
430
431 void
432 Job::resume ()
433 {
434         if (paused ()) {
435                 set_state (RUNNING);
436                 _pause_changed.notify_all ();
437         }
438 }
439
440 void
441 Job::when_finished (boost::signals2::connection& connection, function<void()> finished)
442 {
443         boost::mutex::scoped_lock lm (_state_mutex);
444         if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
445                 finished ();
446         } else {
447                 connection = Finished.connect (finished);
448         }
449 }
450