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