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, shared_ptr<Job> req)
42 , _progress_unknown (false)
48 /** Start the job in a separate thread, returning immediately */
53 _start_time = time (0);
54 boost::thread (boost::bind (&Job::run_wrapper, this));
57 /** A wrapper for the ::run() method to catch exceptions */
65 } catch (libdcp::FileError& e) {
68 set_state (FINISHED_ERROR);
69 set_error (String::compose ("%1 (%2)", e.what(), boost::filesystem::path (e.filename()).leaf()));
71 } catch (std::exception& e) {
74 set_state (FINISHED_ERROR);
75 set_error (e.what ());
83 boost::mutex::scoped_lock lm (_state_mutex);
87 /** @return true if the job is running */
91 boost::mutex::scoped_lock lm (_state_mutex);
92 return _state == RUNNING;
95 /** @return true if the job has finished (either successfully or unsuccessfully) */
97 Job::finished () const
99 boost::mutex::scoped_lock lm (_state_mutex);
100 return _state == FINISHED_OK || _state == FINISHED_ERROR;
103 /** @return true if the job has finished successfully */
105 Job::finished_ok () const
107 boost::mutex::scoped_lock lm (_state_mutex);
108 return _state == FINISHED_OK;
111 /** @return true if the job has finished unsuccessfully */
113 Job::finished_in_error () const
115 boost::mutex::scoped_lock lm (_state_mutex);
116 return _state == FINISHED_ERROR;
119 /** Set the state of this job.
120 * @param s New state.
123 Job::set_state (State s)
125 boost::mutex::scoped_lock lm (_state_mutex);
128 if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
129 _ran_for = elapsed_time ();
134 /** @return Time (in seconds) that this job has been running */
136 Job::elapsed_time () const
138 if (_start_time == 0) {
142 return time (0) - _start_time;
145 /** Set the progress of the current part of the job.
146 * @param p Progress (from 0 to 1)
149 Job::set_progress (float p)
151 boost::mutex::scoped_lock lm (_progress_mutex);
152 _progress_unknown = false;
153 _stack.back().normalised = p;
156 /** @return fractional overall progress, or -1 if not known */
158 Job::overall_progress () const
160 boost::mutex::scoped_lock lm (_progress_mutex);
161 if (_progress_unknown) {
167 for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
168 factor *= i->allocation;
169 overall += i->normalised * factor;
179 /** Ascend up one level in terms of progress reporting; see descend() */
183 boost::mutex::scoped_lock lm (_progress_mutex);
185 assert (!_stack.empty ());
186 float const a = _stack.back().allocation;
188 _stack.back().normalised += a;
191 /** Descend down one level in terms of progress reporting; e.g. if
192 * there is a task which is split up into N subtasks, each of which
193 * report their progress from 0 to 100%, call descend() before executing
194 * each subtask, and ascend() afterwards to ensure that overall progress
195 * is reported correctly.
197 * @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
200 Job::descend (float a)
202 boost::mutex::scoped_lock lm (_progress_mutex);
203 _stack.push_back (Level (a));
206 /** @return Any error string that the job has generated */
210 boost::mutex::scoped_lock lm (_state_mutex);
214 /** Set the current error string.
215 * @param e New error string.
218 Job::set_error (string e)
220 boost::mutex::scoped_lock lm (_state_mutex);
224 /** Say that this job's progress will be unknown until further notice */
226 Job::set_progress_unknown ()
228 boost::mutex::scoped_lock lm (_progress_mutex);
229 _progress_unknown = true;
232 /** @return Human-readable status of this job */
236 float const p = overall_progress ();
237 int const t = elapsed_time ();
238 int const r = remaining_time ();
241 if (!finished () && p >= 0 && t > 10 && r > 0) {
242 s << rint (p * 100) << "%; " << seconds_to_approximate_hms (r) << " remaining";
243 } else if (!finished () && (t <= 10 || r == 0)) {
244 s << rint (p * 100) << "%";
245 } else if (finished_ok ()) {
246 s << "OK (ran for " << seconds_to_hms (_ran_for) << ")";
247 } else if (finished_in_error ()) {
248 s << "Error (" << error() << ")";
254 /** @return An estimate of the remaining time for this job, in seconds */
256 Job::remaining_time () const
258 return elapsed_time() / overall_progress() - elapsed_time();