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