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 ();
62 DCPOMATIC_ASSERT (_thread->joinable ());
69 /** Start the job in a separate thread, returning immediately */
74 _start_time = time (0);
75 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
78 /** A wrapper for the ::run() method to catch exceptions */
86 } catch (dcp::FileError& e) {
88 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
91 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
92 if (s.available < pow (1024, 3)) {
94 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
100 set_error (e.what(), m);
102 set_state (FINISHED_ERROR);
104 } catch (OpenFileError& e) {
107 String::compose (_("Could not open %1"), e.file().string()),
108 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())
112 set_state (FINISHED_ERROR);
114 } catch (boost::filesystem::filesystem_error& e) {
116 if (e.code() == boost::system::errc::no_such_file_or_directory) {
118 String::compose (_("Could not open %1"), e.path1().string ()),
119 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())
124 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
129 set_state (FINISHED_ERROR);
131 } catch (boost::thread_interrupted &) {
133 set_state (FINISHED_CANCELLED);
135 } catch (std::bad_alloc& e) {
137 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."));
139 set_state (FINISHED_ERROR);
141 } catch (std::exception& e) {
145 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
149 set_state (FINISHED_ERROR);
155 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
159 set_state (FINISHED_ERROR);
163 /** @return true if this job is new (ie has not started running) */
167 boost::mutex::scoped_lock lm (_state_mutex);
168 return _state == NEW;
171 /** @return true if the job is running */
173 Job::running () const
175 boost::mutex::scoped_lock lm (_state_mutex);
176 return _state == RUNNING;
179 /** @return true if the job has finished (either successfully or unsuccessfully) */
181 Job::finished () const
183 boost::mutex::scoped_lock lm (_state_mutex);
184 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
187 /** @return true if the job has finished successfully */
189 Job::finished_ok () const
191 boost::mutex::scoped_lock lm (_state_mutex);
192 return _state == FINISHED_OK;
195 /** @return true if the job has finished unsuccessfully */
197 Job::finished_in_error () const
199 boost::mutex::scoped_lock lm (_state_mutex);
200 return _state == FINISHED_ERROR;
204 Job::finished_cancelled () const
206 boost::mutex::scoped_lock lm (_state_mutex);
207 return _state == FINISHED_CANCELLED;
213 boost::mutex::scoped_lock lm (_state_mutex);
214 return _state == PAUSED;
217 /** Set the state of this job.
218 * @param s New state.
221 Job::set_state (State s)
223 bool finished = false;
226 boost::mutex::scoped_lock lm (_state_mutex);
229 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
230 _ran_for = elapsed_time ();
237 emit (boost::bind (boost::ref (Finished)));
241 /** @return DCPTime (in seconds) that this sub-job has been running */
243 Job::elapsed_time () const
245 if (_start_time == 0) {
249 return time (0) - _start_time;
252 /** Set the progress of the current part of the job.
253 * @param p Progress (from 0 to 1)
256 Job::set_progress (float p, bool force)
258 if (!force && fabs (p - progress().get_value_or(0)) < 0.01) {
259 /* Calm excessive progress reporting */
263 set_progress_common (p);
267 Job::set_progress_common (optional<float> p)
269 boost::mutex::scoped_lock lm (_progress_mutex);
271 boost::this_thread::interruption_point ();
273 boost::mutex::scoped_lock lm2 (_state_mutex);
274 while (_state == PAUSED) {
275 _pause_changed.wait (lm2);
281 emit (boost::bind (boost::ref (Progress)));
284 /** @return fractional progress of the current sub-job, if known */
286 Job::progress () const
288 boost::mutex::scoped_lock lm (_progress_mutex);
296 boost::mutex::scoped_lock lm (_progress_mutex);
300 set_progress (0, true);
304 Job::error_details () const
306 boost::mutex::scoped_lock lm (_state_mutex);
307 return _error_details;
310 /** @return A summary of any error that the job has generated */
312 Job::error_summary () const
314 boost::mutex::scoped_lock lm (_state_mutex);
315 return _error_summary;
318 /** Set the current error string.
319 * @param e New error string.
322 Job::set_error (string s, string d)
327 _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), Log::TYPE_ERROR);
328 boost::mutex::scoped_lock lm (_state_mutex);
333 /** Say that this job's progress will be unknown until further notice */
335 Job::set_progress_unknown ()
337 set_progress_common (optional<float> ());
340 /** @return Human-readable status of this job */
344 optional<float> p = progress ();
345 int const t = elapsed_time ();
346 int const r = remaining_time ();
349 if (!finished () && p) {
350 int pc = lrintf (p.get() * 100);
352 /* 100% makes it sound like we've finished when we haven't */
358 if (t > 10 && r > 0) {
359 /// TRANSLATORS: remaining here follows an amount of time that is remaining
361 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
363 } else if (finished_ok ()) {
364 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
365 } else if (finished_in_error ()) {
366 s << String::compose (_("Error: %1"), error_summary ());
367 } else if (finished_cancelled ()) {
375 Job::json_status () const
377 boost::mutex::scoped_lock lm (_state_mutex);
383 return N_("running");
387 return N_("finished_ok");
389 return N_("finished_error");
390 case FINISHED_CANCELLED:
391 return N_("finished_cancelled");
397 /** @return An estimate of the remaining time for this sub-job, in seconds */
399 Job::remaining_time () const
401 if (progress().get_value_or(0) == 0) {
402 return elapsed_time ();
405 return elapsed_time() / progress().get() - elapsed_time();
419 _thread->interrupt ();
420 DCPOMATIC_ASSERT (_thread->joinable ());
431 _pause_changed.notify_all ();
440 _pause_changed.notify_all ();
445 Job::when_finished (boost::signals2::connection& connection, function<void()> finished)
447 boost::mutex::scoped_lock lm (_state_mutex);
448 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
451 connection = Finished.connect (finished);