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.
35 * @param l A log that we can write to.
37 Job::Job (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l)
43 , _progress_unknown (false)
51 /** Start the job in a separate thread, returning immediately */
56 _start_time = time (0);
57 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);
72 set_error (String::compose ("%1 (%2)", e.what(), filesystem::path (e.filename()).leaf()));
74 } catch (std::exception& e) {
77 set_state (FINISHED_ERROR);
78 set_error (e.what ());
83 /** @return true if the job is running */
87 boost::mutex::scoped_lock lm (_state_mutex);
88 return _state == RUNNING;
91 /** @return true if the job has finished (either successfully or unsuccessfully) */
93 Job::finished () const
95 boost::mutex::scoped_lock lm (_state_mutex);
96 return _state == FINISHED_OK || _state == FINISHED_ERROR;
99 /** @return true if the job has finished successfully */
101 Job::finished_ok () const
103 boost::mutex::scoped_lock lm (_state_mutex);
104 return _state == FINISHED_OK;
107 /** @return true if the job has finished unsuccessfully */
109 Job::finished_in_error () const
111 boost::mutex::scoped_lock lm (_state_mutex);
112 return _state == FINISHED_ERROR;
115 /** Set the state of this job.
116 * @param s New state.
119 Job::set_state (State s)
121 boost::mutex::scoped_lock lm (_state_mutex);
124 if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
125 _ran_for = elapsed_time ();
129 /** A hack to work around our lack of cross-thread
130 * signalling; this emits Finished, and listeners
131 * assume that it will be emitted in the GUI thread,
132 * so this method must be called from the GUI thread.
135 Job::emit_finished ()
140 /** @return Time (in seconds) that this job has been running */
142 Job::elapsed_time () const
144 if (_start_time == 0) {
148 return time (0) - _start_time;
151 /** Set the progress of the current part of the job.
152 * @param p Progress (from 0 to 1)
155 Job::set_progress (float p)
157 boost::mutex::scoped_lock lm (_progress_mutex);
158 _stack.back().normalised = p;
161 /** @return fractional overall progress, or -1 if not known */
163 Job::overall_progress () const
165 boost::mutex::scoped_lock lm (_progress_mutex);
166 if (_progress_unknown) {
172 for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
173 factor *= i->allocation;
174 overall += i->normalised * factor;
184 /** Ascend up one level in terms of progress reporting; see descend() */
188 boost::mutex::scoped_lock lm (_progress_mutex);
190 assert (!_stack.empty ());
191 float const a = _stack.back().allocation;
193 _stack.back().normalised += a;
196 /** Descend down one level in terms of progress reporting; e.g. if
197 * there is a task which is split up into N subtasks, each of which
198 * report their progress from 0 to 100%, call descend() before executing
199 * each subtask, and ascend() afterwards to ensure that overall progress
200 * is reported correctly.
202 * @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
205 Job::descend (float a)
207 boost::mutex::scoped_lock lm (_progress_mutex);
208 _stack.push_back (Level (a));
211 /** @return Any error string that the job has generated */
215 boost::mutex::scoped_lock lm (_state_mutex);
219 /** Set the current error string.
220 * @param e New error string.
223 Job::set_error (string e)
225 boost::mutex::scoped_lock lm (_state_mutex);
229 /** Say that this job's progress will always be unknown */
231 Job::set_progress_unknown ()
233 boost::mutex::scoped_lock lm (_progress_mutex);
234 _progress_unknown = true;
237 /** @return Human-readable status of this job */
241 float const p = overall_progress ();
242 int const t = elapsed_time ();
243 int const r = remaining_time ();
246 if (!finished () && p >= 0 && t > 10 && r > 0) {
247 s << rint (p * 100) << "%; " << seconds_to_approximate_hms (r) << " remaining";
248 } else if (!finished () && (t <= 10 || r == 0)) {
249 s << rint (p * 100) << "%";
250 } else if (finished_ok ()) {
251 s << "OK (ran for " << seconds_to_hms (_ran_for) << ")";
252 } else if (finished_in_error ()) {
253 s << "Error (" << error() << ")";
259 /** @return An estimate of the remaining time for this job, in seconds */
261 Job::remaining_time () const
263 return elapsed_time() / overall_progress() - elapsed_time();