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