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"
37 using std::stringstream;
38 using boost::shared_ptr;
40 Job::Job (shared_ptr<const Film> f)
45 , _progress_unknown (false)
51 /** Start the job in a separate thread, returning immediately */
56 _start_time = time (0);
57 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
60 /** A wrapper for the ::run() method to catch exceptions */
68 } catch (libdcp::FileError& e) {
71 set_state (FINISHED_ERROR);
73 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
76 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
77 if (s.available < pow (1024, 3)) {
79 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
85 set_error (e.what(), m);
87 } catch (OpenFileError& e) {
90 set_state (FINISHED_ERROR);
93 String::compose (_("Could not open %1"), e.file().string()),
94 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())
97 } catch (boost::thread_interrupted &) {
99 set_state (FINISHED_CANCELLED);
101 } catch (std::exception& e) {
104 set_state (FINISHED_ERROR);
107 _("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)")
113 set_state (FINISHED_ERROR);
116 _("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)")
122 /** @return true if this job is new (ie has not started running) */
126 boost::mutex::scoped_lock lm (_state_mutex);
127 return _state == NEW;
130 /** @return true if the job is running */
132 Job::running () const
134 boost::mutex::scoped_lock lm (_state_mutex);
135 return _state == RUNNING;
138 /** @return true if the job has finished (either successfully or unsuccessfully) */
140 Job::finished () const
142 boost::mutex::scoped_lock lm (_state_mutex);
143 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
146 /** @return true if the job has finished successfully */
148 Job::finished_ok () const
150 boost::mutex::scoped_lock lm (_state_mutex);
151 return _state == FINISHED_OK;
154 /** @return true if the job has finished unsuccessfully */
156 Job::finished_in_error () const
158 boost::mutex::scoped_lock lm (_state_mutex);
159 return _state == FINISHED_ERROR;
163 Job::finished_cancelled () const
165 boost::mutex::scoped_lock lm (_state_mutex);
166 return _state == FINISHED_CANCELLED;
172 boost::mutex::scoped_lock lm (_state_mutex);
173 return _state == PAUSED;
176 /** Set the state of this job.
177 * @param s New state.
180 Job::set_state (State s)
182 boost::mutex::scoped_lock lm (_state_mutex);
185 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
186 _ran_for = elapsed_time ();
188 ui_signaller->emit (boost::bind (boost::ref (Finished)));
193 /** @return Time (in seconds) that this job has been running */
195 Job::elapsed_time () const
197 if (_start_time == 0) {
201 return time (0) - _start_time;
204 /** Set the progress of the current part of the job.
205 * @param p Progress (from 0 to 1)
208 Job::set_progress (float p)
210 boost::mutex::scoped_lock lm (_progress_mutex);
211 _progress_unknown = false;
212 _stack.back().normalised = p;
213 boost::this_thread::interruption_point ();
220 ui_signaller->emit (boost::bind (boost::ref (Progress)));
224 /** @return fractional overall progress, or -1 if not known */
226 Job::overall_progress () const
228 boost::mutex::scoped_lock lm (_progress_mutex);
229 if (_progress_unknown) {
235 for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
236 factor *= i->allocation;
237 overall += i->normalised * factor;
247 /** Ascend up one level in terms of progress reporting; see descend() */
251 boost::mutex::scoped_lock lm (_progress_mutex);
253 assert (!_stack.empty ());
254 float const a = _stack.back().allocation;
256 _stack.back().normalised += a;
259 /** Descend down one level in terms of progress reporting; e.g. if
260 * there is a task which is split up into N subtasks, each of which
261 * report their progress from 0 to 100%, call descend() before executing
262 * each subtask, and ascend() afterwards to ensure that overall progress
263 * is reported correctly.
265 * @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
268 Job::descend (float a)
270 boost::mutex::scoped_lock lm (_progress_mutex);
271 _stack.push_back (Level (a));
275 Job::error_details () const
277 boost::mutex::scoped_lock lm (_state_mutex);
278 return _error_details;
281 /** @return A summary of any error that the job has generated */
283 Job::error_summary () const
285 boost::mutex::scoped_lock lm (_state_mutex);
286 return _error_summary;
289 /** Set the current error string.
290 * @param e New error string.
293 Job::set_error (string s, string d)
295 boost::mutex::scoped_lock lm (_state_mutex);
300 /** Say that this job's progress will be unknown until further notice */
302 Job::set_progress_unknown ()
304 boost::mutex::scoped_lock lm (_progress_mutex);
305 _progress_unknown = true;
308 /** @return Human-readable status of this job */
312 float const p = overall_progress ();
313 int const t = elapsed_time ();
314 int const r = remaining_time ();
316 int pc = rint (p * 100);
318 /* 100% makes it sound like we've finished when we haven't */
325 if (p >= 0 && t > 10 && r > 0) {
326 /// TRANSLATORS: remaining here follows an amount of time that is remaining
328 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
330 } else if (finished_ok ()) {
331 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
332 } else if (finished_in_error ()) {
333 s << String::compose (_("Error (%1)"), error_summary());
334 } else if (finished_cancelled ()) {
341 /** @return An estimate of the remaining time for this job, in seconds */
343 Job::remaining_time () const
345 return elapsed_time() / overall_progress() - elapsed_time();
355 _thread->interrupt ();