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