2 Copyright (C) 2012-2016 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 <sub/exceptions.h>
34 #include <boost/thread.hpp>
35 #include <boost/filesystem.hpp>
36 #include <boost/foreach.hpp>
44 using boost::shared_ptr;
45 using boost::optional;
46 using boost::function;
48 #define LOG_ERROR_NC(...) _film->log()->log (__VA_ARGS__, LogEntry::TYPE_ERROR);
49 #define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
51 /** @param film Associated film, or 0 */
52 Job::Job (shared_ptr<const Film> film)
67 _thread->interrupt ();
68 /* We can't use DCPOMATIC_ASSERT here as it may throw an exception */
69 if (_thread->joinable ()) {
73 /* Too late to do anything about this */
81 /** Start the job in a separate thread, returning immediately */
86 _start_time = time (0);
87 _sub_start_time = time (0);
88 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
91 /** A wrapper for the ::run() method to catch exceptions */
99 } catch (dcp::FileError& e) {
101 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
104 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
105 if (s.available < pow (1024, 3)) {
107 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
113 set_error (e.what(), m);
115 set_state (FINISHED_ERROR);
117 } catch (OpenFileError& e) {
120 String::compose (_("Could not open %1"), e.file().string()),
122 _("DCP-o-matic could not open the file %1. Perhaps it does not exist or is in an unexpected format."),
123 boost::filesystem::absolute (e.file()).string()
128 set_state (FINISHED_ERROR);
130 } catch (boost::filesystem::filesystem_error& e) {
132 if (e.code() == boost::system::errc::no_such_file_or_directory) {
134 String::compose (_("Could not open %1"), e.path1().string ()),
136 _("DCP-o-matic could not open the file %1. Perhaps it does not exist or is in an unexpected format."),
137 boost::filesystem::absolute (e.path1()).string()
143 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
148 set_state (FINISHED_ERROR);
150 } catch (boost::thread_interrupted &) {
152 set_state (FINISHED_CANCELLED);
154 } catch (sub::SubripError& e) {
156 string extra = "Error is near:\n";
157 BOOST_FOREACH (string i, e.context()) {
161 set_error (e.what (), extra);
163 set_state (FINISHED_ERROR);
165 } catch (std::bad_alloc& e) {
167 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."));
169 set_state (FINISHED_ERROR);
171 } catch (std::exception& e) {
175 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
179 set_state (FINISHED_ERROR);
185 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
189 set_state (FINISHED_ERROR);
193 /** @return true if this job is new (ie has not started running) */
197 boost::mutex::scoped_lock lm (_state_mutex);
198 return _state == NEW;
201 /** @return true if the job is running */
203 Job::running () const
205 boost::mutex::scoped_lock lm (_state_mutex);
206 return _state == RUNNING;
209 /** @return true if the job has finished (either successfully or unsuccessfully) */
211 Job::finished () const
213 boost::mutex::scoped_lock lm (_state_mutex);
214 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
217 /** @return true if the job has finished successfully */
219 Job::finished_ok () const
221 boost::mutex::scoped_lock lm (_state_mutex);
222 return _state == FINISHED_OK;
225 /** @return true if the job has finished unsuccessfully */
227 Job::finished_in_error () const
229 boost::mutex::scoped_lock lm (_state_mutex);
230 return _state == FINISHED_ERROR;
234 Job::finished_cancelled () const
236 boost::mutex::scoped_lock lm (_state_mutex);
237 return _state == FINISHED_CANCELLED;
243 boost::mutex::scoped_lock lm (_state_mutex);
244 return _state == PAUSED;
247 /** Set the state of this job.
248 * @param s New state.
251 Job::set_state (State s)
253 bool finished = false;
256 boost::mutex::scoped_lock lm (_state_mutex);
259 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
260 _ran_for = time(0) - _start_time;
267 emit (boost::bind (boost::ref (Finished)));
271 /** @return DCPTime (in seconds) that this sub-job has been running */
273 Job::elapsed_sub_time () const
275 if (_sub_start_time == 0) {
279 return time (0) - _sub_start_time;
282 /** Set the progress of the current part of the job.
283 * @param p Progress (from 0 to 1)
286 Job::set_progress (float p, bool force)
289 /* Check for excessively frequent progress reporting */
290 boost::mutex::scoped_lock lm (_progress_mutex);
292 gettimeofday (&now, 0);
293 if (_last_progress_update && _last_progress_update->tv_sec > 0) {
294 double const elapsed = (now.tv_sec + now.tv_usec / 1000000.0)
295 - (_last_progress_update->tv_sec + _last_progress_update->tv_usec / 1000000.0);
300 _last_progress_update = now;
303 set_progress_common (p);
307 Job::set_progress_common (optional<float> p)
309 boost::mutex::scoped_lock lm (_progress_mutex);
311 boost::this_thread::interruption_point ();
313 boost::mutex::scoped_lock lm2 (_state_mutex);
314 while (_state == PAUSED) {
315 _pause_changed.wait (lm2);
321 emit (boost::bind (boost::ref (Progress)));
324 /** @return fractional progress of the current sub-job, if known */
326 Job::progress () const
328 boost::mutex::scoped_lock lm (_progress_mutex);
336 boost::mutex::scoped_lock lm (_progress_mutex);
337 LOG_GENERAL ("Sub-job %1 starting", n);
341 set_progress (0, true);
342 _sub_start_time = time (0);
346 Job::error_details () const
348 boost::mutex::scoped_lock lm (_state_mutex);
349 return _error_details;
352 /** @return A summary of any error that the job has generated */
354 Job::error_summary () const
356 boost::mutex::scoped_lock lm (_state_mutex);
357 return _error_summary;
360 /** Set the current error string.
361 * @param e New error string.
364 Job::set_error (string s, string d)
369 _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), LogEntry::TYPE_ERROR);
372 boost::mutex::scoped_lock lm (_state_mutex);
377 /** Say that this job's progress will be unknown until further notice */
379 Job::set_progress_unknown ()
381 set_progress_common (optional<float> ());
384 /** @return Human-readable status of this job */
388 optional<float> p = progress ();
389 int const t = elapsed_sub_time ();
390 int const r = remaining_time ();
393 if (!finished () && p) {
394 int pc = lrintf (p.get() * 100);
396 /* 100% makes it sound like we've finished when we haven't */
401 snprintf (buffer, sizeof(buffer), "%d%%", pc);
404 if (t > 10 && r > 0) {
405 /// TRANSLATORS: remaining here follows an amount of time that is remaining
407 s += "; " + seconds_to_approximate_hms (r) + " " + _("remaining");
409 } else if (finished_ok ()) {
410 s = String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
411 } else if (finished_in_error ()) {
412 s = String::compose (_("Error: %1"), error_summary ());
413 } else if (finished_cancelled ()) {
421 Job::json_status () const
423 boost::mutex::scoped_lock lm (_state_mutex);
429 return N_("running");
433 return N_("finished_ok");
435 return N_("finished_error");
436 case FINISHED_CANCELLED:
437 return N_("finished_cancelled");
443 /** @return An estimate of the remaining time for this sub-job, in seconds */
445 Job::remaining_time () const
447 if (progress().get_value_or(0) == 0) {
448 return elapsed_sub_time ();
451 return elapsed_sub_time() / progress().get() - elapsed_sub_time();
465 _thread->interrupt ();
466 DCPOMATIC_ASSERT (_thread->joinable ());
477 _pause_changed.notify_all ();
486 _pause_changed.notify_all ();
491 Job::when_finished (boost::signals2::connection& connection, function<void()> finished)
493 boost::mutex::scoped_lock lm (_state_mutex);
494 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
497 connection = Finished.connect (finished);