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>
30 #include "ui_signaller.h"
36 using std::stringstream;
37 using boost::shared_ptr;
39 Job::Job (shared_ptr<const Film> f)
44 , _progress_unknown (false)
50 /** Start the job in a separate thread, returning immediately */
55 _start_time = time (0);
56 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
59 /** A wrapper for the ::run() method to catch exceptions */
67 } catch (libdcp::FileError& e) {
70 set_state (FINISHED_ERROR);
72 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
75 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
76 if (s.available < pow (1024, 3)) {
78 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
84 set_error (e.what(), m);
86 } catch (boost::thread_interrupted &) {
88 set_state (FINISHED_CANCELLED);
90 } catch (std::exception& e) {
93 set_state (FINISHED_ERROR);
96 _("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)")
102 set_state (FINISHED_ERROR);
105 _("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)")
111 /** @return true if this job is new (ie has not started running) */
115 boost::mutex::scoped_lock lm (_state_mutex);
116 return _state == NEW;
119 /** @return true if the job is running */
121 Job::running () const
123 boost::mutex::scoped_lock lm (_state_mutex);
124 return _state == RUNNING;
127 /** @return true if the job has finished (either successfully or unsuccessfully) */
129 Job::finished () const
131 boost::mutex::scoped_lock lm (_state_mutex);
132 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
135 /** @return true if the job has finished successfully */
137 Job::finished_ok () const
139 boost::mutex::scoped_lock lm (_state_mutex);
140 return _state == FINISHED_OK;
143 /** @return true if the job has finished unsuccessfully */
145 Job::finished_in_error () const
147 boost::mutex::scoped_lock lm (_state_mutex);
148 return _state == FINISHED_ERROR;
152 Job::finished_cancelled () const
154 boost::mutex::scoped_lock lm (_state_mutex);
155 return _state == FINISHED_CANCELLED;
161 boost::mutex::scoped_lock lm (_state_mutex);
162 return _state == PAUSED;
165 /** Set the state of this job.
166 * @param s New state.
169 Job::set_state (State s)
171 boost::mutex::scoped_lock lm (_state_mutex);
174 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
175 _ran_for = elapsed_time ();
177 ui_signaller->emit (boost::bind (boost::ref (Finished)));
182 /** @return Time (in seconds) that this job has been running */
184 Job::elapsed_time () const
186 if (_start_time == 0) {
190 return time (0) - _start_time;
193 /** Set the progress of the current part of the job.
194 * @param p Progress (from 0 to 1)
197 Job::set_progress (float p)
199 boost::mutex::scoped_lock lm (_progress_mutex);
200 _progress_unknown = false;
201 _stack.back().normalised = p;
202 boost::this_thread::interruption_point ();
209 /** @return fractional overall progress, or -1 if not known */
211 Job::overall_progress () const
213 boost::mutex::scoped_lock lm (_progress_mutex);
214 if (_progress_unknown) {
220 for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
221 factor *= i->allocation;
222 overall += i->normalised * factor;
232 /** Ascend up one level in terms of progress reporting; see descend() */
236 boost::mutex::scoped_lock lm (_progress_mutex);
238 assert (!_stack.empty ());
239 float const a = _stack.back().allocation;
241 _stack.back().normalised += a;
244 /** Descend down one level in terms of progress reporting; e.g. if
245 * there is a task which is split up into N subtasks, each of which
246 * report their progress from 0 to 100%, call descend() before executing
247 * each subtask, and ascend() afterwards to ensure that overall progress
248 * is reported correctly.
250 * @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
253 Job::descend (float a)
255 boost::mutex::scoped_lock lm (_progress_mutex);
256 _stack.push_back (Level (a));
260 Job::error_details () const
262 boost::mutex::scoped_lock lm (_state_mutex);
263 return _error_details;
266 /** @return A summary of any error that the job has generated */
268 Job::error_summary () const
270 boost::mutex::scoped_lock lm (_state_mutex);
271 return _error_summary;
274 /** Set the current error string.
275 * @param e New error string.
278 Job::set_error (string s, string d)
280 boost::mutex::scoped_lock lm (_state_mutex);
285 /** Say that this job's progress will be unknown until further notice */
287 Job::set_progress_unknown ()
289 boost::mutex::scoped_lock lm (_progress_mutex);
290 _progress_unknown = true;
293 /** @return Human-readable status of this job */
297 float const p = overall_progress ();
298 int const t = elapsed_time ();
299 int const r = remaining_time ();
301 int pc = rint (p * 100);
303 /* 100% makes it sound like we've finished when we haven't */
310 if (p >= 0 && t > 10 && r > 0) {
311 /// TRANSLATORS: remaining here follows an amount of time that is remaining
313 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
315 } else if (finished_ok ()) {
316 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
317 } else if (finished_in_error ()) {
318 s << String::compose (_("Error (%1)"), error_summary());
319 } else if (finished_cancelled ()) {
326 /** @return An estimate of the remaining time for this job, in seconds */
328 Job::remaining_time () const
330 return elapsed_time() / overall_progress() - elapsed_time();
340 _thread->interrupt ();