2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 * @brief A parent class to represent long-running tasks which are run in their own thread.
28 #include "exceptions.h"
31 #include "compose.hpp"
32 #include <dcp/exceptions.h>
33 #include <boost/thread.hpp>
34 #include <boost/filesystem.hpp>
42 using boost::shared_ptr;
43 using boost::optional;
44 using boost::function;
46 #define LOG_ERROR_NC(...) _film->log()->log (__VA_ARGS__, LogEntry::TYPE_ERROR);
47 #define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
49 /** @param film Associated film, or 0 */
50 Job::Job (shared_ptr<const Film> film)
64 _thread->interrupt ();
65 /* We can't use DCPOMATIC_ASSERT here as it may throw an exception */
66 if (_thread->joinable ()) {
70 /* Too late to do anything about this */
78 /** Start the job in a separate thread, returning immediately */
83 _start_time = time (0);
84 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
87 /** A wrapper for the ::run() method to catch exceptions */
95 } catch (dcp::FileError& e) {
97 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
100 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
101 if (s.available < pow (1024, 3)) {
103 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
109 set_error (e.what(), m);
111 set_state (FINISHED_ERROR);
113 } catch (OpenFileError& e) {
116 String::compose (_("Could not open %1"), e.file().string()),
118 _("DCP-o-matic could not open the file %1. Perhaps it does not exist or is in an unexpected format."),
119 boost::filesystem::absolute (e.file()).string()
124 set_state (FINISHED_ERROR);
126 } catch (boost::filesystem::filesystem_error& e) {
128 if (e.code() == boost::system::errc::no_such_file_or_directory) {
130 String::compose (_("Could not open %1"), e.path1().string ()),
132 _("DCP-o-matic could not open the file %1. Perhaps it does not exist or is in an unexpected format."),
133 boost::filesystem::absolute (e.path1()).string()
139 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
144 set_state (FINISHED_ERROR);
146 } catch (boost::thread_interrupted &) {
148 set_state (FINISHED_CANCELLED);
150 } catch (std::bad_alloc& e) {
152 set_error (_("Out of memory"), _("There was not enough memory to do this. If you are running a 32-bit operating system try reducing the number of encoding threads in the General tab of Preferences."));
154 set_state (FINISHED_ERROR);
156 } catch (std::exception& e) {
160 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
164 set_state (FINISHED_ERROR);
170 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
174 set_state (FINISHED_ERROR);
178 /** @return true if this job is new (ie has not started running) */
182 boost::mutex::scoped_lock lm (_state_mutex);
183 return _state == NEW;
186 /** @return true if the job is running */
188 Job::running () const
190 boost::mutex::scoped_lock lm (_state_mutex);
191 return _state == RUNNING;
194 /** @return true if the job has finished (either successfully or unsuccessfully) */
196 Job::finished () const
198 boost::mutex::scoped_lock lm (_state_mutex);
199 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
202 /** @return true if the job has finished successfully */
204 Job::finished_ok () const
206 boost::mutex::scoped_lock lm (_state_mutex);
207 return _state == FINISHED_OK;
210 /** @return true if the job has finished unsuccessfully */
212 Job::finished_in_error () const
214 boost::mutex::scoped_lock lm (_state_mutex);
215 return _state == FINISHED_ERROR;
219 Job::finished_cancelled () const
221 boost::mutex::scoped_lock lm (_state_mutex);
222 return _state == FINISHED_CANCELLED;
228 boost::mutex::scoped_lock lm (_state_mutex);
229 return _state == PAUSED;
232 /** Set the state of this job.
233 * @param s New state.
236 Job::set_state (State s)
238 bool finished = false;
241 boost::mutex::scoped_lock lm (_state_mutex);
244 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
245 _ran_for = elapsed_time ();
252 emit (boost::bind (boost::ref (Finished)));
256 /** @return DCPTime (in seconds) that this sub-job has been running */
258 Job::elapsed_time () const
260 if (_start_time == 0) {
264 return time (0) - _start_time;
267 /** Set the progress of the current part of the job.
268 * @param p Progress (from 0 to 1)
271 Job::set_progress (float p, bool force)
273 if (!force && fabs (p - progress().get_value_or(0)) < 0.01) {
274 /* Calm excessive progress reporting */
278 set_progress_common (p);
282 Job::set_progress_common (optional<float> p)
284 boost::mutex::scoped_lock lm (_progress_mutex);
286 boost::this_thread::interruption_point ();
288 boost::mutex::scoped_lock lm2 (_state_mutex);
289 while (_state == PAUSED) {
290 _pause_changed.wait (lm2);
296 emit (boost::bind (boost::ref (Progress)));
299 /** @return fractional progress of the current sub-job, if known */
301 Job::progress () const
303 boost::mutex::scoped_lock lm (_progress_mutex);
311 boost::mutex::scoped_lock lm (_progress_mutex);
312 LOG_GENERAL ("Sub-job %1 starting", n);
316 set_progress (0, true);
320 Job::error_details () const
322 boost::mutex::scoped_lock lm (_state_mutex);
323 return _error_details;
326 /** @return A summary of any error that the job has generated */
328 Job::error_summary () const
330 boost::mutex::scoped_lock lm (_state_mutex);
331 return _error_summary;
334 /** Set the current error string.
335 * @param e New error string.
338 Job::set_error (string s, string d)
343 _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), LogEntry::TYPE_ERROR);
346 boost::mutex::scoped_lock lm (_state_mutex);
351 /** Say that this job's progress will be unknown until further notice */
353 Job::set_progress_unknown ()
355 set_progress_common (optional<float> ());
358 /** @return Human-readable status of this job */
362 optional<float> p = progress ();
363 int const t = elapsed_time ();
364 int const r = remaining_time ();
367 if (!finished () && p) {
368 int pc = lrintf (p.get() * 100);
370 /* 100% makes it sound like we've finished when we haven't */
376 if (t > 10 && r > 0) {
377 /// TRANSLATORS: remaining here follows an amount of time that is remaining
379 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
381 } else if (finished_ok ()) {
382 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
383 } else if (finished_in_error ()) {
384 s << String::compose (_("Error: %1"), error_summary ());
385 } else if (finished_cancelled ()) {
393 Job::json_status () const
395 boost::mutex::scoped_lock lm (_state_mutex);
401 return N_("running");
405 return N_("finished_ok");
407 return N_("finished_error");
408 case FINISHED_CANCELLED:
409 return N_("finished_cancelled");
415 /** @return An estimate of the remaining time for this sub-job, in seconds */
417 Job::remaining_time () const
419 if (progress().get_value_or(0) == 0) {
420 return elapsed_time ();
423 return elapsed_time() / progress().get() - elapsed_time();
437 _thread->interrupt ();
438 DCPOMATIC_ASSERT (_thread->joinable ());
449 _pause_changed.notify_all ();
458 _pause_changed.notify_all ();
463 Job::when_finished (boost::signals2::connection& connection, function<void()> finished)
465 boost::mutex::scoped_lock lm (_state_mutex);
466 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
469 connection = Finished.connect (finished);