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 Film that we are operating on.
35 Job::Job (shared_ptr<Film> f, shared_ptr<Job> req)
40 , _progress_unknown (false)
46 /** Start the job in a separate thread, returning immediately */
51 _start_time = time (0);
52 boost::thread (boost::bind (&Job::run_wrapper, this));
55 /** A wrapper for the ::run() method to catch exceptions */
63 } catch (libdcp::FileError& e) {
66 set_state (FINISHED_ERROR);
67 set_error (String::compose ("%1 (%2)", e.what(), filesystem::path (e.filename()).leaf()));
69 } catch (std::exception& e) {
72 set_state (FINISHED_ERROR);
73 set_error (e.what ());
81 boost::mutex::scoped_lock lm (_state_mutex);
85 /** @return true if the job is running */
89 boost::mutex::scoped_lock lm (_state_mutex);
90 return _state == RUNNING;
93 /** @return true if the job has finished (either successfully or unsuccessfully) */
95 Job::finished () const
97 boost::mutex::scoped_lock lm (_state_mutex);
98 return _state == FINISHED_OK || _state == FINISHED_ERROR;
101 /** @return true if the job has finished successfully */
103 Job::finished_ok () const
105 boost::mutex::scoped_lock lm (_state_mutex);
106 return _state == FINISHED_OK;
109 /** @return true if the job has finished unsuccessfully */
111 Job::finished_in_error () const
113 boost::mutex::scoped_lock lm (_state_mutex);
114 return _state == FINISHED_ERROR;
117 /** Set the state of this job.
118 * @param s New state.
121 Job::set_state (State s)
123 boost::mutex::scoped_lock lm (_state_mutex);
126 if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
127 _ran_for = elapsed_time ();
131 /** A hack to work around our lack of cross-thread
132 * signalling; this emits Finished, and listeners
133 * assume that it will be emitted in the GUI thread,
134 * so this method must be called from the GUI thread.
137 Job::emit_finished ()
142 /** @return Time (in seconds) that this job has been running */
144 Job::elapsed_time () const
146 if (_start_time == 0) {
150 return time (0) - _start_time;
153 /** Set the progress of the current part of the job.
154 * @param p Progress (from 0 to 1)
157 Job::set_progress (float p)
159 boost::mutex::scoped_lock lm (_progress_mutex);
160 _progress_unknown = false;
161 _stack.back().normalised = p;
164 /** @return fractional overall progress, or -1 if not known */
166 Job::overall_progress () const
168 boost::mutex::scoped_lock lm (_progress_mutex);
169 if (_progress_unknown) {
175 for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
176 factor *= i->allocation;
177 overall += i->normalised * factor;
187 /** Ascend up one level in terms of progress reporting; see descend() */
191 boost::mutex::scoped_lock lm (_progress_mutex);
193 assert (!_stack.empty ());
194 float const a = _stack.back().allocation;
196 _stack.back().normalised += a;
199 /** Descend down one level in terms of progress reporting; e.g. if
200 * there is a task which is split up into N subtasks, each of which
201 * report their progress from 0 to 100%, call descend() before executing
202 * each subtask, and ascend() afterwards to ensure that overall progress
203 * is reported correctly.
205 * @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
208 Job::descend (float a)
210 boost::mutex::scoped_lock lm (_progress_mutex);
211 _stack.push_back (Level (a));
214 /** @return Any error string that the job has generated */
218 boost::mutex::scoped_lock lm (_state_mutex);
222 /** Set the current error string.
223 * @param e New error string.
226 Job::set_error (string e)
228 boost::mutex::scoped_lock lm (_state_mutex);
232 /** Say that this job's progress will be unknown until further notice */
234 Job::set_progress_unknown ()
236 boost::mutex::scoped_lock lm (_progress_mutex);
237 _progress_unknown = true;
240 /** @return Human-readable status of this job */
244 float const p = overall_progress ();
245 int const t = elapsed_time ();
246 int const r = remaining_time ();
249 if (!finished () && p >= 0 && t > 10 && r > 0) {
250 s << rint (p * 100) << "%; " << seconds_to_approximate_hms (r) << " remaining";
251 } else if (!finished () && (t <= 10 || r == 0)) {
252 s << rint (p * 100) << "%";
253 } else if (finished_ok ()) {
254 s << "OK (ran for " << seconds_to_hms (_ran_for) << ")";
255 } else if (finished_in_error ()) {
256 s << "Error (" << error() << ")";
262 /** @return An estimate of the remaining time for this job, in seconds */
264 Job::remaining_time () const
266 return elapsed_time() / overall_progress() - elapsed_time();