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)
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);
72 s << e.what() << "(" << filesystem::path (e.filename()).leaf() << ")";
75 } catch (std::exception& e) {
78 set_state (FINISHED_ERROR);
79 set_error (e.what ());
84 /** @return true if the job is running */
88 boost::mutex::scoped_lock lm (_state_mutex);
89 return _state == RUNNING;
92 /** @return true if the job has finished (either successfully or unsuccessfully) */
94 Job::finished () const
96 boost::mutex::scoped_lock lm (_state_mutex);
97 return _state == FINISHED_OK || _state == FINISHED_ERROR;
100 /** @return true if the job has finished successfully */
102 Job::finished_ok () const
104 boost::mutex::scoped_lock lm (_state_mutex);
105 return _state == FINISHED_OK;
108 /** @return true if the job has finished unsuccessfully */
110 Job::finished_in_error () const
112 boost::mutex::scoped_lock lm (_state_mutex);
113 return _state == FINISHED_ERROR;
116 /** Set the state of this job.
117 * @param s New state.
120 Job::set_state (State s)
122 boost::mutex::scoped_lock lm (_state_mutex);
126 /** A hack to work around our lack of cross-thread
127 * signalling; this emits Finished, and listeners
128 * assume that it will be emitted in the GUI thread,
129 * so this method must be called from the GUI thread.
132 Job::emit_finished ()
137 /** @return Time (in seconds) that this job has been running */
139 Job::elapsed_time () const
141 if (_start_time == 0) {
145 return time (0) - _start_time;
148 /** Set the progress of the current part of the job.
149 * @param p Progress (from 0 to 1)
152 Job::set_progress (float p)
154 boost::mutex::scoped_lock lm (_progress_mutex);
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 /** Set that this job's progress will always be unknown */
228 Job::set_progress_unknown ()
230 boost::mutex::scoped_lock lm (_progress_mutex);
231 _progress_unknown = true;
237 float const p = overall_progress ();
238 int const t = elapsed_time ();
241 if (!finished () && p >= 0 && t > 10) {
242 s << rint (p * 100) << "%; about " << seconds_to_approximate_hms (t / p - t) << " remaining";
243 } else if (!finished () && t <= 10) {
244 s << rint (p * 100) << "%";
245 } else if (finished_ok ()) {
246 s << "OK (ran for " << seconds_to_hms (t) << ")";
247 } else if (finished_in_error ()) {
248 s << "Error (" << error() << ")";