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>
31 using namespace boost;
33 /** @param s FilmState for the film that we are operating on.
34 * @param l A log that we can write to.
36 Job::Job (shared_ptr<const FilmState> s, Log* l, shared_ptr<Job> req)
42 , _progress_unknown (false)
50 /** Start the job in a separate thread, returning immediately */
55 _start_time = time (0);
56 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);
71 set_error (String::compose ("%1 (%2)", e.what(), filesystem::path (e.filename()).leaf()));
73 } catch (std::exception& e) {
76 set_state (FINISHED_ERROR);
77 set_error (e.what ());
85 boost::mutex::scoped_lock lm (_state_mutex);
89 /** @return true if the job is running */
93 boost::mutex::scoped_lock lm (_state_mutex);
94 return _state == RUNNING;
97 /** @return true if the job has finished (either successfully or unsuccessfully) */
99 Job::finished () const
101 boost::mutex::scoped_lock lm (_state_mutex);
102 return _state == FINISHED_OK || _state == FINISHED_ERROR;
105 /** @return true if the job has finished successfully */
107 Job::finished_ok () const
109 boost::mutex::scoped_lock lm (_state_mutex);
110 return _state == FINISHED_OK;
113 /** @return true if the job has finished unsuccessfully */
115 Job::finished_in_error () const
117 boost::mutex::scoped_lock lm (_state_mutex);
118 return _state == FINISHED_ERROR;
121 /** Set the state of this job.
122 * @param s New state.
125 Job::set_state (State s)
127 boost::mutex::scoped_lock lm (_state_mutex);
130 if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
131 _ran_for = elapsed_time ();
135 /** A hack to work around our lack of cross-thread
136 * signalling; this emits Finished, and listeners
137 * assume that it will be emitted in the GUI thread,
138 * so this method must be called from the GUI thread.
141 Job::emit_finished ()
146 /** @return Time (in seconds) that this job has been running */
148 Job::elapsed_time () const
150 if (_start_time == 0) {
154 return time (0) - _start_time;
157 /** Set the progress of the current part of the job.
158 * @param p Progress (from 0 to 1)
161 Job::set_progress (float p)
163 boost::mutex::scoped_lock lm (_progress_mutex);
164 _progress_unknown = false;
165 _stack.back().normalised = p;
168 /** @return fractional overall progress, or -1 if not known */
170 Job::overall_progress () const
172 boost::mutex::scoped_lock lm (_progress_mutex);
173 if (_progress_unknown) {
179 for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
180 factor *= i->allocation;
181 overall += i->normalised * factor;
191 /** Ascend up one level in terms of progress reporting; see descend() */
195 boost::mutex::scoped_lock lm (_progress_mutex);
197 assert (!_stack.empty ());
198 float const a = _stack.back().allocation;
200 _stack.back().normalised += a;
203 /** Descend down one level in terms of progress reporting; e.g. if
204 * there is a task which is split up into N subtasks, each of which
205 * report their progress from 0 to 100%, call descend() before executing
206 * each subtask, and ascend() afterwards to ensure that overall progress
207 * is reported correctly.
209 * @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
212 Job::descend (float a)
214 boost::mutex::scoped_lock lm (_progress_mutex);
215 _stack.push_back (Level (a));
218 /** @return Any error string that the job has generated */
222 boost::mutex::scoped_lock lm (_state_mutex);
226 /** Set the current error string.
227 * @param e New error string.
230 Job::set_error (string e)
232 boost::mutex::scoped_lock lm (_state_mutex);
236 /** Say that this job's progress will be unknown until further notice */
238 Job::set_progress_unknown ()
240 boost::mutex::scoped_lock lm (_progress_mutex);
241 _progress_unknown = true;
244 /** @return Human-readable status of this job */
248 float const p = overall_progress ();
249 int const t = elapsed_time ();
250 int const r = remaining_time ();
253 if (!finished () && p >= 0 && t > 10 && r > 0) {
254 s << rint (p * 100) << "%; " << seconds_to_approximate_hms (r) << " remaining";
255 } else if (!finished () && (t <= 10 || r == 0)) {
256 s << rint (p * 100) << "%";
257 } else if (finished_ok ()) {
258 s << "OK (ran for " << seconds_to_hms (_ran_for) << ")";
259 } else if (finished_in_error ()) {
260 s << "Error (" << error() << ")";
266 /** @return An estimate of the remaining time for this job, in seconds */
268 Job::remaining_time () const
270 return elapsed_time() / overall_progress() - elapsed_time();