2 Copyright (C) 2012-2014 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 <dcp/exceptions.h>
30 #include "ui_signaller.h"
31 #include "exceptions.h"
40 using boost::shared_ptr;
42 #define LOG_ERROR_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_ERROR);
44 Job::Job (shared_ptr<const Film> f)
55 /** Start the job in a separate thread, returning immediately */
60 _start_time = time (0);
61 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
64 /** A wrapper for the ::run() method to catch exceptions */
72 } catch (dcp::FileError& e) {
74 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
77 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
78 if (s.available < pow (1024, 3)) {
80 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
86 set_error (e.what(), m);
88 set_state (FINISHED_ERROR);
90 } catch (OpenFileError& e) {
93 String::compose (_("Could not open %1"), e.file().string()),
94 String::compose (_("DCP-o-matic could not open the file %1. Perhaps it does not exist or is in an unexpected format."), e.file().string())
98 set_state (FINISHED_ERROR);
100 } catch (boost::thread_interrupted &) {
102 set_state (FINISHED_CANCELLED);
104 } catch (std::bad_alloc& e) {
106 set_error (_("Out of memory"), _("There was not enough memory to do this."));
108 set_state (FINISHED_ERROR);
110 } catch (std::exception& e) {
114 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
118 set_state (FINISHED_ERROR);
124 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
128 set_state (FINISHED_ERROR);
132 /** @return true if this job is new (ie has not started running) */
136 boost::mutex::scoped_lock lm (_state_mutex);
137 return _state == NEW;
140 /** @return true if the job is running */
142 Job::running () const
144 boost::mutex::scoped_lock lm (_state_mutex);
145 return _state == RUNNING;
148 /** @return true if the job has finished (either successfully or unsuccessfully) */
150 Job::finished () const
152 boost::mutex::scoped_lock lm (_state_mutex);
153 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
156 /** @return true if the job has finished successfully */
158 Job::finished_ok () const
160 boost::mutex::scoped_lock lm (_state_mutex);
161 return _state == FINISHED_OK;
164 /** @return true if the job has finished unsuccessfully */
166 Job::finished_in_error () const
168 boost::mutex::scoped_lock lm (_state_mutex);
169 return _state == FINISHED_ERROR;
173 Job::finished_cancelled () const
175 boost::mutex::scoped_lock lm (_state_mutex);
176 return _state == FINISHED_CANCELLED;
182 boost::mutex::scoped_lock lm (_state_mutex);
183 return _state == PAUSED;
186 /** Set the state of this job.
187 * @param s New state.
190 Job::set_state (State s)
192 bool finished = false;
195 boost::mutex::scoped_lock lm (_state_mutex);
198 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
199 _ran_for = elapsed_time ();
205 if (finished && ui_signaller) {
206 ui_signaller->emit (boost::bind (boost::ref (Finished)));
210 /** @return DCPTime (in seconds) that this sub-job has been running */
212 Job::elapsed_time () const
214 if (_start_time == 0) {
218 return time (0) - _start_time;
221 /** Set the progress of the current part of the job.
222 * @param p Progress (from 0 to 1)
225 Job::set_progress (float p, bool force)
227 if (!force && fabs (p - progress()) < 0.01) {
228 /* Calm excessive progress reporting */
232 boost::mutex::scoped_lock lm (_progress_mutex);
234 boost::this_thread::interruption_point ();
236 boost::mutex::scoped_lock lm2 (_state_mutex);
237 while (_state == PAUSED) {
238 _pause_changed.wait (lm2);
242 ui_signaller->emit (boost::bind (boost::ref (Progress)));
246 /** @return fractional progress of the current sub-job, or -1 if not known */
248 Job::progress () const
250 boost::mutex::scoped_lock lm (_progress_mutex);
251 return _progress.get_value_or (-1);
258 boost::mutex::scoped_lock lm (_progress_mutex);
262 set_progress (0, true);
266 Job::error_details () const
268 boost::mutex::scoped_lock lm (_state_mutex);
269 return _error_details;
272 /** @return A summary of any error that the job has generated */
274 Job::error_summary () const
276 boost::mutex::scoped_lock lm (_state_mutex);
277 return _error_summary;
280 /** Set the current error string.
281 * @param e New error string.
284 Job::set_error (string s, string d)
289 _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), Log::TYPE_ERROR);
290 boost::mutex::scoped_lock lm (_state_mutex);
295 /** Say that this job's progress will be unknown until further notice */
297 Job::set_progress_unknown ()
299 boost::mutex::scoped_lock lm (_progress_mutex);
303 /** @return Human-readable status of this job */
307 float const p = progress ();
308 int const t = elapsed_time ();
309 int const r = remaining_time ();
311 int pc = rint (p * 100);
313 /* 100% makes it sound like we've finished when we haven't */
320 if (p >= 0 && t > 10 && r > 0) {
321 /// TRANSLATORS: remaining here follows an amount of time that is remaining
323 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
325 } else if (finished_ok ()) {
326 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
327 } else if (finished_in_error ()) {
328 s << String::compose (_("Error (%1)"), error_summary());
329 } else if (finished_cancelled ()) {
336 /** @return An estimate of the remaining time for this sub-job, in seconds */
338 Job::remaining_time () const
340 return elapsed_time() / progress() - elapsed_time();
350 _thread->interrupt ();
359 _pause_changed.notify_all ();
368 _pause_changed.notify_all ();