2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 * @brief A parent class to represent long-running tasks which are run in their own thread.
24 #include <boost/thread.hpp>
25 #include <boost/filesystem.hpp>
26 #include <libdcp/exceptions.h>
34 using std::stringstream;
35 using boost::shared_ptr;
37 Job::Job (shared_ptr<Film> f)
42 , _progress_unknown (false)
48 /** Start the job in a separate thread, returning immediately */
53 _start_time = time (0);
54 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
57 /** A wrapper for the ::run() method to catch exceptions */
65 } catch (libdcp::FileError& e) {
68 set_state (FINISHED_ERROR);
70 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
73 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
74 if (s.available < pow (1024, 3)) {
76 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
82 set_error (e.what(), m);
84 } catch (boost::thread_interrupted &) {
86 set_state (FINISHED_CANCELLED);
88 } catch (std::exception& e) {
91 set_state (FINISHED_ERROR);
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)")
100 set_state (FINISHED_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)")
109 /** @return true if this job is new (ie has not started running) */
113 boost::mutex::scoped_lock lm (_state_mutex);
114 return _state == NEW;
117 /** @return true if the job is running */
119 Job::running () const
121 boost::mutex::scoped_lock lm (_state_mutex);
122 return _state == RUNNING;
125 /** @return true if the job has finished (either successfully or unsuccessfully) */
127 Job::finished () const
129 boost::mutex::scoped_lock lm (_state_mutex);
130 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
133 /** @return true if the job has finished successfully */
135 Job::finished_ok () const
137 boost::mutex::scoped_lock lm (_state_mutex);
138 return _state == FINISHED_OK;
141 /** @return true if the job has finished unsuccessfully */
143 Job::finished_in_error () const
145 boost::mutex::scoped_lock lm (_state_mutex);
146 return _state == FINISHED_ERROR;
150 Job::finished_cancelled () const
152 boost::mutex::scoped_lock lm (_state_mutex);
153 return _state == FINISHED_CANCELLED;
156 /** Set the state of this job.
157 * @param s New state.
160 Job::set_state (State s)
162 boost::mutex::scoped_lock lm (_state_mutex);
165 if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
166 _ran_for = elapsed_time ();
170 /** @return Time (in seconds) that this job has been running */
172 Job::elapsed_time () const
174 if (_start_time == 0) {
178 return time (0) - _start_time;
181 /** Set the progress of the current part of the job.
182 * @param p Progress (from 0 to 1)
185 Job::set_progress (float p)
187 boost::mutex::scoped_lock lm (_progress_mutex);
188 _progress_unknown = false;
189 _stack.back().normalised = p;
190 boost::this_thread::interruption_point ();
193 /** @return fractional overall progress, or -1 if not known */
195 Job::overall_progress () const
197 boost::mutex::scoped_lock lm (_progress_mutex);
198 if (_progress_unknown) {
204 for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
205 factor *= i->allocation;
206 overall += i->normalised * factor;
216 /** Ascend up one level in terms of progress reporting; see descend() */
220 boost::mutex::scoped_lock lm (_progress_mutex);
222 assert (!_stack.empty ());
223 float const a = _stack.back().allocation;
225 _stack.back().normalised += a;
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.
234 * @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
237 Job::descend (float a)
239 boost::mutex::scoped_lock lm (_progress_mutex);
240 _stack.push_back (Level (a));
244 Job::error_details () const
246 boost::mutex::scoped_lock lm (_state_mutex);
247 return _error_details;
250 /** @return A summary of any error that the job has generated */
252 Job::error_summary () const
254 boost::mutex::scoped_lock lm (_state_mutex);
255 return _error_summary;
258 /** Set the current error string.
259 * @param e New error string.
262 Job::set_error (string s, string d)
264 boost::mutex::scoped_lock lm (_state_mutex);
269 /** Say that this job's progress will be unknown until further notice */
271 Job::set_progress_unknown ()
273 boost::mutex::scoped_lock lm (_progress_mutex);
274 _progress_unknown = true;
277 /** @return Human-readable status of this job */
281 float const p = overall_progress ();
282 int const t = elapsed_time ();
283 int const r = remaining_time ();
285 int pc = rint (p * 100);
287 /* 100% makes it sound like we've finished when we haven't */
294 if (p >= 0 && t > 10 && r > 0) {
295 /// TRANSLATORS: remaining here follows an amount of time that is remaining
297 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
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 ()) {
310 /** @return An estimate of the remaining time for this job, in seconds */
312 Job::remaining_time () const
314 return elapsed_time() / overall_progress() - elapsed_time();
324 _thread->interrupt ();