Try to not start jobs if a dependant fails.
[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 namespace std;
31 using namespace boost;
32
33 /** @param s FilmState for the film that we are operating on.
34  *  @param o Options.
35  *  @param l A log that we can write to.
36  */
37 Job::Job (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l, shared_ptr<Job> req)
38         : _fs (s)
39         , _opt (o)
40         , _log (l)
41         , _required (req)
42         , _state (NEW)
43         , _start_time (0)
44         , _progress_unknown (false)
45         , _ran_for (0)
46 {
47         assert (_log);
48         
49         descend (1);
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         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                 set_error (String::compose ("%1 (%2)", e.what(), filesystem::path (e.filename()).leaf()));
74                 
75         } catch (std::exception& e) {
76
77                 set_progress (1);
78                 set_state (FINISHED_ERROR);
79                 set_error (e.what ());
80
81         }
82 }
83
84 bool
85 Job::is_new () const
86 {
87         boost::mutex::scoped_lock lm (_state_mutex);
88         return _state == NEW;
89 }
90
91 /** @return true if the job is running */
92 bool
93 Job::running () const
94 {
95         boost::mutex::scoped_lock lm (_state_mutex);
96         return _state == RUNNING;
97 }
98
99 /** @return true if the job has finished (either successfully or unsuccessfully) */
100 bool
101 Job::finished () const
102 {
103         boost::mutex::scoped_lock lm (_state_mutex);
104         return _state == FINISHED_OK || _state == FINISHED_ERROR;
105 }
106
107 /** @return true if the job has finished successfully */
108 bool
109 Job::finished_ok () const
110 {
111         boost::mutex::scoped_lock lm (_state_mutex);
112         return _state == FINISHED_OK;
113 }
114
115 /** @return true if the job has finished unsuccessfully */
116 bool
117 Job::finished_in_error () const
118 {
119         boost::mutex::scoped_lock lm (_state_mutex);
120         return _state == FINISHED_ERROR;
121 }
122
123 /** Set the state of this job.
124  *  @param s New state.
125  */
126 void
127 Job::set_state (State s)
128 {
129         boost::mutex::scoped_lock lm (_state_mutex);
130         _state = s;
131
132         if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
133                 _ran_for = elapsed_time ();
134         }
135 }
136
137 /** A hack to work around our lack of cross-thread
138  *  signalling; this emits Finished, and listeners
139  *  assume that it will be emitted in the GUI thread,
140  *  so this method must be called from the GUI thread.
141  */
142 void
143 Job::emit_finished ()
144 {
145         Finished ();
146 }
147
148 /** @return Time (in seconds) that this job has been running */
149 int
150 Job::elapsed_time () const
151 {
152         if (_start_time == 0) {
153                 return 0;
154         }
155         
156         return time (0) - _start_time;
157 }
158
159 /** Set the progress of the current part of the job.
160  *  @param p Progress (from 0 to 1)
161  */
162 void
163 Job::set_progress (float p)
164 {
165         boost::mutex::scoped_lock lm (_progress_mutex);
166         _stack.back().normalised = p;
167 }
168
169 /** @return fractional overall progress, or -1 if not known */
170 float
171 Job::overall_progress () const
172 {
173         boost::mutex::scoped_lock lm (_progress_mutex);
174         if (_progress_unknown) {
175                 return -1;
176         }
177
178         float overall = 0;
179         float factor = 1;
180         for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
181                 factor *= i->allocation;
182                 overall += i->normalised * factor;
183         }
184
185         if (overall > 1) {
186                 overall = 1;
187         }
188         
189         return overall;
190 }
191
192 /** Ascend up one level in terms of progress reporting; see descend() */
193 void
194 Job::ascend ()
195 {
196         boost::mutex::scoped_lock lm (_progress_mutex);
197         
198         assert (!_stack.empty ());
199         float const a = _stack.back().allocation;
200         _stack.pop_back ();
201         _stack.back().normalised += a;
202 }
203
204 /** Descend down one level in terms of progress reporting; e.g. if
205  *  there is a task which is split up into N subtasks, each of which
206  *  report their progress from 0 to 100%, call descend() before executing
207  *  each subtask, and ascend() afterwards to ensure that overall progress
208  *  is reported correctly.
209  *
210  *  @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
211  */
212 void
213 Job::descend (float a)
214 {
215         boost::mutex::scoped_lock lm (_progress_mutex);
216         _stack.push_back (Level (a));
217 }
218
219 /** @return Any error string that the job has generated */
220 string
221 Job::error () const
222 {
223         boost::mutex::scoped_lock lm (_state_mutex);
224         return _error;
225 }
226
227 /** Set the current error string.
228  *  @param e New error string.
229  */
230 void
231 Job::set_error (string e)
232 {
233         boost::mutex::scoped_lock lm (_state_mutex);
234         _error = e;
235 }
236
237 /** Say that this job's progress will always be unknown */
238 void
239 Job::set_progress_unknown ()
240 {
241         boost::mutex::scoped_lock lm (_progress_mutex);
242         _progress_unknown = true;
243 }
244
245 /** @return Human-readable status of this job */
246 string
247 Job::status () const
248 {
249         float const p = overall_progress ();
250         int const t = elapsed_time ();
251         int const r = remaining_time ();
252         
253         stringstream s;
254         if (!finished () && p >= 0 && t > 10 && r > 0) {
255                 s << rint (p * 100) << "%; " << seconds_to_approximate_hms (r) << " remaining";
256         } else if (!finished () && (t <= 10 || r == 0)) {
257                 s << rint (p * 100) << "%";
258         } else if (finished_ok ()) {
259                 s << "OK (ran for " << seconds_to_hms (_ran_for) << ")";
260         } else if (finished_in_error ()) {
261                 s << "Error (" << error() << ")";
262         }
263
264         return s.str ();
265 }
266
267 /** @return An estimate of the remaining time for this job, in seconds */
268 int
269 Job::remaining_time () const
270 {
271         return elapsed_time() / overall_progress() - elapsed_time();
272 }