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 /** @return fractional overall progress, or -1 if not known */
222 Job::overall_progress () const
224 boost::mutex::scoped_lock lm (_progress_mutex);
225 if (_progress_unknown) {
231 for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
232 factor *= i->allocation;
233 overall += i->normalised * factor;
243 /** Ascend up one level in terms of progress reporting; see descend() */
247 boost::mutex::scoped_lock lm (_progress_mutex);
249 assert (!_stack.empty ());
250 float const a = _stack.back().allocation;
252 _stack.back().normalised += a;
255 /** Descend down one level in terms of progress reporting; e.g. if
256 * there is a task which is split up into N subtasks, each of which
257 * report their progress from 0 to 100%, call descend() before executing
258 * each subtask, and ascend() afterwards to ensure that overall progress
259 * is reported correctly.
261 * @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
264 Job::descend (float a)
266 boost::mutex::scoped_lock lm (_progress_mutex);
267 _stack.push_back (Level (a));
271 Job::error_details () const
273 boost::mutex::scoped_lock lm (_state_mutex);
274 return _error_details;
277 /** @return A summary of any error that the job has generated */
279 Job::error_summary () const
281 boost::mutex::scoped_lock lm (_state_mutex);
282 return _error_summary;
285 /** Set the current error string.
286 * @param e New error string.
289 Job::set_error (string s, string d)
291 boost::mutex::scoped_lock lm (_state_mutex);
296 /** Say that this job's progress will be unknown until further notice */
298 Job::set_progress_unknown ()
300 boost::mutex::scoped_lock lm (_progress_mutex);
301 _progress_unknown = true;
304 /** @return Human-readable status of this job */
308 float const p = overall_progress ();
309 int const t = elapsed_time ();
310 int const r = remaining_time ();
312 int pc = rint (p * 100);
314 /* 100% makes it sound like we've finished when we haven't */
321 if (p >= 0 && t > 10 && r > 0) {
322 /// TRANSLATORS: remaining here follows an amount of time that is remaining
324 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
326 } else if (finished_ok ()) {
327 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
328 } else if (finished_in_error ()) {
329 s << String::compose (_("Error (%1)"), error_summary());
330 } else if (finished_cancelled ()) {
337 /** @return An estimate of the remaining time for this job, in seconds */
339 Job::remaining_time () const
341 return elapsed_time() / overall_progress() - elapsed_time();
351 _thread->interrupt ();