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.
27 #include "exceptions.h"
30 #include "compose.hpp"
31 #include <dcp/exceptions.h>
32 #include <boost/thread.hpp>
33 #include <boost/filesystem.hpp>
40 using boost::shared_ptr;
41 using boost::optional;
43 #define LOG_ERROR_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_ERROR);
45 Job::Job (shared_ptr<const Film> film)
56 /** Start the job in a separate thread, returning immediately */
61 _start_time = time (0);
62 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
65 /** A wrapper for the ::run() method to catch exceptions */
73 } catch (dcp::FileError& e) {
75 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
78 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
79 if (s.available < pow (1024, 3)) {
81 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
87 set_error (e.what(), m);
89 set_state (FINISHED_ERROR);
91 } catch (OpenFileError& e) {
94 String::compose (_("Could not open %1"), e.file().string()),
95 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())
99 set_state (FINISHED_ERROR);
101 } catch (boost::filesystem::filesystem_error& e) {
103 if (e.code() == boost::system::errc::no_such_file_or_directory) {
105 String::compose (_("Could not open %1"), e.path1().string ()),
106 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())
111 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
116 set_state (FINISHED_ERROR);
118 } catch (boost::thread_interrupted &) {
120 set_state (FINISHED_CANCELLED);
122 } catch (std::bad_alloc& e) {
124 set_error (_("Out of memory"), _("There was not enough memory to do this."));
126 set_state (FINISHED_ERROR);
128 } catch (std::exception& e) {
132 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
136 set_state (FINISHED_ERROR);
142 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
146 set_state (FINISHED_ERROR);
150 /** @return true if this job is new (ie has not started running) */
154 boost::mutex::scoped_lock lm (_state_mutex);
155 return _state == NEW;
158 /** @return true if the job is running */
160 Job::running () const
162 boost::mutex::scoped_lock lm (_state_mutex);
163 return _state == RUNNING;
166 /** @return true if the job has finished (either successfully or unsuccessfully) */
168 Job::finished () const
170 boost::mutex::scoped_lock lm (_state_mutex);
171 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
174 /** @return true if the job has finished successfully */
176 Job::finished_ok () const
178 boost::mutex::scoped_lock lm (_state_mutex);
179 return _state == FINISHED_OK;
182 /** @return true if the job has finished unsuccessfully */
184 Job::finished_in_error () const
186 boost::mutex::scoped_lock lm (_state_mutex);
187 return _state == FINISHED_ERROR;
191 Job::finished_cancelled () const
193 boost::mutex::scoped_lock lm (_state_mutex);
194 return _state == FINISHED_CANCELLED;
200 boost::mutex::scoped_lock lm (_state_mutex);
201 return _state == PAUSED;
204 /** Set the state of this job.
205 * @param s New state.
208 Job::set_state (State s)
210 bool finished = false;
213 boost::mutex::scoped_lock lm (_state_mutex);
216 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
217 _ran_for = elapsed_time ();
224 emit (boost::bind (boost::ref (Finished)));
228 /** @return DCPTime (in seconds) that this sub-job has been running */
230 Job::elapsed_time () const
232 if (_start_time == 0) {
236 return time (0) - _start_time;
239 /** Set the progress of the current part of the job.
240 * @param p Progress (from 0 to 1)
243 Job::set_progress (float p, bool force)
245 if (!force && fabs (p - progress().get_value_or(0)) < 0.01) {
246 /* Calm excessive progress reporting */
250 boost::mutex::scoped_lock lm (_progress_mutex);
252 boost::this_thread::interruption_point ();
254 boost::mutex::scoped_lock lm2 (_state_mutex);
255 while (_state == PAUSED) {
256 _pause_changed.wait (lm2);
262 emit (boost::bind (boost::ref (Progress)));
265 /** @return fractional progress of the current sub-job, if known */
267 Job::progress () const
269 boost::mutex::scoped_lock lm (_progress_mutex);
277 boost::mutex::scoped_lock lm (_progress_mutex);
281 set_progress (0, true);
285 Job::error_details () const
287 boost::mutex::scoped_lock lm (_state_mutex);
288 return _error_details;
291 /** @return A summary of any error that the job has generated */
293 Job::error_summary () const
295 boost::mutex::scoped_lock lm (_state_mutex);
296 return _error_summary;
299 /** Set the current error string.
300 * @param e New error string.
303 Job::set_error (string s, string d)
308 _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), Log::TYPE_ERROR);
309 boost::mutex::scoped_lock lm (_state_mutex);
314 /** Say that this job's progress will be unknown until further notice */
316 Job::set_progress_unknown ()
318 boost::mutex::scoped_lock lm (_progress_mutex);
322 emit (boost::bind (boost::ref (Progress)));
325 /** @return Human-readable status of this job */
329 optional<float> p = progress ();
330 int const t = elapsed_time ();
331 int const r = remaining_time ();
334 if (!finished () && p) {
335 int pc = lrintf (p.get() * 100);
337 /* 100% makes it sound like we've finished when we haven't */
343 if (t > 10 && r > 0) {
344 /// TRANSLATORS: remaining here follows an amount of time that is remaining
346 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
348 } else if (finished_ok ()) {
349 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
350 } else if (finished_in_error ()) {
351 s << String::compose (_("Error: %1"), error_summary ());
352 } else if (finished_cancelled ()) {
360 Job::json_status () const
362 boost::mutex::scoped_lock lm (_state_mutex);
368 return N_("running");
372 return N_("finished_ok");
374 return N_("finished_error");
375 case FINISHED_CANCELLED:
376 return N_("finished_cancelled");
382 /** @return An estimate of the remaining time for this sub-job, in seconds */
384 Job::remaining_time () const
386 if (progress().get_value_or(0) == 0) {
387 return elapsed_time ();
390 return elapsed_time() / progress().get() - elapsed_time();
404 _thread->interrupt ();
413 _pause_changed.notify_all ();
422 _pause_changed.notify_all ();