2 Copyright (C) 2012-2019 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;
49 using namespace dcpomatic;
51 /** @param film Associated film, or 0 */
52 Job::Job (shared_ptr<const Film> film)
73 _thread->interrupt ();
74 /* We can't use DCPOMATIC_ASSERT here as it may throw an exception */
75 if (_thread->joinable ()) {
79 /* Too late to do anything about this */
88 /** Start the job in a separate thread, returning immediately */
93 _start_time = time (0);
94 _sub_start_time = time (0);
95 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
96 #ifdef DCPOMATIC_LINUX
97 pthread_setname_np (_thread->native_handle(), "job-wrapper");
101 /** A wrapper for the ::run() method to catch exceptions */
109 } catch (dcp::FileError& e) {
111 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
114 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
115 if (s.available < pow (1024, 3)) {
117 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
123 set_error (e.what(), m);
125 set_state (FINISHED_ERROR);
127 } catch (OpenFileError& e) {
130 String::compose (_("Could not open %1"), e.file().string()),
132 _("DCP-o-matic could not open the file %1 (%2). Perhaps it does not exist or is in an unexpected format."),
133 boost::filesystem::absolute (e.file()).string(),
139 set_state (FINISHED_ERROR);
141 } catch (boost::filesystem::filesystem_error& e) {
143 if (e.code() == boost::system::errc::no_such_file_or_directory) {
145 String::compose (_("Could not open %1"), e.path1().string ()),
147 _("DCP-o-matic could not open the file %1 (%2). Perhaps it does not exist or is in an unexpected format."),
148 boost::filesystem::absolute (e.path1()).string(),
155 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
160 set_state (FINISHED_ERROR);
162 } catch (boost::thread_interrupted &) {
164 set_state (FINISHED_CANCELLED);
166 } catch (sub::SubripError& e) {
168 string extra = "Error is near:\n";
169 BOOST_FOREACH (string i, e.context()) {
173 set_error (e.what (), extra);
175 set_state (FINISHED_ERROR);
177 } catch (std::bad_alloc& e) {
179 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."));
181 set_state (FINISHED_ERROR);
183 } catch (dcp::DCPReadError& e) {
185 set_error (e.message(), e.detail().get_value_or(""));
187 set_state (FINISHED_ERROR);
189 } catch (KDMError& e) {
191 set_error (e.summary(), e.detail());
193 set_state (FINISHED_ERROR);
195 } catch (FileError& e) {
197 set_error (e.what(), e.what());
199 set_state (FINISHED_ERROR);
201 } catch (std::exception& e) {
205 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
209 set_state (FINISHED_ERROR);
215 string (_("It is not known what caused this error.")) + " " + REPORT_PROBLEM
219 set_state (FINISHED_ERROR);
223 /** @return true if this job is new (ie has not started running) */
227 boost::mutex::scoped_lock lm (_state_mutex);
228 return _state == NEW;
231 /** @return true if the job is running */
233 Job::running () const
235 boost::mutex::scoped_lock lm (_state_mutex);
236 return _state == RUNNING;
239 /** @return true if the job has finished (either successfully or unsuccessfully) */
241 Job::finished () const
243 boost::mutex::scoped_lock lm (_state_mutex);
244 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
247 /** @return true if the job has finished successfully */
249 Job::finished_ok () const
251 boost::mutex::scoped_lock lm (_state_mutex);
252 return _state == FINISHED_OK;
255 /** @return true if the job has finished unsuccessfully */
257 Job::finished_in_error () const
259 boost::mutex::scoped_lock lm (_state_mutex);
260 return _state == FINISHED_ERROR;
264 Job::finished_cancelled () const
266 boost::mutex::scoped_lock lm (_state_mutex);
267 return _state == FINISHED_CANCELLED;
271 Job::paused_by_user () const
273 boost::mutex::scoped_lock lm (_state_mutex);
274 return _state == PAUSED_BY_USER;
278 Job::paused_by_priority () const
280 boost::mutex::scoped_lock lm (_state_mutex);
281 return _state == PAUSED_BY_PRIORITY;
284 /** Set the state of this job.
285 * @param s New state.
288 Job::set_state (State s)
290 bool finished = false;
293 boost::mutex::scoped_lock lm (_state_mutex);
296 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
297 _ran_for = time(0) - _start_time;
304 emit (boost::bind (boost::ref (Finished)));
305 FinishedImmediate ();
309 /** @return DCPTime (in seconds) that this sub-job has been running */
311 Job::elapsed_sub_time () const
313 if (_sub_start_time == 0) {
317 return time (0) - _sub_start_time;
320 /** Check to see if this job has been interrupted or paused */
322 Job::check_for_interruption_or_pause ()
324 boost::this_thread::interruption_point ();
326 boost::mutex::scoped_lock lm (_state_mutex);
327 while (_state == PAUSED_BY_USER || _state == PAUSED_BY_PRIORITY) {
328 emit (boost::bind (boost::ref (Progress)));
329 _pause_changed.wait (lm);
333 /** Set the progress of the current part of the job.
334 * @param p Progress (from 0 to 1)
335 * @param force Do not ignore this update, even if it hasn't been long since the last one.
338 Job::set_progress (float p, bool force)
340 check_for_interruption_or_pause ();
343 /* Check for excessively frequent progress reporting */
344 boost::mutex::scoped_lock lm (_progress_mutex);
346 gettimeofday (&now, 0);
347 if (_last_progress_update && _last_progress_update->tv_sec > 0) {
348 double const elapsed = (now.tv_sec + now.tv_usec / 1000000.0)
349 - (_last_progress_update->tv_sec + _last_progress_update->tv_usec / 1000000.0);
354 _last_progress_update = now;
357 set_progress_common (p);
361 Job::set_progress_common (optional<float> p)
364 boost::mutex::scoped_lock lm (_progress_mutex);
368 emit (boost::bind (boost::ref (Progress)));
371 /** @return fractional progress of the current sub-job, if known */
373 Job::progress () const
375 boost::mutex::scoped_lock lm (_progress_mutex);
383 boost::mutex::scoped_lock lm (_progress_mutex);
384 LOG_GENERAL ("Sub-job %1 starting", n);
388 set_progress (0, true);
389 _sub_start_time = time (0);
393 Job::error_details () const
395 boost::mutex::scoped_lock lm (_state_mutex);
396 return _error_details;
399 /** @return A summary of any error that the job has generated */
401 Job::error_summary () const
403 boost::mutex::scoped_lock lm (_state_mutex);
404 return _error_summary;
407 /** Set the current error string.
408 * @param s New error string.
409 * @param d New error detail string.
412 Job::set_error (string s, string d)
415 _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), LogEntry::TYPE_ERROR);
418 boost::mutex::scoped_lock lm (_state_mutex);
423 /** Say that this job's progress will be unknown until further notice */
425 Job::set_progress_unknown ()
427 check_for_interruption_or_pause ();
428 set_progress_common (optional<float> ());
431 /** @return Human-readable status of this job */
435 optional<float> p = progress ();
436 int const t = elapsed_sub_time ();
437 int const r = remaining_time ();
440 if (!finished () && p) {
441 int pc = lrintf (p.get() * 100);
443 /* 100% makes it sound like we've finished when we haven't */
448 snprintf (buffer, sizeof(buffer), "%d%%", pc);
451 if (t > 10 && r > 0) {
452 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
453 boost::posix_time::ptime finish = now + boost::posix_time::seconds(r);
454 char finish_string[16];
455 snprintf (finish_string, sizeof(finish_string), "%02d:%02d", int(finish.time_of_day().hours()), int(finish.time_of_day().minutes()));
457 if (now.date() != finish.date()) {
458 /// TRANSLATORS: the %1 in this string will be filled in with a day of the week
459 /// to say what day a job will finish.
460 day = String::compose (_(" on %1"), day_of_week_to_string(finish.date().day_of_week()));
462 /// TRANSLATORS: "remaining; finishing at" here follows an amount of time that is remaining
463 /// on an operation; after it is an estimated wall-clock completion time.
464 s += String::compose(
465 _("; %1 remaining; finishing at %2%3"),
466 seconds_to_approximate_hms(r), finish_string, day
469 } else if (finished_ok ()) {
470 s = String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
471 } else if (finished_in_error ()) {
472 s = String::compose (_("Error: %1"), error_summary ());
473 } else if (finished_cancelled ()) {
481 Job::json_status () const
483 boost::mutex::scoped_lock lm (_state_mutex);
489 return N_("running");
491 case PAUSED_BY_PRIORITY:
494 return N_("finished_ok");
496 return N_("finished_error");
497 case FINISHED_CANCELLED:
498 return N_("finished_cancelled");
504 /** @return An estimate of the remaining time for this sub-job, in seconds */
506 Job::remaining_time () const
508 if (progress().get_value_or(0) == 0) {
509 return elapsed_sub_time ();
512 return elapsed_sub_time() / progress().get() - elapsed_sub_time();
522 if (paused_by_user() || paused_by_priority()) {
526 _thread->interrupt ();
527 DCPOMATIC_ASSERT (_thread->joinable ());
533 /** @return true if the job was paused, false if it was not running */
535 Job::pause_by_user ()
539 boost::mutex::scoped_lock lm (_state_mutex);
540 /* We can set _state here directly because we have a lock and we aren't
541 setting the job to FINISHED_*
543 if (_state == RUNNING) {
545 _state = PAUSED_BY_USER;
550 _pause_changed.notify_all ();
557 Job::pause_by_priority ()
560 set_state (PAUSED_BY_PRIORITY);
561 _pause_changed.notify_all ();
568 if (paused_by_user() || paused_by_priority()) {
570 _pause_changed.notify_all ();
575 Job::when_finished (boost::signals2::connection& connection, function<void()> finished)
577 boost::mutex::scoped_lock lm (_state_mutex);
578 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
581 connection = Finished.connect (finished);
586 Job::message () const
588 boost::mutex::scoped_lock lm (_state_mutex);
593 Job::set_message (string m)
595 boost::mutex::scoped_lock lm (_state_mutex);