2 Copyright (C) 2012-2014 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.
24 #include <boost/thread.hpp>
25 #include <boost/filesystem.hpp>
26 #include <dcp/exceptions.h>
30 #include "ui_signaller.h"
31 #include "exceptions.h"
40 using std::stringstream;
41 using boost::shared_ptr;
43 Job::Job (shared_ptr<const Film> f)
54 /** Start the job in a separate thread, returning immediately */
59 _start_time = time (0);
60 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
63 /** A wrapper for the ::run() method to catch exceptions */
71 } catch (dcp::FileError& e) {
73 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
76 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
77 if (s.available < pow (1024, 3)) {
79 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
85 set_error (e.what(), m);
87 set_state (FINISHED_ERROR);
89 } catch (OpenFileError& e) {
92 String::compose (_("Could not open %1"), e.file().string()),
93 String::compose (_("DCP-o-matic could not open the file %1. Perhaps it does not exist or is in an unexpected format."), e.file().string())
97 set_state (FINISHED_ERROR);
99 } catch (boost::thread_interrupted &) {
101 set_state (FINISHED_CANCELLED);
103 } catch (std::bad_alloc& e) {
105 set_error (_("Out of memory"), _("There was not enough memory to do this."));
107 set_state (FINISHED_ERROR);
109 } catch (std::exception& e) {
113 _("It is not known what caused this error. Please report the problem to the DCP-o-matic author (carl@dcpomatic.com).")
117 set_state (FINISHED_ERROR);
123 _("It is not known what caused this error. Please report the problem to the DCP-o-matic author (carl@dcpomatic.com).")
127 set_state (FINISHED_ERROR);
131 /** @return true if this job is new (ie has not started running) */
135 boost::mutex::scoped_lock lm (_state_mutex);
136 return _state == NEW;
139 /** @return true if the job is running */
141 Job::running () const
143 boost::mutex::scoped_lock lm (_state_mutex);
144 return _state == RUNNING;
147 /** @return true if the job has finished (either successfully or unsuccessfully) */
149 Job::finished () const
151 boost::mutex::scoped_lock lm (_state_mutex);
152 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
155 /** @return true if the job has finished successfully */
157 Job::finished_ok () const
159 boost::mutex::scoped_lock lm (_state_mutex);
160 return _state == FINISHED_OK;
163 /** @return true if the job has finished unsuccessfully */
165 Job::finished_in_error () const
167 boost::mutex::scoped_lock lm (_state_mutex);
168 return _state == FINISHED_ERROR;
172 Job::finished_cancelled () const
174 boost::mutex::scoped_lock lm (_state_mutex);
175 return _state == FINISHED_CANCELLED;
181 boost::mutex::scoped_lock lm (_state_mutex);
182 return _state == PAUSED;
185 /** Set the state of this job.
186 * @param s New state.
189 Job::set_state (State s)
191 bool finished = false;
194 boost::mutex::scoped_lock lm (_state_mutex);
197 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
198 _ran_for = elapsed_time ();
204 if (finished && ui_signaller) {
205 ui_signaller->emit (boost::bind (boost::ref (Finished)));
209 /** @return DCPTime (in seconds) that this sub-job has been running */
211 Job::elapsed_time () const
213 if (_start_time == 0) {
217 return time (0) - _start_time;
220 /** Set the progress of the current part of the job.
221 * @param p Progress (from 0 to 1)
224 Job::set_progress (float p, bool force)
226 if (!force && fabs (p - progress()) < 0.01) {
227 /* Calm excessive progress reporting */
231 boost::mutex::scoped_lock lm (_progress_mutex);
233 boost::this_thread::interruption_point ();
240 ui_signaller->emit (boost::bind (boost::ref (Progress)));
244 /** @return fractional progress of the current sub-job, or -1 if not known */
246 Job::progress () const
248 boost::mutex::scoped_lock lm (_progress_mutex);
249 return _progress.get_value_or (-1);
256 boost::mutex::scoped_lock lm (_progress_mutex);
260 set_progress (0, true);
264 Job::error_details () const
266 boost::mutex::scoped_lock lm (_state_mutex);
267 return _error_details;
270 /** @return A summary of any error that the job has generated */
272 Job::error_summary () const
274 boost::mutex::scoped_lock lm (_state_mutex);
275 return _error_summary;
278 /** Set the current error string.
279 * @param e New error string.
282 Job::set_error (string s, string d)
284 _film->log()->log (String::compose ("Error in job: %1 (%2)", s, d), Log::TYPE_ERROR);
285 boost::mutex::scoped_lock lm (_state_mutex);
290 /** Say that this job's progress will be unknown until further notice */
292 Job::set_progress_unknown ()
294 boost::mutex::scoped_lock lm (_progress_mutex);
298 /** @return Human-readable status of this job */
302 float const p = progress ();
303 int const t = elapsed_time ();
304 int const r = remaining_time ();
306 int pc = rint (p * 100);
308 /* 100% makes it sound like we've finished when we haven't */
315 if (p >= 0 && t > 10 && r > 0) {
316 /// TRANSLATORS: remaining here follows an amount of time that is remaining
318 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
320 } else if (finished_ok ()) {
321 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
322 } else if (finished_in_error ()) {
323 s << String::compose (_("Error (%1)"), error_summary());
324 } else if (finished_cancelled ()) {
332 Job::json_status () const
334 boost::mutex::scoped_lock lm (_state_mutex);
340 return N_("running");
344 return N_("finished_ok");
346 return N_("finished_error");
347 case FINISHED_CANCELLED:
348 return N_("finished_cancelled");
354 /** @return An estimate of the remaining time for this sub-job, in seconds */
356 Job::remaining_time () const
358 return elapsed_time() / progress() - elapsed_time();
368 _thread->interrupt ();