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"
31 #include "exceptions.h"
38 using std::stringstream;
39 using boost::shared_ptr;
41 Job::Job (shared_ptr<const Film> f)
46 , _progress_unknown (false)
53 /** Start the job in a separate thread, returning immediately */
58 _start_time = time (0);
59 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
62 /** A wrapper for the ::run() method to catch exceptions */
70 } catch (libdcp::FileError& e) {
73 set_state (FINISHED_ERROR);
75 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
78 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
79 if (s.available < pow (1024, 3)) {
81 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
87 set_error (e.what(), m);
89 } catch (OpenFileError& e) {
92 set_state (FINISHED_ERROR);
95 String::compose (_("Could not open %1"), e.file().string()),
96 String::compose (_("DCP-o-matic could not open the file %1. Perhaps it does not exist or is in an unexpected format."), e.file().string())
99 } catch (boost::thread_interrupted &) {
101 set_state (FINISHED_CANCELLED);
103 } catch (std::exception& e) {
106 set_state (FINISHED_ERROR);
109 _("It is not known what caused this error. The best idea is to report the problem to the DCP-o-matic mailing list (carl@dcpomatic.com)")
115 set_state (FINISHED_ERROR);
118 _("It is not known what caused this error. The best idea is to report the problem to the DCP-o-matic mailing list (carl@dcpomatic.com)")
124 /** @return true if this job is new (ie has not started running) */
128 boost::mutex::scoped_lock lm (_state_mutex);
129 return _state == NEW;
132 /** @return true if the job is running */
134 Job::running () const
136 boost::mutex::scoped_lock lm (_state_mutex);
137 return _state == RUNNING;
140 /** @return true if the job has finished (either successfully or unsuccessfully) */
142 Job::finished () const
144 boost::mutex::scoped_lock lm (_state_mutex);
145 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
148 /** @return true if the job has finished successfully */
150 Job::finished_ok () const
152 boost::mutex::scoped_lock lm (_state_mutex);
153 return _state == FINISHED_OK;
156 /** @return true if the job has finished unsuccessfully */
158 Job::finished_in_error () const
160 boost::mutex::scoped_lock lm (_state_mutex);
161 return _state == FINISHED_ERROR;
165 Job::finished_cancelled () const
167 boost::mutex::scoped_lock lm (_state_mutex);
168 return _state == FINISHED_CANCELLED;
174 boost::mutex::scoped_lock lm (_state_mutex);
175 return _state == PAUSED;
178 /** Set the state of this job.
179 * @param s New state.
182 Job::set_state (State s)
184 bool finished = false;
187 boost::mutex::scoped_lock lm (_state_mutex);
190 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
191 _ran_for = elapsed_time ();
196 if (finished && ui_signaller) {
197 ui_signaller->emit (boost::bind (boost::ref (Finished)));
201 /** @return Time (in seconds) that this job has been running */
203 Job::elapsed_time () const
205 if (_start_time == 0) {
209 return time (0) - _start_time;
212 /** Set the progress of the current part of the job.
213 * @param p Progress (from 0 to 1)
216 Job::set_progress (float p)
218 if (fabs (p - _last_set) < 0.01) {
219 /* Calm excessive progress reporting */
225 boost::mutex::scoped_lock lm (_progress_mutex);
226 _progress_unknown = false;
227 _stack.back().normalised = p;
228 boost::this_thread::interruption_point ();
235 ui_signaller->emit (boost::bind (boost::ref (Progress)));
239 /** @return fractional overall progress, or -1 if not known */
241 Job::overall_progress () const
243 boost::mutex::scoped_lock lm (_progress_mutex);
244 if (_progress_unknown) {
250 for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
251 factor *= i->allocation;
252 overall += i->normalised * factor;
262 /** Ascend up one level in terms of progress reporting; see descend() */
266 boost::mutex::scoped_lock lm (_progress_mutex);
268 assert (!_stack.empty ());
269 float const a = _stack.back().allocation;
271 _stack.back().normalised += a;
274 /** Descend down one level in terms of progress reporting; e.g. if
275 * there is a task which is split up into N subtasks, each of which
276 * report their progress from 0 to 100%, call descend() before executing
277 * each subtask, and ascend() afterwards to ensure that overall progress
278 * is reported correctly.
280 * @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
283 Job::descend (float a)
285 boost::mutex::scoped_lock lm (_progress_mutex);
286 _stack.push_back (Level (a));
290 Job::error_details () const
292 boost::mutex::scoped_lock lm (_state_mutex);
293 return _error_details;
296 /** @return A summary of any error that the job has generated */
298 Job::error_summary () const
300 boost::mutex::scoped_lock lm (_state_mutex);
301 return _error_summary;
304 /** Set the current error string.
305 * @param e New error string.
308 Job::set_error (string s, string d)
310 boost::mutex::scoped_lock lm (_state_mutex);
315 /** Say that this job's progress will be unknown until further notice */
317 Job::set_progress_unknown ()
319 boost::mutex::scoped_lock lm (_progress_mutex);
320 _progress_unknown = true;
323 /** @return Human-readable status of this job */
327 float const p = overall_progress ();
328 int const t = elapsed_time ();
329 int const r = remaining_time ();
331 int pc = rint (p * 100);
333 /* 100% makes it sound like we've finished when we haven't */
340 if (p >= 0 && t > 10 && r > 0) {
341 /// TRANSLATORS: remaining here follows an amount of time that is remaining
343 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
345 } else if (finished_ok ()) {
346 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
347 } else if (finished_in_error ()) {
348 s << String::compose (_("Error (%1)"), error_summary());
349 } else if (finished_cancelled ()) {
356 /** @return An estimate of the remaining time for this job, in seconds */
358 Job::remaining_time () const
360 return elapsed_time() / overall_progress() - elapsed_time();
370 _thread->interrupt ();