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>
35 using std::stringstream;
36 using boost::shared_ptr;
38 /** @param s Film that we are operating on.
40 Job::Job (shared_ptr<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 (boost::thread_interrupted &) {
89 set_state (FINISHED_CANCELLED);
91 } catch (std::exception& e) {
94 set_state (FINISHED_ERROR);
97 _("It is not known what caused this error. The best idea is to report the problem to the DVD-o-matic mailing list (dvdomatic@carlh.net)")
103 set_state (FINISHED_ERROR);
106 _("It is not known what caused this error. The best idea is to report the problem to the DVD-o-matic mailing list (dvdomatic@carlh.net)")
112 /** @return true if this job is new (ie has not started running) */
116 boost::mutex::scoped_lock lm (_state_mutex);
117 return _state == NEW;
120 /** @return true if the job is running */
122 Job::running () const
124 boost::mutex::scoped_lock lm (_state_mutex);
125 return _state == RUNNING;
128 /** @return true if the job has finished (either successfully or unsuccessfully) */
130 Job::finished () const
132 boost::mutex::scoped_lock lm (_state_mutex);
133 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
136 /** @return true if the job has finished successfully */
138 Job::finished_ok () const
140 boost::mutex::scoped_lock lm (_state_mutex);
141 return _state == FINISHED_OK;
144 /** @return true if the job has finished unsuccessfully */
146 Job::finished_in_error () const
148 boost::mutex::scoped_lock lm (_state_mutex);
149 return _state == FINISHED_ERROR;
153 Job::finished_cancelled () const
155 boost::mutex::scoped_lock lm (_state_mutex);
156 return _state == FINISHED_CANCELLED;
162 boost::mutex::scoped_lock lm (_state_mutex);
163 return _state == PAUSED;
166 /** Set the state of this job.
167 * @param s New state.
170 Job::set_state (State s)
172 boost::mutex::scoped_lock lm (_state_mutex);
175 if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
176 _ran_for = elapsed_time ();
180 /** @return Time (in seconds) that this job has been running */
182 Job::elapsed_time () const
184 if (_start_time == 0) {
188 return time (0) - _start_time;
191 /** Set the progress of the current part of the job.
192 * @param p Progress (from 0 to 1)
195 Job::set_progress (float p)
197 boost::mutex::scoped_lock lm (_progress_mutex);
198 _progress_unknown = false;
199 _stack.back().normalised = p;
200 boost::this_thread::interruption_point ();
207 /** @return fractional overall progress, or -1 if not known */
209 Job::overall_progress () const
211 boost::mutex::scoped_lock lm (_progress_mutex);
212 if (_progress_unknown) {
218 for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
219 factor *= i->allocation;
220 overall += i->normalised * factor;
230 /** Ascend up one level in terms of progress reporting; see descend() */
234 boost::mutex::scoped_lock lm (_progress_mutex);
236 assert (!_stack.empty ());
237 float const a = _stack.back().allocation;
239 _stack.back().normalised += a;
242 /** Descend down one level in terms of progress reporting; e.g. if
243 * there is a task which is split up into N subtasks, each of which
244 * report their progress from 0 to 100%, call descend() before executing
245 * each subtask, and ascend() afterwards to ensure that overall progress
246 * is reported correctly.
248 * @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
251 Job::descend (float a)
253 boost::mutex::scoped_lock lm (_progress_mutex);
254 _stack.push_back (Level (a));
258 Job::error_details () const
260 boost::mutex::scoped_lock lm (_state_mutex);
261 return _error_details;
264 /** @return A summary of any error that the job has generated */
266 Job::error_summary () const
268 boost::mutex::scoped_lock lm (_state_mutex);
269 return _error_summary;
272 /** Set the current error string.
273 * @param e New error string.
276 Job::set_error (string s, string d)
278 boost::mutex::scoped_lock lm (_state_mutex);
283 /** Say that this job's progress will be unknown until further notice */
285 Job::set_progress_unknown ()
287 boost::mutex::scoped_lock lm (_progress_mutex);
288 _progress_unknown = true;
291 /** @return Human-readable status of this job */
295 float const p = overall_progress ();
296 int const t = elapsed_time ();
297 int const r = remaining_time ();
299 int pc = rint (p * 100);
301 /* 100% makes it sound like we've finished when we haven't */
308 if (p >= 0 && t > 10 && r > 0) {
309 /// TRANSLATORS: remaining here follows an amount of time that is remaining
311 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
313 } else if (finished_ok ()) {
314 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
315 } else if (finished_in_error ()) {
316 s << String::compose (_("Error (%1)"), error_summary());
317 } else if (finished_cancelled ()) {
324 /** @return An estimate of the remaining time for this job, in seconds */
326 Job::remaining_time () const
328 return elapsed_time() / overall_progress() - elapsed_time();
338 _thread->interrupt ();