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