Fix tests.
[dcpomatic.git] / src / lib / job.cc
1 /*
2     Copyright (C) 2012 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 <libdcp/exceptions.h>
27 #include "job.h"
28 #include "util.h"
29 #include "cross.h"
30 #include "ui_signaller.h"
31
32 #include "i18n.h"
33
34 using std::string;
35 using std::list;
36 using std::stringstream;
37 using boost::shared_ptr;
38
39 Job::Job (shared_ptr<const Film> f)
40         : _film (f)
41         , _thread (0)
42         , _state (NEW)
43         , _start_time (0)
44         , _progress_unknown (false)
45         , _ran_for (0)
46 {
47         descend (1);
48 }
49
50 /** Start the job in a separate thread, returning immediately */
51 void
52 Job::start ()
53 {
54         set_state (RUNNING);
55         _start_time = time (0);
56         _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
57 }
58
59 /** A wrapper for the ::run() method to catch exceptions */
60 void
61 Job::run_wrapper ()
62 {
63         try {
64
65                 run ();
66
67         } catch (libdcp::FileError& e) {
68                 
69                 set_progress (1);
70                 set_state (FINISHED_ERROR);
71                 
72                 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
73
74                 try {
75                         boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
76                         if (s.available < pow (1024, 3)) {
77                                 m += N_("\n\n");
78                                 m += _("The drive that the film is stored on is low in disc space.  Free some more space and try again.");
79                         }
80                 } catch (...) {
81
82                 }
83
84                 set_error (e.what(), m);
85
86         } catch (boost::thread_interrupted &) {
87
88                 set_state (FINISHED_CANCELLED);
89                 
90         } catch (std::exception& e) {
91
92                 set_progress (1);
93                 set_state (FINISHED_ERROR);
94                 set_error (
95                         e.what (),
96                         _("It is not known what caused this error.  The best idea is to report the problem to the DCP-o-matic mailing list (dcpomatic@carlh.net)")
97                         );
98
99         } catch (...) {
100
101                 set_progress (1);
102                 set_state (FINISHED_ERROR);
103                 set_error (
104                         _("Unknown error"),
105                         _("It is not known what caused this error.  The best idea is to report the problem to the DCP-o-matic mailing list (dcpomatic@carlh.net)")
106                         );
107
108         }
109 }
110
111 /** @return true if this job is new (ie has not started running) */
112 bool
113 Job::is_new () const
114 {
115         boost::mutex::scoped_lock lm (_state_mutex);
116         return _state == NEW;
117 }
118
119 /** @return true if the job is running */
120 bool
121 Job::running () const
122 {
123         boost::mutex::scoped_lock lm (_state_mutex);
124         return _state == RUNNING;
125 }
126
127 /** @return true if the job has finished (either successfully or unsuccessfully) */
128 bool
129 Job::finished () const
130 {
131         boost::mutex::scoped_lock lm (_state_mutex);
132         return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
133 }
134
135 /** @return true if the job has finished successfully */
136 bool
137 Job::finished_ok () const
138 {
139         boost::mutex::scoped_lock lm (_state_mutex);
140         return _state == FINISHED_OK;
141 }
142
143 /** @return true if the job has finished unsuccessfully */
144 bool
145 Job::finished_in_error () const
146 {
147         boost::mutex::scoped_lock lm (_state_mutex);
148         return _state == FINISHED_ERROR;
149 }
150
151 bool
152 Job::finished_cancelled () const
153 {
154         boost::mutex::scoped_lock lm (_state_mutex);
155         return _state == FINISHED_CANCELLED;
156 }
157
158 bool
159 Job::paused () const
160 {
161         boost::mutex::scoped_lock lm (_state_mutex);
162         return _state == PAUSED;
163 }
164         
165 /** Set the state of this job.
166  *  @param s New state.
167  */
168 void
169 Job::set_state (State s)
170 {
171         boost::mutex::scoped_lock lm (_state_mutex);
172         _state = s;
173
174         if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
175                 _ran_for = elapsed_time ();
176                 if (ui_signaller) {
177                         ui_signaller->emit (boost::bind (boost::ref (Finished)));
178                 }
179         }
180 }
181
182 /** @return Time (in seconds) that this job has been running */
183 int
184 Job::elapsed_time () const
185 {
186         if (_start_time == 0) {
187                 return 0;
188         }
189         
190         return time (0) - _start_time;
191 }
192
193 /** Set the progress of the current part of the job.
194  *  @param p Progress (from 0 to 1)
195  */
196 void
197 Job::set_progress (float p)
198 {
199         boost::mutex::scoped_lock lm (_progress_mutex);
200         _progress_unknown = false;
201         _stack.back().normalised = p;
202         boost::this_thread::interruption_point ();
203
204         if (paused ()) {
205                 dcpomatic_sleep (1);
206         }
207 }
208
209 /** @return fractional overall progress, or -1 if not known */
210 float
211 Job::overall_progress () const
212 {
213         boost::mutex::scoped_lock lm (_progress_mutex);
214         if (_progress_unknown) {
215                 return -1;
216         }
217
218         float overall = 0;
219         float factor = 1;
220         for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
221                 factor *= i->allocation;
222                 overall += i->normalised * factor;
223         }
224
225         if (overall > 1) {
226                 overall = 1;
227         }
228         
229         return overall;
230 }
231
232 /** Ascend up one level in terms of progress reporting; see descend() */
233 void
234 Job::ascend ()
235 {
236         boost::mutex::scoped_lock lm (_progress_mutex);
237         
238         assert (!_stack.empty ());
239         float const a = _stack.back().allocation;
240         _stack.pop_back ();
241         _stack.back().normalised += a;
242 }
243
244 /** Descend down one level in terms of progress reporting; e.g. if
245  *  there is a task which is split up into N subtasks, each of which
246  *  report their progress from 0 to 100%, call descend() before executing
247  *  each subtask, and ascend() afterwards to ensure that overall progress
248  *  is reported correctly.
249  *
250  *  @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
251  */
252 void
253 Job::descend (float a)
254 {
255         boost::mutex::scoped_lock lm (_progress_mutex);
256         _stack.push_back (Level (a));
257 }
258
259 string
260 Job::error_details () const
261 {
262         boost::mutex::scoped_lock lm (_state_mutex);
263         return _error_details;
264 }
265
266 /** @return A summary of any error that the job has generated */
267 string
268 Job::error_summary () const
269 {
270         boost::mutex::scoped_lock lm (_state_mutex);
271         return _error_summary;
272 }
273
274 /** Set the current error string.
275  *  @param e New error string.
276  */
277 void
278 Job::set_error (string s, string d)
279 {
280         boost::mutex::scoped_lock lm (_state_mutex);
281         _error_summary = s;
282         _error_details = d;
283 }
284
285 /** Say that this job's progress will be unknown until further notice */
286 void
287 Job::set_progress_unknown ()
288 {
289         boost::mutex::scoped_lock lm (_progress_mutex);
290         _progress_unknown = true;
291 }
292
293 /** @return Human-readable status of this job */
294 string
295 Job::status () const
296 {
297         float const p = overall_progress ();
298         int const t = elapsed_time ();
299         int const r = remaining_time ();
300
301         int pc = rint (p * 100);
302         if (pc == 100) {
303                 /* 100% makes it sound like we've finished when we haven't */
304                 pc = 99;
305         }
306
307         stringstream s;
308         if (!finished ()) {
309                 s << pc << N_("%");
310                 if (p >= 0 && t > 10 && r > 0) {
311                         /// TRANSLATORS: remaining here follows an amount of time that is remaining
312                         /// on an operation.
313                         s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
314                 }
315         } else if (finished_ok ()) {
316                 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
317         } else if (finished_in_error ()) {
318                 s << String::compose (_("Error (%1)"), error_summary());
319         } else if (finished_cancelled ()) {
320                 s << _("Cancelled");
321         }
322
323         return s.str ();
324 }
325
326 /** @return An estimate of the remaining time for this job, in seconds */
327 int
328 Job::remaining_time () const
329 {
330         return elapsed_time() / overall_progress() - elapsed_time();
331 }
332
333 void
334 Job::cancel ()
335 {
336         if (!_thread) {
337                 return;
338         }
339
340         _thread->interrupt ();
341         _thread->join ();
342 }
343
344 void
345 Job::pause ()
346 {
347         if (running ()) {
348                 set_state (PAUSED);
349         }
350 }
351
352 void
353 Job::resume ()
354 {
355         if (paused ()) {
356                 set_state (RUNNING);
357         }
358 }