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 "dcpomatic_log.h"
32 #include "compose.hpp"
33 #include <dcp/exceptions.h>
34 #include <sub/exceptions.h>
35 #include <boost/thread.hpp>
36 #include <boost/filesystem.hpp>
37 #include <boost/foreach.hpp>
38 #include <boost/date_time/posix_time/posix_time.hpp>
46 using boost::shared_ptr;
47 using boost::optional;
48 using boost::function;
50 /** @param film Associated film, or 0 */
51 Job::Job (shared_ptr<const Film> film)
66 _thread->interrupt ();
67 /* We can't use DCPOMATIC_ASSERT here as it may throw an exception */
68 if (_thread->joinable ()) {
72 /* Too late to do anything about this */
80 /** Start the job in a separate thread, returning immediately */
85 _start_time = time (0);
86 _sub_start_time = time (0);
87 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
88 #ifdef DCPOMATIC_LINUX
89 pthread_setname_np (_thread->native_handle(), "job-wrapper");
93 /** A wrapper for the ::run() method to catch exceptions */
101 } catch (dcp::FileError& e) {
103 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
106 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
107 if (s.available < pow (1024, 3)) {
109 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
115 set_error (e.what(), m);
117 set_state (FINISHED_ERROR);
119 } catch (OpenFileError& e) {
122 String::compose (_("Could not open %1"), e.file().string()),
124 _("DCP-o-matic could not open the file %1 (%2). Perhaps it does not exist or is in an unexpected format."),
125 boost::filesystem::absolute (e.file()).string(),
131 set_state (FINISHED_ERROR);
133 } catch (boost::filesystem::filesystem_error& e) {
135 if (e.code() == boost::system::errc::no_such_file_or_directory) {
137 String::compose (_("Could not open %1"), e.path1().string ()),
139 _("DCP-o-matic could not open the file %1 (%2). Perhaps it does not exist or is in an unexpected format."),
140 boost::filesystem::absolute (e.path1()).string(),
147 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
152 set_state (FINISHED_ERROR);
154 } catch (boost::thread_interrupted &) {
156 set_state (FINISHED_CANCELLED);
158 } catch (sub::SubripError& e) {
160 string extra = "Error is near:\n";
161 BOOST_FOREACH (string i, e.context()) {
165 set_error (e.what (), extra);
167 set_state (FINISHED_ERROR);
169 } catch (std::bad_alloc& e) {
171 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."));
173 set_state (FINISHED_ERROR);
175 } catch (dcp::DCPReadError& e) {
177 set_error (e.message(), e.detail().get_value_or(""));
179 set_state (FINISHED_ERROR);
181 } catch (KDMError& e) {
183 set_error (e.summary(), e.detail());
185 set_state (FINISHED_ERROR);
187 } catch (std::exception& e) {
191 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
195 set_state (FINISHED_ERROR);
201 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
205 set_state (FINISHED_ERROR);
209 /** @return true if this job is new (ie has not started running) */
213 boost::mutex::scoped_lock lm (_state_mutex);
214 return _state == NEW;
217 /** @return true if the job is running */
219 Job::running () const
221 boost::mutex::scoped_lock lm (_state_mutex);
222 return _state == RUNNING;
225 /** @return true if the job has finished (either successfully or unsuccessfully) */
227 Job::finished () const
229 boost::mutex::scoped_lock lm (_state_mutex);
230 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
233 /** @return true if the job has finished successfully */
235 Job::finished_ok () const
237 boost::mutex::scoped_lock lm (_state_mutex);
238 return _state == FINISHED_OK;
241 /** @return true if the job has finished unsuccessfully */
243 Job::finished_in_error () const
245 boost::mutex::scoped_lock lm (_state_mutex);
246 return _state == FINISHED_ERROR;
250 Job::finished_cancelled () const
252 boost::mutex::scoped_lock lm (_state_mutex);
253 return _state == FINISHED_CANCELLED;
257 Job::paused_by_user () const
259 boost::mutex::scoped_lock lm (_state_mutex);
260 return _state == PAUSED_BY_USER;
264 Job::paused_by_priority () const
266 boost::mutex::scoped_lock lm (_state_mutex);
267 return _state == PAUSED_BY_PRIORITY;
270 /** Set the state of this job.
271 * @param s New state.
274 Job::set_state (State s)
276 bool finished = false;
279 boost::mutex::scoped_lock lm (_state_mutex);
282 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
283 _ran_for = time(0) - _start_time;
290 emit (boost::bind (boost::ref (Finished)));
294 /** @return DCPTime (in seconds) that this sub-job has been running */
296 Job::elapsed_sub_time () const
298 if (_sub_start_time == 0) {
302 return time (0) - _sub_start_time;
305 /** Check to see if this job has been interrupted or paused */
307 Job::check_for_interruption_or_pause ()
309 boost::this_thread::interruption_point ();
311 boost::mutex::scoped_lock lm (_state_mutex);
312 while (_state == PAUSED_BY_USER || _state == PAUSED_BY_PRIORITY) {
313 emit (boost::bind (boost::ref (Progress)));
314 _pause_changed.wait (lm);
318 /** Set the progress of the current part of the job.
319 * @param p Progress (from 0 to 1)
320 * @param force Do not ignore this update, even if it hasn't been long since the last one.
323 Job::set_progress (float p, bool force)
325 check_for_interruption_or_pause ();
328 /* Check for excessively frequent progress reporting */
329 boost::mutex::scoped_lock lm (_progress_mutex);
331 gettimeofday (&now, 0);
332 if (_last_progress_update && _last_progress_update->tv_sec > 0) {
333 double const elapsed = (now.tv_sec + now.tv_usec / 1000000.0)
334 - (_last_progress_update->tv_sec + _last_progress_update->tv_usec / 1000000.0);
339 _last_progress_update = now;
342 set_progress_common (p);
346 Job::set_progress_common (optional<float> p)
349 boost::mutex::scoped_lock lm (_progress_mutex);
353 emit (boost::bind (boost::ref (Progress)));
356 /** @return fractional progress of the current sub-job, if known */
358 Job::progress () const
360 boost::mutex::scoped_lock lm (_progress_mutex);
368 boost::mutex::scoped_lock lm (_progress_mutex);
369 LOG_GENERAL ("Sub-job %1 starting", n);
373 set_progress (0, true);
374 _sub_start_time = time (0);
378 Job::error_details () const
380 boost::mutex::scoped_lock lm (_state_mutex);
381 return _error_details;
384 /** @return A summary of any error that the job has generated */
386 Job::error_summary () const
388 boost::mutex::scoped_lock lm (_state_mutex);
389 return _error_summary;
392 /** Set the current error string.
393 * @param s New error string.
394 * @param d New error detail string.
397 Job::set_error (string s, string d)
400 _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), LogEntry::TYPE_ERROR);
403 boost::mutex::scoped_lock lm (_state_mutex);
408 /** Say that this job's progress will be unknown until further notice */
410 Job::set_progress_unknown ()
412 check_for_interruption_or_pause ();
413 set_progress_common (optional<float> ());
416 /** @return Human-readable status of this job */
420 optional<float> p = progress ();
421 int const t = elapsed_sub_time ();
422 int const r = remaining_time ();
425 if (!finished () && p) {
426 int pc = lrintf (p.get() * 100);
428 /* 100% makes it sound like we've finished when we haven't */
433 snprintf (buffer, sizeof(buffer), "%d%%", pc);
436 if (t > 10 && r > 0) {
437 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
438 boost::posix_time::ptime finish = now + boost::posix_time::seconds(r);
439 char finish_string[16];
440 snprintf (finish_string, sizeof(finish_string), "%02d:%02d", int(finish.time_of_day().hours()), int(finish.time_of_day().minutes()));
442 if (now.date() != finish.date()) {
443 /// TRANSLATORS: the %1 in this string will be filled in with a day of the week
444 /// to say what day a job will finish.
445 day = String::compose (_(" on %1"), finish.date().day_of_week().as_long_string());
447 /// TRANSLATORS: "remaining; finishing at" here follows an amount of time that is remaining
448 /// on an operation; after it is an estimated wall-clock completion time.
449 s += String::compose(
450 _("; %1 remaining; finishing at %2%3"),
451 seconds_to_approximate_hms(r), finish_string, day
454 } else if (finished_ok ()) {
455 s = String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
456 } else if (finished_in_error ()) {
457 s = String::compose (_("Error: %1"), error_summary ());
458 } else if (finished_cancelled ()) {
466 Job::json_status () const
468 boost::mutex::scoped_lock lm (_state_mutex);
474 return N_("running");
476 case PAUSED_BY_PRIORITY:
479 return N_("finished_ok");
481 return N_("finished_error");
482 case FINISHED_CANCELLED:
483 return N_("finished_cancelled");
489 /** @return An estimate of the remaining time for this sub-job, in seconds */
491 Job::remaining_time () const
493 if (progress().get_value_or(0) == 0) {
494 return elapsed_sub_time ();
497 return elapsed_sub_time() / progress().get() - elapsed_sub_time();
507 if (paused_by_user() || paused_by_priority()) {
511 _thread->interrupt ();
512 DCPOMATIC_ASSERT (_thread->joinable ());
518 /** @return true if the job was paused, false if it was not running */
520 Job::pause_by_user ()
524 boost::mutex::scoped_lock lm (_state_mutex);
525 /* We can set _state here directly because we have a lock and we aren't
526 setting the job to FINISHED_*
528 if (_state == RUNNING) {
530 _state = PAUSED_BY_USER;
535 _pause_changed.notify_all ();
542 Job::pause_by_priority ()
545 set_state (PAUSED_BY_PRIORITY);
546 _pause_changed.notify_all ();
553 if (paused_by_user() || paused_by_priority()) {
555 _pause_changed.notify_all ();
560 Job::when_finished (boost::signals2::connection& connection, function<void()> finished)
562 boost::mutex::scoped_lock lm (_state_mutex);
563 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
566 connection = Finished.connect (finished);
571 Job::message () const
573 boost::mutex::scoped_lock lm (_state_mutex);
578 Job::set_message (string m)
580 boost::mutex::scoped_lock lm (_state_mutex);