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