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