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()),
111 _("DCP-o-matic could not open the file %1. Perhaps it does not exist or is in an unexpected format."),
112 boost::filesystem::absolute (e.file()).string()
117 set_state (FINISHED_ERROR);
119 } catch (boost::filesystem::filesystem_error& e) {
121 if (e.code() == boost::system::errc::no_such_file_or_directory) {
123 String::compose (_("Could not open %1"), e.path1().string ()),
125 _("DCP-o-matic could not open the file %1. Perhaps it does not exist or is in an unexpected format."),
126 boost::filesystem::absolute (e.path1()).string()
132 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
137 set_state (FINISHED_ERROR);
139 } catch (boost::thread_interrupted &) {
141 set_state (FINISHED_CANCELLED);
143 } catch (std::bad_alloc& e) {
145 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."));
147 set_state (FINISHED_ERROR);
149 } catch (std::exception& e) {
153 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
157 set_state (FINISHED_ERROR);
163 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
167 set_state (FINISHED_ERROR);
171 /** @return true if this job is new (ie has not started running) */
175 boost::mutex::scoped_lock lm (_state_mutex);
176 return _state == NEW;
179 /** @return true if the job is running */
181 Job::running () const
183 boost::mutex::scoped_lock lm (_state_mutex);
184 return _state == RUNNING;
187 /** @return true if the job has finished (either successfully or unsuccessfully) */
189 Job::finished () const
191 boost::mutex::scoped_lock lm (_state_mutex);
192 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
195 /** @return true if the job has finished successfully */
197 Job::finished_ok () const
199 boost::mutex::scoped_lock lm (_state_mutex);
200 return _state == FINISHED_OK;
203 /** @return true if the job has finished unsuccessfully */
205 Job::finished_in_error () const
207 boost::mutex::scoped_lock lm (_state_mutex);
208 return _state == FINISHED_ERROR;
212 Job::finished_cancelled () const
214 boost::mutex::scoped_lock lm (_state_mutex);
215 return _state == FINISHED_CANCELLED;
221 boost::mutex::scoped_lock lm (_state_mutex);
222 return _state == PAUSED;
225 /** Set the state of this job.
226 * @param s New state.
229 Job::set_state (State s)
231 bool finished = false;
234 boost::mutex::scoped_lock lm (_state_mutex);
237 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
238 _ran_for = elapsed_time ();
245 emit (boost::bind (boost::ref (Finished)));
249 /** @return DCPTime (in seconds) that this sub-job has been running */
251 Job::elapsed_time () const
253 if (_start_time == 0) {
257 return time (0) - _start_time;
260 /** Set the progress of the current part of the job.
261 * @param p Progress (from 0 to 1)
264 Job::set_progress (float p, bool force)
266 if (!force && fabs (p - progress().get_value_or(0)) < 0.01) {
267 /* Calm excessive progress reporting */
271 set_progress_common (p);
275 Job::set_progress_common (optional<float> p)
277 boost::mutex::scoped_lock lm (_progress_mutex);
279 boost::this_thread::interruption_point ();
281 boost::mutex::scoped_lock lm2 (_state_mutex);
282 while (_state == PAUSED) {
283 _pause_changed.wait (lm2);
289 emit (boost::bind (boost::ref (Progress)));
292 /** @return fractional progress of the current sub-job, if known */
294 Job::progress () const
296 boost::mutex::scoped_lock lm (_progress_mutex);
304 boost::mutex::scoped_lock lm (_progress_mutex);
305 LOG_GENERAL ("Sub-job %1 starting", n);
309 set_progress (0, true);
313 Job::error_details () const
315 boost::mutex::scoped_lock lm (_state_mutex);
316 return _error_details;
319 /** @return A summary of any error that the job has generated */
321 Job::error_summary () const
323 boost::mutex::scoped_lock lm (_state_mutex);
324 return _error_summary;
327 /** Set the current error string.
328 * @param e New error string.
331 Job::set_error (string s, string d)
336 _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), LogEntry::TYPE_ERROR);
339 boost::mutex::scoped_lock lm (_state_mutex);
344 /** Say that this job's progress will be unknown until further notice */
346 Job::set_progress_unknown ()
348 set_progress_common (optional<float> ());
351 /** @return Human-readable status of this job */
355 optional<float> p = progress ();
356 int const t = elapsed_time ();
357 int const r = remaining_time ();
360 if (!finished () && p) {
361 int pc = lrintf (p.get() * 100);
363 /* 100% makes it sound like we've finished when we haven't */
369 if (t > 10 && r > 0) {
370 /// TRANSLATORS: remaining here follows an amount of time that is remaining
372 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
374 } else if (finished_ok ()) {
375 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
376 } else if (finished_in_error ()) {
377 s << String::compose (_("Error: %1"), error_summary ());
378 } else if (finished_cancelled ()) {
386 Job::json_status () const
388 boost::mutex::scoped_lock lm (_state_mutex);
394 return N_("running");
398 return N_("finished_ok");
400 return N_("finished_error");
401 case FINISHED_CANCELLED:
402 return N_("finished_cancelled");
408 /** @return An estimate of the remaining time for this sub-job, in seconds */
410 Job::remaining_time () const
412 if (progress().get_value_or(0) == 0) {
413 return elapsed_time ();
416 return elapsed_time() / progress().get() - elapsed_time();
430 _thread->interrupt ();
431 DCPOMATIC_ASSERT (_thread->joinable ());
442 _pause_changed.notify_all ();
451 _pause_changed.notify_all ();
456 Job::when_finished (boost::signals2::connection& connection, function<void()> finished)
458 boost::mutex::scoped_lock lm (_state_mutex);
459 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
462 connection = Finished.connect (finished);