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>
32 using std::stringstream;
33 using boost::shared_ptr;
35 /** @param s Film that we are operating on.
37 Job::Job (shared_ptr<Film> f)
41 , _progress_unknown (false)
47 /** Start the job in a separate thread, returning immediately */
52 _start_time = time (0);
53 boost::thread (boost::bind (&Job::run_wrapper, this));
56 /** A wrapper for the ::run() method to catch exceptions */
64 } catch (libdcp::FileError& e) {
67 set_state (FINISHED_ERROR);
69 string m = String::compose ("An error occurred whilst handling the file %1.", boost::filesystem::path (e.filename()).leaf());
71 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
72 if (s.available < pow (1024, 3)) {
73 m += "\n\nThe drive that the film is stored on is low in disc space. Free some more space and try again.";
76 set_error (e.what(), m);
78 } catch (std::exception& e) {
81 set_state (FINISHED_ERROR);
84 "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)"
90 set_state (FINISHED_ERROR);
93 "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)"
99 /** @return true if this job is new (ie has not started running) */
103 boost::mutex::scoped_lock lm (_state_mutex);
104 return _state == NEW;
107 /** @return true if the job is running */
109 Job::running () const
111 boost::mutex::scoped_lock lm (_state_mutex);
112 return _state == RUNNING;
115 /** @return true if the job has finished (either successfully or unsuccessfully) */
117 Job::finished () const
119 boost::mutex::scoped_lock lm (_state_mutex);
120 return _state == FINISHED_OK || _state == FINISHED_ERROR;
123 /** @return true if the job has finished successfully */
125 Job::finished_ok () const
127 boost::mutex::scoped_lock lm (_state_mutex);
128 return _state == FINISHED_OK;
131 /** @return true if the job has finished unsuccessfully */
133 Job::finished_in_error () const
135 boost::mutex::scoped_lock lm (_state_mutex);
136 return _state == FINISHED_ERROR;
139 /** Set the state of this job.
140 * @param s New state.
143 Job::set_state (State s)
145 boost::mutex::scoped_lock lm (_state_mutex);
148 if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
149 _ran_for = elapsed_time ();
154 /** @return Time (in seconds) that this job has been running */
156 Job::elapsed_time () const
158 if (_start_time == 0) {
162 return time (0) - _start_time;
165 /** Set the progress of the current part of the job.
166 * @param p Progress (from 0 to 1)
169 Job::set_progress (float p)
171 boost::mutex::scoped_lock lm (_progress_mutex);
172 _progress_unknown = false;
173 _stack.back().normalised = p;
176 /** @return fractional overall progress, or -1 if not known */
178 Job::overall_progress () const
180 boost::mutex::scoped_lock lm (_progress_mutex);
181 if (_progress_unknown) {
187 for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
188 factor *= i->allocation;
189 overall += i->normalised * factor;
199 /** Ascend up one level in terms of progress reporting; see descend() */
203 boost::mutex::scoped_lock lm (_progress_mutex);
205 assert (!_stack.empty ());
206 float const a = _stack.back().allocation;
208 _stack.back().normalised += a;
211 /** Descend down one level in terms of progress reporting; e.g. if
212 * there is a task which is split up into N subtasks, each of which
213 * report their progress from 0 to 100%, call descend() before executing
214 * each subtask, and ascend() afterwards to ensure that overall progress
215 * is reported correctly.
217 * @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
220 Job::descend (float a)
222 boost::mutex::scoped_lock lm (_progress_mutex);
223 _stack.push_back (Level (a));
227 Job::error_details () const
229 boost::mutex::scoped_lock lm (_state_mutex);
230 return _error_details;
233 /** @return A summary of any error that the job has generated */
235 Job::error_summary () const
237 boost::mutex::scoped_lock lm (_state_mutex);
238 return _error_summary;
241 /** Set the current error string.
242 * @param e New error string.
245 Job::set_error (string s, string d)
247 boost::mutex::scoped_lock lm (_state_mutex);
252 /** Say that this job's progress will be unknown until further notice */
254 Job::set_progress_unknown ()
256 boost::mutex::scoped_lock lm (_progress_mutex);
257 _progress_unknown = true;
260 /** @return Human-readable status of this job */
264 float const p = overall_progress ();
265 int const t = elapsed_time ();
266 int const r = remaining_time ();
268 int pc = rint (p * 100);
270 /* 100% makes it sound like we've finished when we haven't */
275 if (!finished () && p >= 0 && t > 10 && r > 0) {
276 s << pc << "%; " << seconds_to_approximate_hms (r) << " remaining";
277 } else if (!finished () && (t <= 10 || r == 0)) {
279 } else if (finished_ok ()) {
280 s << "OK (ran for " << seconds_to_hms (_ran_for) << ")";
281 } else if (finished_in_error ()) {
282 s << "Error (" << error_summary() << ")";
288 /** @return An estimate of the remaining time for this job, in seconds */
290 Job::remaining_time () const
292 return elapsed_time() / overall_progress() - elapsed_time();