2 Copyright (C) 2012-2021 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/>.
23 * @brief A parent class to represent long-running tasks which are run in their own thread.
27 #include "compose.hpp"
29 #include "dcpomatic_log.h"
30 #include "exceptions.h"
35 #include <dcp/exceptions.h>
36 #include <sub/exceptions.h>
37 #include <boost/date_time/posix_time/posix_time.hpp>
38 #include <boost/filesystem.hpp>
39 #include <boost/thread.hpp>
48 using std::shared_ptr;
50 using boost::optional;
51 using namespace dcpomatic;
54 /** @param film Associated film, or 0 */
55 Job::Job (shared_ptr<const Film> film)
69 #ifdef DCPOMATIC_DEBUG
70 /* Any subclass should have called stop_thread in its destructor */
71 assert (!_thread.joinable());
79 boost::this_thread::disable_interruption dis;
88 /** Start the job in a separate thread, returning immediately */
93 _start_time = time (0);
94 _sub_start_time = time (0);
95 _thread = boost::thread (boost::bind(&Job::run_wrapper, this));
96 #ifdef DCPOMATIC_LINUX
97 pthread_setname_np (_thread.native_handle(), "job-wrapper");
102 /** A wrapper for the ::run() method to catch exceptions */
106 start_of_thread (String::compose("Job-%1", json_name()));
112 } catch (dcp::FileError& e) {
114 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
117 auto const s = boost::filesystem::space (e.filename());
118 if (s.available < pow (1024, 3)) {
120 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
126 set_error (e.what(), m);
128 set_state (FINISHED_ERROR);
130 } catch (dcp::StartCompressionError& e) {
134 #ifdef DCPOMATIC_WINDOWS
135 #if (__GNUC__ && !__x86_64__)
138 _("Failed to encode the DCP."),
139 _("This error has probably occurred because you are running the 32-bit version of DCP-o-matic and "
140 "trying to use too many encoding threads. Please reduce the 'number of threads DCP-o-matic should "
141 "use' in the General tab of Preferences and try again.")
146 if (running_32_on_64()) {
148 _("Failed to encode the DCP."),
149 _("This error has probably occurred because you are running the 32-bit version of DCP-o-matic. Please re-install DCP-o-matic with the 64-bit installer and try again.")
159 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
164 set_state (FINISHED_ERROR);
166 } catch (OpenFileError& e) {
169 String::compose (_("Could not open %1"), e.file().string()),
171 _("DCP-o-matic could not open the file %1 (%2). Perhaps it does not exist or is in an unexpected format."),
172 boost::filesystem::absolute (e.file()).string(),
178 set_state (FINISHED_ERROR);
180 } catch (boost::filesystem::filesystem_error& e) {
182 if (e.code() == boost::system::errc::no_such_file_or_directory) {
184 String::compose (_("Could not open %1"), e.path1().string ()),
186 _("DCP-o-matic could not open the file %1 (%2). Perhaps it does not exist or is in an unexpected format."),
187 boost::filesystem::absolute (e.path1()).string(),
194 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
199 set_state (FINISHED_ERROR);
201 } catch (boost::thread_interrupted &) {
202 /* The job was cancelled; there's nothing else we need to do here */
203 } catch (sub::SubripError& e) {
205 string extra = "Error is near:\n";
206 for (auto i: e.context()) {
210 set_error (e.what (), extra);
212 set_state (FINISHED_ERROR);
214 } catch (std::bad_alloc& e) {
216 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."));
218 set_state (FINISHED_ERROR);
220 } catch (dcp::ReadError& e) {
222 set_error (e.message(), e.detail().get_value_or(""));
224 set_state (FINISHED_ERROR);
226 } catch (KDMError& e) {
228 set_error (e.summary(), e.detail());
230 set_state (FINISHED_ERROR);
232 } catch (FileError& e) {
234 set_error (e.what(), e.what());
236 set_state (FINISHED_ERROR);
238 } catch (CPLNotFoundError& e) {
242 set_state(FINISHED_ERROR);
244 } catch (std::exception& e) {
248 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
252 set_state (FINISHED_ERROR);
258 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
262 set_state (FINISHED_ERROR);
267 /** @return true if this job is new (ie has not started running) */
271 boost::mutex::scoped_lock lm (_state_mutex);
272 return _state == NEW;
276 /** @return true if the job is running */
278 Job::running () const
280 boost::mutex::scoped_lock lm (_state_mutex);
281 return _state == RUNNING;
285 /** @return true if the job has finished (either successfully or unsuccessfully) */
287 Job::finished () const
289 boost::mutex::scoped_lock lm (_state_mutex);
290 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
294 /** @return true if the job has finished successfully */
296 Job::finished_ok () const
298 boost::mutex::scoped_lock lm (_state_mutex);
299 return _state == FINISHED_OK;
303 /** @return true if the job has finished unsuccessfully */
305 Job::finished_in_error () const
307 boost::mutex::scoped_lock lm (_state_mutex);
308 return _state == FINISHED_ERROR;
313 Job::finished_cancelled () const
315 boost::mutex::scoped_lock lm (_state_mutex);
316 return _state == FINISHED_CANCELLED;
321 Job::paused_by_user () const
323 boost::mutex::scoped_lock lm (_state_mutex);
324 return _state == PAUSED_BY_USER;
329 Job::paused_by_priority () const
331 boost::mutex::scoped_lock lm (_state_mutex);
332 return _state == PAUSED_BY_PRIORITY;
336 /** Set the state of this job.
337 * @param s New state.
340 Job::set_state (State s)
342 bool finished = false;
345 boost::mutex::scoped_lock lm (_state_mutex);
348 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
349 _ran_for = time(0) - _start_time;
356 emit (boost::bind (boost::ref (Finished)));
357 FinishedImmediate ();
362 /** @return DCPTime (in seconds) that this sub-job has been running */
364 Job::elapsed_sub_time () const
366 if (_sub_start_time == 0) {
370 return time (0) - _sub_start_time;
374 /** Check to see if this job has been interrupted or paused */
376 Job::check_for_interruption_or_pause ()
378 boost::this_thread::interruption_point ();
380 boost::mutex::scoped_lock lm (_state_mutex);
381 while (_state == PAUSED_BY_USER || _state == PAUSED_BY_PRIORITY) {
382 emit (boost::bind (boost::ref (Progress)));
383 _pause_changed.wait (lm);
389 Job::seconds_since_last_progress_update () const
391 boost::mutex::scoped_lock lm (_progress_mutex);
392 if (!_last_progress_update) {
397 gettimeofday (&now, 0);
399 return seconds(now) - seconds(*_last_progress_update);
403 /** Set the progress of the current part of the job.
404 * @param p Progress (from 0 to 1)
405 * @param force Do not ignore this update, even if it hasn't been long since the last one.
408 Job::set_progress (float p, bool force)
410 check_for_interruption_or_pause ();
413 /* Check for excessively frequent progress reporting */
414 boost::mutex::scoped_lock lm (_progress_mutex);
416 gettimeofday (&now, 0);
417 if (_last_progress_update && _last_progress_update->tv_sec > 0) {
418 double const elapsed = seconds(now) - seconds(*_last_progress_update);
423 _last_progress_update = now;
426 set_progress_common (p);
431 Job::set_progress_common (optional<float> p)
434 boost::mutex::scoped_lock lm (_progress_mutex);
438 emit (boost::bind (boost::ref (Progress)));
442 /** @return fractional progress of the current sub-job, if known */
444 Job::progress () const
446 boost::mutex::scoped_lock lm (_progress_mutex);
455 boost::mutex::scoped_lock lm (_progress_mutex);
456 LOG_GENERAL ("Sub-job %1 starting", n);
460 set_progress (0, true);
461 _sub_start_time = time (0);
466 Job::error_details () const
468 boost::mutex::scoped_lock lm (_state_mutex);
469 return _error_details;
473 /** @return A summary of any error that the job has generated */
475 Job::error_summary () const
477 boost::mutex::scoped_lock lm (_state_mutex);
478 return _error_summary;
482 /** Set the current error string.
483 * @param s New error string.
484 * @param d New error detail string.
487 Job::set_error (string s, string d)
490 _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), LogEntry::TYPE_ERROR);
493 boost::mutex::scoped_lock lm (_state_mutex);
499 /** Say that this job's progress will be unknown until further notice */
501 Job::set_progress_unknown ()
503 check_for_interruption_or_pause ();
504 set_progress_common (optional<float> ());
508 /** @return Human-readable status of this job */
512 optional<float> p = progress ();
513 int const t = elapsed_sub_time ();
514 int const r = remaining_time ();
517 if (!finished () && p) {
518 int pc = lrintf (p.get() * 100);
520 /* 100% makes it sound like we've finished when we haven't */
525 snprintf (buffer, sizeof(buffer), "%d%%", pc);
528 if (t > 10 && r > 0) {
529 auto now = boost::posix_time::second_clock::local_time();
530 auto finish = now + boost::posix_time::seconds(r);
531 char finish_string[16];
532 snprintf (finish_string, sizeof(finish_string), "%02d:%02d", int(finish.time_of_day().hours()), int(finish.time_of_day().minutes()));
534 if (now.date() != finish.date()) {
535 /// TRANSLATORS: the %1 in this string will be filled in with a day of the week
536 /// to say what day a job will finish.
537 day = String::compose (_(" on %1"), day_of_week_to_string(finish.date().day_of_week()));
539 /// TRANSLATORS: "remaining; finishing at" here follows an amount of time that is remaining
540 /// on an operation; after it is an estimated wall-clock completion time.
541 s += String::compose(
542 _("; %1 remaining; finishing at %2%3"),
543 seconds_to_approximate_hms(r), finish_string, day
546 } else if (finished_ok ()) {
547 s = String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
548 } else if (finished_in_error ()) {
549 s = String::compose (_("Error: %1"), error_summary ());
550 } else if (finished_cancelled ()) {
559 Job::json_status () const
561 boost::mutex::scoped_lock lm (_state_mutex);
567 return N_("running");
569 case PAUSED_BY_PRIORITY:
572 return N_("finished_ok");
574 return N_("finished_error");
575 case FINISHED_CANCELLED:
576 return N_("finished_cancelled");
583 /** @return An estimate of the remaining time for this sub-job, in seconds */
585 Job::remaining_time () const
587 if (progress().get_value_or(0) == 0) {
588 return elapsed_sub_time ();
591 return elapsed_sub_time() / progress().get() - elapsed_sub_time();
598 if (_thread.joinable()) {
601 _thread.interrupt ();
605 set_state (FINISHED_CANCELLED);
609 /** @return true if the job was paused, false if it was not running */
611 Job::pause_by_user ()
615 boost::mutex::scoped_lock lm (_state_mutex);
616 /* We can set _state here directly because we have a lock and we aren't
617 setting the job to FINISHED_*
619 if (_state == RUNNING) {
621 _state = PAUSED_BY_USER;
626 _pause_changed.notify_all ();
634 Job::pause_by_priority ()
637 set_state (PAUSED_BY_PRIORITY);
638 _pause_changed.notify_all ();
646 if (paused_by_user() || paused_by_priority()) {
648 _pause_changed.notify_all ();
654 Job::when_finished (boost::signals2::connection& connection, function<void()> finished)
656 boost::mutex::scoped_lock lm (_state_mutex);
657 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
660 connection = Finished.connect (finished);
666 Job::message () const
668 boost::mutex::scoped_lock lm (_state_mutex);
674 Job::set_message (string m)
676 boost::mutex::scoped_lock lm (_state_mutex);