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 DCPOMATIC_ASSERT (_thread->joinable ());
72 /** Start the job in a separate thread, returning immediately */
77 _start_time = time (0);
78 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
81 /** A wrapper for the ::run() method to catch exceptions */
89 } catch (dcp::FileError& e) {
91 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
94 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
95 if (s.available < pow (1024, 3)) {
97 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
103 set_error (e.what(), m);
105 set_state (FINISHED_ERROR);
107 } catch (OpenFileError& e) {
110 String::compose (_("Could not open %1"), e.file().string()),
112 _("DCP-o-matic could not open the file %1. Perhaps it does not exist or is in an unexpected format."),
113 boost::filesystem::absolute (e.file()).string()
118 set_state (FINISHED_ERROR);
120 } catch (boost::filesystem::filesystem_error& e) {
122 if (e.code() == boost::system::errc::no_such_file_or_directory) {
124 String::compose (_("Could not open %1"), e.path1().string ()),
126 _("DCP-o-matic could not open the file %1. Perhaps it does not exist or is in an unexpected format."),
127 boost::filesystem::absolute (e.path1()).string()
133 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
138 set_state (FINISHED_ERROR);
140 } catch (boost::thread_interrupted &) {
142 set_state (FINISHED_CANCELLED);
144 } catch (std::bad_alloc& e) {
146 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."));
148 set_state (FINISHED_ERROR);
150 } catch (std::exception& e) {
154 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
158 set_state (FINISHED_ERROR);
164 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
168 set_state (FINISHED_ERROR);
172 /** @return true if this job is new (ie has not started running) */
176 boost::mutex::scoped_lock lm (_state_mutex);
177 return _state == NEW;
180 /** @return true if the job is running */
182 Job::running () const
184 boost::mutex::scoped_lock lm (_state_mutex);
185 return _state == RUNNING;
188 /** @return true if the job has finished (either successfully or unsuccessfully) */
190 Job::finished () const
192 boost::mutex::scoped_lock lm (_state_mutex);
193 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
196 /** @return true if the job has finished successfully */
198 Job::finished_ok () const
200 boost::mutex::scoped_lock lm (_state_mutex);
201 return _state == FINISHED_OK;
204 /** @return true if the job has finished unsuccessfully */
206 Job::finished_in_error () const
208 boost::mutex::scoped_lock lm (_state_mutex);
209 return _state == FINISHED_ERROR;
213 Job::finished_cancelled () const
215 boost::mutex::scoped_lock lm (_state_mutex);
216 return _state == FINISHED_CANCELLED;
222 boost::mutex::scoped_lock lm (_state_mutex);
223 return _state == PAUSED;
226 /** Set the state of this job.
227 * @param s New state.
230 Job::set_state (State s)
232 bool finished = false;
235 boost::mutex::scoped_lock lm (_state_mutex);
238 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
239 _ran_for = elapsed_time ();
246 emit (boost::bind (boost::ref (Finished)));
250 /** @return DCPTime (in seconds) that this sub-job has been running */
252 Job::elapsed_time () const
254 if (_start_time == 0) {
258 return time (0) - _start_time;
261 /** Set the progress of the current part of the job.
262 * @param p Progress (from 0 to 1)
265 Job::set_progress (float p, bool force)
267 if (!force && fabs (p - progress().get_value_or(0)) < 0.01) {
268 /* Calm excessive progress reporting */
272 set_progress_common (p);
276 Job::set_progress_common (optional<float> p)
278 boost::mutex::scoped_lock lm (_progress_mutex);
280 boost::this_thread::interruption_point ();
282 boost::mutex::scoped_lock lm2 (_state_mutex);
283 while (_state == PAUSED) {
284 _pause_changed.wait (lm2);
290 emit (boost::bind (boost::ref (Progress)));
293 /** @return fractional progress of the current sub-job, if known */
295 Job::progress () const
297 boost::mutex::scoped_lock lm (_progress_mutex);
305 boost::mutex::scoped_lock lm (_progress_mutex);
306 LOG_GENERAL ("Sub-job %1 starting", n);
310 set_progress (0, true);
314 Job::error_details () const
316 boost::mutex::scoped_lock lm (_state_mutex);
317 return _error_details;
320 /** @return A summary of any error that the job has generated */
322 Job::error_summary () const
324 boost::mutex::scoped_lock lm (_state_mutex);
325 return _error_summary;
328 /** Set the current error string.
329 * @param e New error string.
332 Job::set_error (string s, string d)
337 _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), LogEntry::TYPE_ERROR);
340 boost::mutex::scoped_lock lm (_state_mutex);
345 /** Say that this job's progress will be unknown until further notice */
347 Job::set_progress_unknown ()
349 set_progress_common (optional<float> ());
352 /** @return Human-readable status of this job */
356 optional<float> p = progress ();
357 int const t = elapsed_time ();
358 int const r = remaining_time ();
361 if (!finished () && p) {
362 int pc = lrintf (p.get() * 100);
364 /* 100% makes it sound like we've finished when we haven't */
370 if (t > 10 && r > 0) {
371 /// TRANSLATORS: remaining here follows an amount of time that is remaining
373 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
375 } else if (finished_ok ()) {
376 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
377 } else if (finished_in_error ()) {
378 s << String::compose (_("Error: %1"), error_summary ());
379 } else if (finished_cancelled ()) {
387 Job::json_status () const
389 boost::mutex::scoped_lock lm (_state_mutex);
395 return N_("running");
399 return N_("finished_ok");
401 return N_("finished_error");
402 case FINISHED_CANCELLED:
403 return N_("finished_cancelled");
409 /** @return An estimate of the remaining time for this sub-job, in seconds */
411 Job::remaining_time () const
413 if (progress().get_value_or(0) == 0) {
414 return elapsed_time ();
417 return elapsed_time() / progress().get() - elapsed_time();
431 _thread->interrupt ();
432 DCPOMATIC_ASSERT (_thread->joinable ());
443 _pause_changed.notify_all ();
452 _pause_changed.notify_all ();
457 Job::when_finished (boost::signals2::connection& connection, function<void()> finished)
459 boost::mutex::scoped_lock lm (_state_mutex);
460 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
463 connection = Finished.connect (finished);