2 Copyright (C) 2012-2015 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 "exceptions.h"
39 using boost::shared_ptr;
40 using boost::optional;
42 #define LOG_ERROR_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_ERROR);
44 Job::Job (shared_ptr<const Film> film)
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::filesystem::filesystem_error& e) {
102 if (e.code() == boost::system::errc::no_such_file_or_directory) {
104 String::compose (_("Could not open %1"), e.path1().string ()),
105 String::compose (_("DCP-o-matic could not open the file %1. Perhaps it does not exist or is in an unexpected format."), e.path1().string())
110 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
115 set_state (FINISHED_ERROR);
117 } catch (boost::thread_interrupted &) {
119 set_state (FINISHED_CANCELLED);
121 } catch (std::bad_alloc& e) {
123 set_error (_("Out of memory"), _("There was not enough memory to do this."));
125 set_state (FINISHED_ERROR);
127 } catch (std::exception& e) {
131 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
135 set_state (FINISHED_ERROR);
141 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
145 set_state (FINISHED_ERROR);
149 /** @return true if this job is new (ie has not started running) */
153 boost::mutex::scoped_lock lm (_state_mutex);
154 return _state == NEW;
157 /** @return true if the job is running */
159 Job::running () const
161 boost::mutex::scoped_lock lm (_state_mutex);
162 return _state == RUNNING;
165 /** @return true if the job has finished (either successfully or unsuccessfully) */
167 Job::finished () const
169 boost::mutex::scoped_lock lm (_state_mutex);
170 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
173 /** @return true if the job has finished successfully */
175 Job::finished_ok () const
177 boost::mutex::scoped_lock lm (_state_mutex);
178 return _state == FINISHED_OK;
181 /** @return true if the job has finished unsuccessfully */
183 Job::finished_in_error () const
185 boost::mutex::scoped_lock lm (_state_mutex);
186 return _state == FINISHED_ERROR;
190 Job::finished_cancelled () const
192 boost::mutex::scoped_lock lm (_state_mutex);
193 return _state == FINISHED_CANCELLED;
199 boost::mutex::scoped_lock lm (_state_mutex);
200 return _state == PAUSED;
203 /** Set the state of this job.
204 * @param s New state.
207 Job::set_state (State s)
209 bool finished = false;
212 boost::mutex::scoped_lock lm (_state_mutex);
215 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
216 _ran_for = elapsed_time ();
223 emit (boost::bind (boost::ref (Finished)));
227 /** @return DCPTime (in seconds) that this sub-job has been running */
229 Job::elapsed_time () const
231 if (_start_time == 0) {
235 return time (0) - _start_time;
238 /** Set the progress of the current part of the job.
239 * @param p Progress (from 0 to 1)
242 Job::set_progress (float p, bool force)
244 if (!force && fabs (p - progress().get_value_or(0)) < 0.01) {
245 /* Calm excessive progress reporting */
249 boost::mutex::scoped_lock lm (_progress_mutex);
251 boost::this_thread::interruption_point ();
253 boost::mutex::scoped_lock lm2 (_state_mutex);
254 while (_state == PAUSED) {
255 _pause_changed.wait (lm2);
261 emit (boost::bind (boost::ref (Progress)));
264 /** @return fractional progress of the current sub-job, if known */
266 Job::progress () const
268 boost::mutex::scoped_lock lm (_progress_mutex);
276 boost::mutex::scoped_lock lm (_progress_mutex);
280 set_progress (0, true);
284 Job::error_details () const
286 boost::mutex::scoped_lock lm (_state_mutex);
287 return _error_details;
290 /** @return A summary of any error that the job has generated */
292 Job::error_summary () const
294 boost::mutex::scoped_lock lm (_state_mutex);
295 return _error_summary;
298 /** Set the current error string.
299 * @param e New error string.
302 Job::set_error (string s, string d)
307 _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), Log::TYPE_ERROR);
308 boost::mutex::scoped_lock lm (_state_mutex);
313 /** Say that this job's progress will be unknown until further notice */
315 Job::set_progress_unknown ()
317 boost::mutex::scoped_lock lm (_progress_mutex);
321 emit (boost::bind (boost::ref (Progress)));
324 /** @return Human-readable status of this job */
328 optional<float> p = progress ();
329 int const t = elapsed_time ();
330 int const r = remaining_time ();
333 if (!finished () && p) {
334 int pc = rint (p.get() * 100);
336 /* 100% makes it sound like we've finished when we haven't */
342 if (t > 10 && r > 0) {
343 /// TRANSLATORS: remaining here follows an amount of time that is remaining
345 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
347 } else if (finished_ok ()) {
348 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
349 } else if (finished_in_error ()) {
350 s << String::compose (_("Error: %1"), error_summary ());
351 } else if (finished_cancelled ()) {
359 Job::json_status () const
361 boost::mutex::scoped_lock lm (_state_mutex);
367 return N_("running");
371 return N_("finished_ok");
373 return N_("finished_error");
374 case FINISHED_CANCELLED:
375 return N_("finished_cancelled");
381 /** @return An estimate of the remaining time for this sub-job, in seconds */
383 Job::remaining_time () const
385 if (progress().get_value_or(0) == 0) {
386 return elapsed_time ();
389 return elapsed_time() / progress().get() - elapsed_time();
403 _thread->interrupt ();
412 _pause_changed.notify_all ();
421 _pause_changed.notify_all ();