Bump version
[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
30 #include "i18n.h"
31
32 using std::string;
33 using std::list;
34 using std::stringstream;
35 using boost::shared_ptr;
36
37 /** @param s Film that we are operating on.
38  */
39 Job::Job (shared_ptr<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 DVD-o-matic mailing list (dvdomatic@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 DVD-o-matic mailing list (dvdomatic@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 /** Set the state of this job.
159  *  @param s New state.
160  */
161 void
162 Job::set_state (State s)
163 {
164         boost::mutex::scoped_lock lm (_state_mutex);
165         _state = s;
166
167         if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
168                 _ran_for = elapsed_time ();
169         }
170 }
171
172 /** @return Time (in seconds) that this job has been running */
173 int
174 Job::elapsed_time () const
175 {
176         if (_start_time == 0) {
177                 return 0;
178         }
179         
180         return time (0) - _start_time;
181 }
182
183 /** Set the progress of the current part of the job.
184  *  @param p Progress (from 0 to 1)
185  */
186 void
187 Job::set_progress (float p)
188 {
189         boost::mutex::scoped_lock lm (_progress_mutex);
190         _progress_unknown = false;
191         _stack.back().normalised = p;
192         boost::this_thread::interruption_point ();
193 }
194
195 /** @return fractional overall progress, or -1 if not known */
196 float
197 Job::overall_progress () const
198 {
199         boost::mutex::scoped_lock lm (_progress_mutex);
200         if (_progress_unknown) {
201                 return -1;
202         }
203
204         float overall = 0;
205         float factor = 1;
206         for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
207                 factor *= i->allocation;
208                 overall += i->normalised * factor;
209         }
210
211         if (overall > 1) {
212                 overall = 1;
213         }
214         
215         return overall;
216 }
217
218 /** Ascend up one level in terms of progress reporting; see descend() */
219 void
220 Job::ascend ()
221 {
222         boost::mutex::scoped_lock lm (_progress_mutex);
223         
224         assert (!_stack.empty ());
225         float const a = _stack.back().allocation;
226         _stack.pop_back ();
227         _stack.back().normalised += a;
228 }
229
230 /** Descend down one level in terms of progress reporting; e.g. if
231  *  there is a task which is split up into N subtasks, each of which
232  *  report their progress from 0 to 100%, call descend() before executing
233  *  each subtask, and ascend() afterwards to ensure that overall progress
234  *  is reported correctly.
235  *
236  *  @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
237  */
238 void
239 Job::descend (float a)
240 {
241         boost::mutex::scoped_lock lm (_progress_mutex);
242         _stack.push_back (Level (a));
243 }
244
245 string
246 Job::error_details () const
247 {
248         boost::mutex::scoped_lock lm (_state_mutex);
249         return _error_details;
250 }
251
252 /** @return A summary of any error that the job has generated */
253 string
254 Job::error_summary () const
255 {
256         boost::mutex::scoped_lock lm (_state_mutex);
257         return _error_summary;
258 }
259
260 /** Set the current error string.
261  *  @param e New error string.
262  */
263 void
264 Job::set_error (string s, string d)
265 {
266         boost::mutex::scoped_lock lm (_state_mutex);
267         _error_summary = s;
268         _error_details = d;
269 }
270
271 /** Say that this job's progress will be unknown until further notice */
272 void
273 Job::set_progress_unknown ()
274 {
275         boost::mutex::scoped_lock lm (_progress_mutex);
276         _progress_unknown = true;
277 }
278
279 /** @return Human-readable status of this job */
280 string
281 Job::status () const
282 {
283         float const p = overall_progress ();
284         int const t = elapsed_time ();
285         int const r = remaining_time ();
286
287         int pc = rint (p * 100);
288         if (pc == 100) {
289                 /* 100% makes it sound like we've finished when we haven't */
290                 pc = 99;
291         }
292
293         stringstream s;
294         if (!finished ()) {
295                 s << pc << N_("%");
296                 if (p >= 0 && t > 10 && r > 0) {
297                         /// TRANSLATORS: remaining here follows an amount of time that is remaining
298                         /// on an operation.
299                         s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
300                 }
301         } else if (finished_ok ()) {
302                 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
303         } else if (finished_in_error ()) {
304                 s << String::compose (_("Error (%1)"), error_summary());
305         } else if (finished_cancelled ()) {
306                 s << _("Cancelled");
307         }
308
309         return s.str ();
310 }
311
312 /** @return An estimate of the remaining time for this job, in seconds */
313 int
314 Job::remaining_time () const
315 {
316         return elapsed_time() / overall_progress() - elapsed_time();
317 }
318
319 void
320 Job::cancel ()
321 {
322         if (!_thread) {
323                 return;
324         }
325
326         _thread->interrupt ();
327         _thread->join ();
328 }