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.
36 * @param req Job that must be completed before this job is run.
38 Job::Job (shared_ptr<Film> f, shared_ptr<Job> req)
43 , _progress_unknown (false)
49 /** Start the job in a separate thread, returning immediately */
54 _start_time = time (0);
55 boost::thread (boost::bind (&Job::run_wrapper, this));
58 /** A wrapper for the ::run() method to catch exceptions */
66 } catch (libdcp::FileError& e) {
69 set_state (FINISHED_ERROR);
70 set_error (String::compose ("%1 (%2)", e.what(), boost::filesystem::path (e.filename()).leaf()));
72 } catch (std::exception& e) {
75 set_state (FINISHED_ERROR);
76 set_error (e.what ());
81 /** @return true if this job is new (ie has not started running) */
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 ();
136 /** @return Time (in seconds) that this job has been running */
138 Job::elapsed_time () const
140 if (_start_time == 0) {
144 return time (0) - _start_time;
147 /** Set the progress of the current part of the job.
148 * @param p Progress (from 0 to 1)
151 Job::set_progress (float p)
153 boost::mutex::scoped_lock lm (_progress_mutex);
154 _progress_unknown = false;
155 _stack.back().normalised = p;
158 /** @return fractional overall progress, or -1 if not known */
160 Job::overall_progress () const
162 boost::mutex::scoped_lock lm (_progress_mutex);
163 if (_progress_unknown) {
169 for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
170 factor *= i->allocation;
171 overall += i->normalised * factor;
181 /** Ascend up one level in terms of progress reporting; see descend() */
185 boost::mutex::scoped_lock lm (_progress_mutex);
187 assert (!_stack.empty ());
188 float const a = _stack.back().allocation;
190 _stack.back().normalised += a;
193 /** Descend down one level in terms of progress reporting; e.g. if
194 * there is a task which is split up into N subtasks, each of which
195 * report their progress from 0 to 100%, call descend() before executing
196 * each subtask, and ascend() afterwards to ensure that overall progress
197 * is reported correctly.
199 * @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
202 Job::descend (float a)
204 boost::mutex::scoped_lock lm (_progress_mutex);
205 _stack.push_back (Level (a));
208 /** @return Any error string that the job has generated */
212 boost::mutex::scoped_lock lm (_state_mutex);
216 /** Set the current error string.
217 * @param e New error string.
220 Job::set_error (string e)
222 boost::mutex::scoped_lock lm (_state_mutex);
226 /** Say that this job's progress will be unknown until further notice */
228 Job::set_progress_unknown ()
230 boost::mutex::scoped_lock lm (_progress_mutex);
231 _progress_unknown = true;
234 /** @return Human-readable status of this job */
238 float const p = overall_progress ();
239 int const t = elapsed_time ();
240 int const r = remaining_time ();
243 if (!finished () && p >= 0 && t > 10 && r > 0) {
244 s << rint (p * 100) << "%; " << seconds_to_approximate_hms (r) << " remaining";
245 } else if (!finished () && (t <= 10 || r == 0)) {
246 s << rint (p * 100) << "%";
247 } else if (finished_ok ()) {
248 s << "OK (ran for " << seconds_to_hms (_ran_for) << ")";
249 } else if (finished_in_error ()) {
250 s << "Error (" << error() << ")";
256 /** @return An estimate of the remaining time for this job, in seconds */
258 Job::remaining_time () const
260 return elapsed_time() / overall_progress() - elapsed_time();