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 /** @param s Film that we are operating on.
39 Job::Job (shared_ptr<Film> f)
44 , _progress_unknown (false)
50 /** Start the job in a separate thread, returning immediately */
55 _start_time = time (0);
56 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
59 /** A wrapper for the ::run() method to catch exceptions */
67 } catch (libdcp::FileError& e) {
70 set_state (FINISHED_ERROR);
72 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
74 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
75 if (s.available < pow (1024, 3)) {
77 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
80 set_error (e.what(), m);
82 } catch (boost::thread_interrupted &) {
84 set_state (FINISHED_CANCELLED);
86 } catch (std::exception& e) {
89 set_state (FINISHED_ERROR);
92 _("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)")
98 set_state (FINISHED_ERROR);
101 _("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)")
107 /** @return true if this job is new (ie has not started running) */
111 boost::mutex::scoped_lock lm (_state_mutex);
112 return _state == NEW;
115 /** @return true if the job is running */
117 Job::running () const
119 boost::mutex::scoped_lock lm (_state_mutex);
120 return _state == RUNNING;
123 /** @return true if the job has finished (either successfully or unsuccessfully) */
125 Job::finished () const
127 boost::mutex::scoped_lock lm (_state_mutex);
128 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
131 /** @return true if the job has finished successfully */
133 Job::finished_ok () const
135 boost::mutex::scoped_lock lm (_state_mutex);
136 return _state == FINISHED_OK;
139 /** @return true if the job has finished unsuccessfully */
141 Job::finished_in_error () const
143 boost::mutex::scoped_lock lm (_state_mutex);
144 return _state == FINISHED_ERROR;
148 Job::finished_cancelled () const
150 boost::mutex::scoped_lock lm (_state_mutex);
151 return _state == FINISHED_CANCELLED;
154 /** Set the state of this job.
155 * @param s New state.
158 Job::set_state (State s)
160 boost::mutex::scoped_lock lm (_state_mutex);
163 if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
164 _ran_for = elapsed_time ();
168 /** @return Time (in seconds) that this job has been running */
170 Job::elapsed_time () const
172 if (_start_time == 0) {
176 return time (0) - _start_time;
179 /** Set the progress of the current part of the job.
180 * @param p Progress (from 0 to 1)
183 Job::set_progress (float p)
185 boost::mutex::scoped_lock lm (_progress_mutex);
186 _progress_unknown = false;
187 _stack.back().normalised = p;
188 boost::this_thread::interruption_point ();
191 /** @return fractional overall progress, or -1 if not known */
193 Job::overall_progress () const
195 boost::mutex::scoped_lock lm (_progress_mutex);
196 if (_progress_unknown) {
202 for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
203 factor *= i->allocation;
204 overall += i->normalised * factor;
214 /** Ascend up one level in terms of progress reporting; see descend() */
218 boost::mutex::scoped_lock lm (_progress_mutex);
220 assert (!_stack.empty ());
221 float const a = _stack.back().allocation;
223 _stack.back().normalised += a;
226 /** Descend down one level in terms of progress reporting; e.g. if
227 * there is a task which is split up into N subtasks, each of which
228 * report their progress from 0 to 100%, call descend() before executing
229 * each subtask, and ascend() afterwards to ensure that overall progress
230 * is reported correctly.
232 * @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
235 Job::descend (float a)
237 boost::mutex::scoped_lock lm (_progress_mutex);
238 _stack.push_back (Level (a));
242 Job::error_details () const
244 boost::mutex::scoped_lock lm (_state_mutex);
245 return _error_details;
248 /** @return A summary of any error that the job has generated */
250 Job::error_summary () const
252 boost::mutex::scoped_lock lm (_state_mutex);
253 return _error_summary;
256 /** Set the current error string.
257 * @param e New error string.
260 Job::set_error (string s, string d)
262 boost::mutex::scoped_lock lm (_state_mutex);
267 /** Say that this job's progress will be unknown until further notice */
269 Job::set_progress_unknown ()
271 boost::mutex::scoped_lock lm (_progress_mutex);
272 _progress_unknown = true;
275 /** @return Human-readable status of this job */
279 float const p = overall_progress ();
280 int const t = elapsed_time ();
281 int const r = remaining_time ();
283 int pc = rint (p * 100);
285 /* 100% makes it sound like we've finished when we haven't */
292 if (p >= 0 && t > 10 && r > 0) {
293 /// TRANSLATORS: remaining here follows an amount of time that is remaining
295 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
297 } else if (finished_ok ()) {
298 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
299 } else if (finished_in_error ()) {
300 s << String::compose (_("Error (%1)"), error_summary());
301 } else if (finished_cancelled ()) {
308 /** @return An estimate of the remaining time for this job, in seconds */
310 Job::remaining_time () const
312 return elapsed_time() / overall_progress() - elapsed_time();
322 _thread->interrupt ();