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>
41 using boost::shared_ptr;
42 using boost::optional;
43 using boost::function;
45 #define LOG_ERROR_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_ERROR);
47 Job::Job (shared_ptr<const Film> film)
61 _thread->interrupt ();
68 /** Start the job in a separate thread, returning immediately */
73 _start_time = time (0);
74 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
77 /** A wrapper for the ::run() method to catch exceptions */
85 } catch (dcp::FileError& e) {
87 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
90 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
91 if (s.available < pow (1024, 3)) {
93 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
99 set_error (e.what(), m);
101 set_state (FINISHED_ERROR);
103 } catch (OpenFileError& e) {
106 String::compose (_("Could not open %1"), e.file().string()),
107 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())
111 set_state (FINISHED_ERROR);
113 } catch (boost::filesystem::filesystem_error& e) {
115 if (e.code() == boost::system::errc::no_such_file_or_directory) {
117 String::compose (_("Could not open %1"), e.path1().string ()),
118 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())
123 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
128 set_state (FINISHED_ERROR);
130 } catch (boost::thread_interrupted &) {
132 set_state (FINISHED_CANCELLED);
134 } catch (std::bad_alloc& e) {
136 set_error (_("Out of memory"), _("There was not enough memory to do this."));
138 set_state (FINISHED_ERROR);
140 } catch (std::exception& e) {
144 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
148 set_state (FINISHED_ERROR);
154 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
158 set_state (FINISHED_ERROR);
162 /** @return true if this job is new (ie has not started running) */
166 boost::mutex::scoped_lock lm (_state_mutex);
167 return _state == NEW;
170 /** @return true if the job is running */
172 Job::running () const
174 boost::mutex::scoped_lock lm (_state_mutex);
175 return _state == RUNNING;
178 /** @return true if the job has finished (either successfully or unsuccessfully) */
180 Job::finished () const
182 boost::mutex::scoped_lock lm (_state_mutex);
183 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
186 /** @return true if the job has finished successfully */
188 Job::finished_ok () const
190 boost::mutex::scoped_lock lm (_state_mutex);
191 return _state == FINISHED_OK;
194 /** @return true if the job has finished unsuccessfully */
196 Job::finished_in_error () const
198 boost::mutex::scoped_lock lm (_state_mutex);
199 return _state == FINISHED_ERROR;
203 Job::finished_cancelled () const
205 boost::mutex::scoped_lock lm (_state_mutex);
206 return _state == FINISHED_CANCELLED;
212 boost::mutex::scoped_lock lm (_state_mutex);
213 return _state == PAUSED;
216 /** Set the state of this job.
217 * @param s New state.
220 Job::set_state (State s)
222 bool finished = false;
225 boost::mutex::scoped_lock lm (_state_mutex);
228 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
229 _ran_for = elapsed_time ();
236 emit (boost::bind (boost::ref (Finished)));
240 /** @return DCPTime (in seconds) that this sub-job has been running */
242 Job::elapsed_time () const
244 if (_start_time == 0) {
248 return time (0) - _start_time;
251 /** Set the progress of the current part of the job.
252 * @param p Progress (from 0 to 1)
255 Job::set_progress (float p, bool force)
257 if (!force && fabs (p - progress().get_value_or(0)) < 0.01) {
258 /* Calm excessive progress reporting */
262 set_progress_common (p);
266 Job::set_progress_common (optional<float> p)
268 boost::mutex::scoped_lock lm (_progress_mutex);
270 boost::this_thread::interruption_point ();
272 boost::mutex::scoped_lock lm2 (_state_mutex);
273 while (_state == PAUSED) {
274 _pause_changed.wait (lm2);
280 emit (boost::bind (boost::ref (Progress)));
283 /** @return fractional progress of the current sub-job, if known */
285 Job::progress () const
287 boost::mutex::scoped_lock lm (_progress_mutex);
295 boost::mutex::scoped_lock lm (_progress_mutex);
299 set_progress (0, true);
303 Job::error_details () const
305 boost::mutex::scoped_lock lm (_state_mutex);
306 return _error_details;
309 /** @return A summary of any error that the job has generated */
311 Job::error_summary () const
313 boost::mutex::scoped_lock lm (_state_mutex);
314 return _error_summary;
317 /** Set the current error string.
318 * @param e New error string.
321 Job::set_error (string s, string d)
326 _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), Log::TYPE_ERROR);
327 boost::mutex::scoped_lock lm (_state_mutex);
332 /** Say that this job's progress will be unknown until further notice */
334 Job::set_progress_unknown ()
336 set_progress_common (optional<float> ());
339 /** @return Human-readable status of this job */
343 optional<float> p = progress ();
344 int const t = elapsed_time ();
345 int const r = remaining_time ();
348 if (!finished () && p) {
349 int pc = lrintf (p.get() * 100);
351 /* 100% makes it sound like we've finished when we haven't */
357 if (t > 10 && r > 0) {
358 /// TRANSLATORS: remaining here follows an amount of time that is remaining
360 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
362 } else if (finished_ok ()) {
363 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
364 } else if (finished_in_error ()) {
365 s << String::compose (_("Error: %1"), error_summary ());
366 } else if (finished_cancelled ()) {
374 Job::json_status () const
376 boost::mutex::scoped_lock lm (_state_mutex);
382 return N_("running");
386 return N_("finished_ok");
388 return N_("finished_error");
389 case FINISHED_CANCELLED:
390 return N_("finished_cancelled");
396 /** @return An estimate of the remaining time for this sub-job, in seconds */
398 Job::remaining_time () const
400 if (progress().get_value_or(0) == 0) {
401 return elapsed_time ();
404 return elapsed_time() / progress().get() - elapsed_time();
418 _thread->interrupt ();
427 _pause_changed.notify_all ();
436 _pause_changed.notify_all ();
441 Job::when_finished (boost::signals2::connection& connection, function<void()> finished)
443 boost::mutex::scoped_lock lm (_state_mutex);
444 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
447 connection = Finished.connect (finished);