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