Various markup and tweaks.
[dcpomatic.git] / src / lib / job.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file src/job.cc
21  *  @brief A parent class to represent long-running tasks which are run in their own thread.
22  */
23
24 #include <boost/thread.hpp>
25 #include <boost/filesystem.hpp>
26 #include <libdcp/exceptions.h>
27 #include "job.h"
28 #include "util.h"
29
30 #include "i18n.h"
31
32 using std::string;
33 using std::list;
34 using std::stringstream;
35 using boost::shared_ptr;
36
37 /** @param s Film that we are operating on.
38  */
39 Job::Job (shared_ptr<Film> f)
40         : _film (f)
41         , _state (NEW)
42         , _start_time (0)
43         , _progress_unknown (false)
44         , _ran_for (0)
45 {
46         descend (1);
47 }
48
49 /** Start the job in a separate thread, returning immediately */
50 void
51 Job::start ()
52 {
53         set_state (RUNNING);
54         _start_time = time (0);
55         boost::thread (boost::bind (&Job::run_wrapper, this));
56 }
57
58 /** A wrapper for the ::run() method to catch exceptions */
59 void
60 Job::run_wrapper ()
61 {
62         try {
63
64                 run ();
65
66         } catch (libdcp::FileError& e) {
67                 
68                 set_progress (1);
69                 set_state (FINISHED_ERROR);
70                 
71                 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
72                 
73                 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
74                 if (s.available < pow (1024, 3)) {
75                         m += N_("\n\n");
76                         m += _("The drive that the film is stored on is low in disc space.  Free some more space and try again.");
77                 }
78
79                 set_error (e.what(), m);
80                 
81         } catch (std::exception& e) {
82
83                 set_progress (1);
84                 set_state (FINISHED_ERROR);
85                 set_error (
86                         e.what (),
87                         _("It is not known what caused this error.  The best idea is to report the problem to the DVD-o-matic mailing list (dvdomatic@carlh.net)")
88                         );
89
90         } catch (...) {
91
92                 set_progress (1);
93                 set_state (FINISHED_ERROR);
94                 set_error (
95                         _("Unknown error"),
96                         _("It is not known what caused this error.  The best idea is to report the problem to the DVD-o-matic mailing list (dvdomatic@carlh.net)")
97                         );
98
99         }
100 }
101
102 /** @return true if this job is new (ie has not started running) */
103 bool
104 Job::is_new () const
105 {
106         boost::mutex::scoped_lock lm (_state_mutex);
107         return _state == NEW;
108 }
109
110 /** @return true if the job is running */
111 bool
112 Job::running () const
113 {
114         boost::mutex::scoped_lock lm (_state_mutex);
115         return _state == RUNNING;
116 }
117
118 /** @return true if the job has finished (either successfully or unsuccessfully) */
119 bool
120 Job::finished () const
121 {
122         boost::mutex::scoped_lock lm (_state_mutex);
123         return _state == FINISHED_OK || _state == FINISHED_ERROR;
124 }
125
126 /** @return true if the job has finished successfully */
127 bool
128 Job::finished_ok () const
129 {
130         boost::mutex::scoped_lock lm (_state_mutex);
131         return _state == FINISHED_OK;
132 }
133
134 /** @return true if the job has finished unsuccessfully */
135 bool
136 Job::finished_in_error () const
137 {
138         boost::mutex::scoped_lock lm (_state_mutex);
139         return _state == FINISHED_ERROR;
140 }
141
142 /** Set the state of this job.
143  *  @param s New state.
144  */
145 void
146 Job::set_state (State s)
147 {
148         boost::mutex::scoped_lock lm (_state_mutex);
149         _state = s;
150
151         if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
152                 _ran_for = elapsed_time ();
153                 Finished ();
154         }
155 }
156
157 /** @return Time (in seconds) that this job has been running */
158 int
159 Job::elapsed_time () const
160 {
161         if (_start_time == 0) {
162                 return 0;
163         }
164         
165         return time (0) - _start_time;
166 }
167
168 /** Set the progress of the current part of the job.
169  *  @param p Progress (from 0 to 1)
170  */
171 void
172 Job::set_progress (float p)
173 {
174         boost::mutex::scoped_lock lm (_progress_mutex);
175         _progress_unknown = false;
176         _stack.back().normalised = p;
177 }
178
179 /** @return fractional overall progress, or -1 if not known */
180 float
181 Job::overall_progress () const
182 {
183         boost::mutex::scoped_lock lm (_progress_mutex);
184         if (_progress_unknown) {
185                 return -1;
186         }
187
188         float overall = 0;
189         float factor = 1;
190         for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
191                 factor *= i->allocation;
192                 overall += i->normalised * factor;
193         }
194
195         if (overall > 1) {
196                 overall = 1;
197         }
198         
199         return overall;
200 }
201
202 /** Ascend up one level in terms of progress reporting; see descend() */
203 void
204 Job::ascend ()
205 {
206         boost::mutex::scoped_lock lm (_progress_mutex);
207         
208         assert (!_stack.empty ());
209         float const a = _stack.back().allocation;
210         _stack.pop_back ();
211         _stack.back().normalised += a;
212 }
213
214 /** Descend down one level in terms of progress reporting; e.g. if
215  *  there is a task which is split up into N subtasks, each of which
216  *  report their progress from 0 to 100%, call descend() before executing
217  *  each subtask, and ascend() afterwards to ensure that overall progress
218  *  is reported correctly.
219  *
220  *  @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
221  */
222 void
223 Job::descend (float a)
224 {
225         boost::mutex::scoped_lock lm (_progress_mutex);
226         _stack.push_back (Level (a));
227 }
228
229 string
230 Job::error_details () const
231 {
232         boost::mutex::scoped_lock lm (_state_mutex);
233         return _error_details;
234 }
235
236 /** @return A summary of any error that the job has generated */
237 string
238 Job::error_summary () const
239 {
240         boost::mutex::scoped_lock lm (_state_mutex);
241         return _error_summary;
242 }
243
244 /** Set the current error string.
245  *  @param e New error string.
246  */
247 void
248 Job::set_error (string s, string d)
249 {
250         boost::mutex::scoped_lock lm (_state_mutex);
251         _error_summary = s;
252         _error_details = d;
253 }
254
255 /** Say that this job's progress will be unknown until further notice */
256 void
257 Job::set_progress_unknown ()
258 {
259         boost::mutex::scoped_lock lm (_progress_mutex);
260         _progress_unknown = true;
261 }
262
263 /** @return Human-readable status of this job */
264 string
265 Job::status () const
266 {
267         float const p = overall_progress ();
268         int const t = elapsed_time ();
269         int const r = remaining_time ();
270
271         int pc = rint (p * 100);
272         if (pc == 100) {
273                 /* 100% makes it sound like we've finished when we haven't */
274                 pc = 99;
275         }
276
277         stringstream s;
278         if (!finished ()) {
279                 s << pc << N_("%");
280                 if (p >= 0 && t > 10 && r > 0) {
281                         /// TRANSLATORS: remaining here follows an amount of time that is remaining
282                         /// on an operation.
283                         s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
284                 }
285         } else if (finished_ok ()) {
286                 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
287         } else if (finished_in_error ()) {
288                 s << String::compose (_("Error (%1)"), error_summary());
289         }
290
291         return s.str ();
292 }
293
294 /** @return An estimate of the remaining time for this job, in seconds */
295 int
296 Job::remaining_time () const
297 {
298         return elapsed_time() / overall_progress() - elapsed_time();
299 }