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());
72 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
73 if (s.available < pow (1024, 3)) {
75 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
78 set_error (e.what(), m);
80 } catch (boost::thread_interrupted &) {
82 set_state (FINISHED_CANCELLED);
84 } catch (std::exception& e) {
87 set_state (FINISHED_ERROR);
90 _("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)")
96 set_state (FINISHED_ERROR);
99 _("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)")
105 /** @return true if this job is new (ie has not started running) */
109 boost::mutex::scoped_lock lm (_state_mutex);
110 return _state == NEW;
113 /** @return true if the job is running */
115 Job::running () const
117 boost::mutex::scoped_lock lm (_state_mutex);
118 return _state == RUNNING;
121 /** @return true if the job has finished (either successfully or unsuccessfully) */
123 Job::finished () const
125 boost::mutex::scoped_lock lm (_state_mutex);
126 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
129 /** @return true if the job has finished successfully */
131 Job::finished_ok () const
133 boost::mutex::scoped_lock lm (_state_mutex);
134 return _state == FINISHED_OK;
137 /** @return true if the job has finished unsuccessfully */
139 Job::finished_in_error () const
141 boost::mutex::scoped_lock lm (_state_mutex);
142 return _state == FINISHED_ERROR;
146 Job::finished_cancelled () const
148 boost::mutex::scoped_lock lm (_state_mutex);
149 return _state == FINISHED_CANCELLED;
152 /** Set the state of this job.
153 * @param s New state.
156 Job::set_state (State s)
158 boost::mutex::scoped_lock lm (_state_mutex);
161 if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
162 _ran_for = elapsed_time ();
166 /** @return Time (in seconds) that this job has been running */
168 Job::elapsed_time () const
170 if (_start_time == 0) {
174 return time (0) - _start_time;
177 /** Set the progress of the current part of the job.
178 * @param p Progress (from 0 to 1)
181 Job::set_progress (float p)
183 boost::mutex::scoped_lock lm (_progress_mutex);
184 _progress_unknown = false;
185 _stack.back().normalised = p;
186 boost::this_thread::interruption_point ();
189 /** @return fractional overall progress, or -1 if not known */
191 Job::overall_progress () const
193 boost::mutex::scoped_lock lm (_progress_mutex);
194 if (_progress_unknown) {
200 for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
201 factor *= i->allocation;
202 overall += i->normalised * factor;
212 /** Ascend up one level in terms of progress reporting; see descend() */
216 boost::mutex::scoped_lock lm (_progress_mutex);
218 assert (!_stack.empty ());
219 float const a = _stack.back().allocation;
221 _stack.back().normalised += a;
224 /** Descend down one level in terms of progress reporting; e.g. if
225 * there is a task which is split up into N subtasks, each of which
226 * report their progress from 0 to 100%, call descend() before executing
227 * each subtask, and ascend() afterwards to ensure that overall progress
228 * is reported correctly.
230 * @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
233 Job::descend (float a)
235 boost::mutex::scoped_lock lm (_progress_mutex);
236 _stack.push_back (Level (a));
240 Job::error_details () const
242 boost::mutex::scoped_lock lm (_state_mutex);
243 return _error_details;
246 /** @return A summary of any error that the job has generated */
248 Job::error_summary () const
250 boost::mutex::scoped_lock lm (_state_mutex);
251 return _error_summary;
254 /** Set the current error string.
255 * @param e New error string.
258 Job::set_error (string s, string d)
260 boost::mutex::scoped_lock lm (_state_mutex);
265 /** Say that this job's progress will be unknown until further notice */
267 Job::set_progress_unknown ()
269 boost::mutex::scoped_lock lm (_progress_mutex);
270 _progress_unknown = true;
273 /** @return Human-readable status of this job */
277 float const p = overall_progress ();
278 int const t = elapsed_time ();
279 int const r = remaining_time ();
281 int pc = rint (p * 100);
283 /* 100% makes it sound like we've finished when we haven't */
290 if (p >= 0 && t > 10 && r > 0) {
291 /// TRANSLATORS: remaining here follows an amount of time that is remaining
293 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
295 } else if (finished_ok ()) {
296 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
297 } else if (finished_in_error ()) {
298 s << String::compose (_("Error (%1)"), error_summary());
299 } else if (finished_cancelled ()) {
306 /** @return An estimate of the remaining time for this job, in seconds */
308 Job::remaining_time () const
310 return elapsed_time() / overall_progress() - elapsed_time();
320 _thread->interrupt ();