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__, LogEntry::TYPE_ERROR);
46 #define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
48 /** @param film Associated film, or 0 */
49 Job::Job (shared_ptr<const Film> film)
63 _thread->interrupt ();
64 DCPOMATIC_ASSERT (_thread->joinable ());
71 /** Start the job in a separate thread, returning immediately */
76 _start_time = time (0);
77 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
80 /** A wrapper for the ::run() method to catch exceptions */
88 } catch (dcp::FileError& e) {
90 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
93 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
94 if (s.available < pow (1024, 3)) {
96 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
102 set_error (e.what(), m);
104 set_state (FINISHED_ERROR);
106 } catch (OpenFileError& e) {
109 String::compose (_("Could not open %1"), e.file().string()),
110 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())
114 set_state (FINISHED_ERROR);
116 } catch (boost::filesystem::filesystem_error& e) {
118 if (e.code() == boost::system::errc::no_such_file_or_directory) {
120 String::compose (_("Could not open %1"), e.path1().string ()),
121 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())
126 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
131 set_state (FINISHED_ERROR);
133 } catch (boost::thread_interrupted &) {
135 set_state (FINISHED_CANCELLED);
137 } catch (std::bad_alloc& e) {
139 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."));
141 set_state (FINISHED_ERROR);
143 } catch (std::exception& e) {
147 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
151 set_state (FINISHED_ERROR);
157 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
161 set_state (FINISHED_ERROR);
165 /** @return true if this job is new (ie has not started running) */
169 boost::mutex::scoped_lock lm (_state_mutex);
170 return _state == NEW;
173 /** @return true if the job is running */
175 Job::running () const
177 boost::mutex::scoped_lock lm (_state_mutex);
178 return _state == RUNNING;
181 /** @return true if the job has finished (either successfully or unsuccessfully) */
183 Job::finished () const
185 boost::mutex::scoped_lock lm (_state_mutex);
186 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
189 /** @return true if the job has finished successfully */
191 Job::finished_ok () const
193 boost::mutex::scoped_lock lm (_state_mutex);
194 return _state == FINISHED_OK;
197 /** @return true if the job has finished unsuccessfully */
199 Job::finished_in_error () const
201 boost::mutex::scoped_lock lm (_state_mutex);
202 return _state == FINISHED_ERROR;
206 Job::finished_cancelled () const
208 boost::mutex::scoped_lock lm (_state_mutex);
209 return _state == FINISHED_CANCELLED;
215 boost::mutex::scoped_lock lm (_state_mutex);
216 return _state == PAUSED;
219 /** Set the state of this job.
220 * @param s New state.
223 Job::set_state (State s)
225 bool finished = false;
228 boost::mutex::scoped_lock lm (_state_mutex);
231 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
232 _ran_for = elapsed_time ();
239 emit (boost::bind (boost::ref (Finished)));
243 /** @return DCPTime (in seconds) that this sub-job has been running */
245 Job::elapsed_time () const
247 if (_start_time == 0) {
251 return time (0) - _start_time;
254 /** Set the progress of the current part of the job.
255 * @param p Progress (from 0 to 1)
258 Job::set_progress (float p, bool force)
260 if (!force && fabs (p - progress().get_value_or(0)) < 0.01) {
261 /* Calm excessive progress reporting */
265 set_progress_common (p);
269 Job::set_progress_common (optional<float> p)
271 boost::mutex::scoped_lock lm (_progress_mutex);
273 boost::this_thread::interruption_point ();
275 boost::mutex::scoped_lock lm2 (_state_mutex);
276 while (_state == PAUSED) {
277 _pause_changed.wait (lm2);
283 emit (boost::bind (boost::ref (Progress)));
286 /** @return fractional progress of the current sub-job, if known */
288 Job::progress () const
290 boost::mutex::scoped_lock lm (_progress_mutex);
298 boost::mutex::scoped_lock lm (_progress_mutex);
299 LOG_GENERAL ("Sub-job %1 starting", n);
303 set_progress (0, true);
307 Job::error_details () const
309 boost::mutex::scoped_lock lm (_state_mutex);
310 return _error_details;
313 /** @return A summary of any error that the job has generated */
315 Job::error_summary () const
317 boost::mutex::scoped_lock lm (_state_mutex);
318 return _error_summary;
321 /** Set the current error string.
322 * @param e New error string.
325 Job::set_error (string s, string d)
330 _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), LogEntry::TYPE_ERROR);
333 boost::mutex::scoped_lock lm (_state_mutex);
338 /** Say that this job's progress will be unknown until further notice */
340 Job::set_progress_unknown ()
342 set_progress_common (optional<float> ());
345 /** @return Human-readable status of this job */
349 optional<float> p = progress ();
350 int const t = elapsed_time ();
351 int const r = remaining_time ();
354 if (!finished () && p) {
355 int pc = lrintf (p.get() * 100);
357 /* 100% makes it sound like we've finished when we haven't */
363 if (t > 10 && r > 0) {
364 /// TRANSLATORS: remaining here follows an amount of time that is remaining
366 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
368 } else if (finished_ok ()) {
369 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
370 } else if (finished_in_error ()) {
371 s << String::compose (_("Error: %1"), error_summary ());
372 } else if (finished_cancelled ()) {
380 Job::json_status () const
382 boost::mutex::scoped_lock lm (_state_mutex);
388 return N_("running");
392 return N_("finished_ok");
394 return N_("finished_error");
395 case FINISHED_CANCELLED:
396 return N_("finished_cancelled");
402 /** @return An estimate of the remaining time for this sub-job, in seconds */
404 Job::remaining_time () const
406 if (progress().get_value_or(0) == 0) {
407 return elapsed_time ();
410 return elapsed_time() / progress().get() - elapsed_time();
424 _thread->interrupt ();
425 DCPOMATIC_ASSERT (_thread->joinable ());
436 _pause_changed.notify_all ();
445 _pause_changed.notify_all ();
450 Job::when_finished (boost::signals2::connection& connection, function<void()> finished)
452 boost::mutex::scoped_lock lm (_state_mutex);
453 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
456 connection = Finished.connect (finished);