Add details button to job manager; stretch jobs across the whole of the bottom of...
[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 using std::string;
31 using std::list;
32 using std::stringstream;
33 using boost::shared_ptr;
34
35 /** @param s Film that we are operating on.
36  *  @param req Job that must be completed before this job is run.
37  */
38 Job::Job (shared_ptr<Film> f, shared_ptr<Job> req)
39         : _film (f)
40         , _required (req)
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\nThe drive that the film is stored on is low in disc space.  Free some more space and try again.";
76                 }
77
78                 set_error (e.what(), m);
79                 
80         } catch (std::exception& e) {
81
82                 set_progress (1);
83                 set_state (FINISHED_ERROR);
84                 set_error (
85                         e.what (),
86                         "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)"
87                         );
88
89         } catch (...) {
90
91                 set_progress (1);
92                 set_state (FINISHED_ERROR);
93                 set_error (
94                         "Unknown error",
95                         "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)"
96                         );
97
98         }
99 }
100
101 /** @return true if this job is new (ie has not started running) */
102 bool
103 Job::is_new () const
104 {
105         boost::mutex::scoped_lock lm (_state_mutex);
106         return _state == NEW;
107 }
108
109 /** @return true if the job is running */
110 bool
111 Job::running () const
112 {
113         boost::mutex::scoped_lock lm (_state_mutex);
114         return _state == RUNNING;
115 }
116
117 /** @return true if the job has finished (either successfully or unsuccessfully) */
118 bool
119 Job::finished () const
120 {
121         boost::mutex::scoped_lock lm (_state_mutex);
122         return _state == FINISHED_OK || _state == FINISHED_ERROR;
123 }
124
125 /** @return true if the job has finished successfully */
126 bool
127 Job::finished_ok () const
128 {
129         boost::mutex::scoped_lock lm (_state_mutex);
130         return _state == FINISHED_OK;
131 }
132
133 /** @return true if the job has finished unsuccessfully */
134 bool
135 Job::finished_in_error () const
136 {
137         boost::mutex::scoped_lock lm (_state_mutex);
138         return _state == FINISHED_ERROR;
139 }
140
141 /** Set the state of this job.
142  *  @param s New state.
143  */
144 void
145 Job::set_state (State s)
146 {
147         boost::mutex::scoped_lock lm (_state_mutex);
148         _state = s;
149
150         if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
151                 _ran_for = elapsed_time ();
152                 Finished ();
153         }
154 }
155
156 /** @return Time (in seconds) that this job has been running */
157 int
158 Job::elapsed_time () const
159 {
160         if (_start_time == 0) {
161                 return 0;
162         }
163         
164         return time (0) - _start_time;
165 }
166
167 /** Set the progress of the current part of the job.
168  *  @param p Progress (from 0 to 1)
169  */
170 void
171 Job::set_progress (float p)
172 {
173         boost::mutex::scoped_lock lm (_progress_mutex);
174         _progress_unknown = false;
175         _stack.back().normalised = p;
176 }
177
178 /** @return fractional overall progress, or -1 if not known */
179 float
180 Job::overall_progress () const
181 {
182         boost::mutex::scoped_lock lm (_progress_mutex);
183         if (_progress_unknown) {
184                 return -1;
185         }
186
187         float overall = 0;
188         float factor = 1;
189         for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
190                 factor *= i->allocation;
191                 overall += i->normalised * factor;
192         }
193
194         if (overall > 1) {
195                 overall = 1;
196         }
197         
198         return overall;
199 }
200
201 /** Ascend up one level in terms of progress reporting; see descend() */
202 void
203 Job::ascend ()
204 {
205         boost::mutex::scoped_lock lm (_progress_mutex);
206         
207         assert (!_stack.empty ());
208         float const a = _stack.back().allocation;
209         _stack.pop_back ();
210         _stack.back().normalised += a;
211 }
212
213 /** Descend down one level in terms of progress reporting; e.g. if
214  *  there is a task which is split up into N subtasks, each of which
215  *  report their progress from 0 to 100%, call descend() before executing
216  *  each subtask, and ascend() afterwards to ensure that overall progress
217  *  is reported correctly.
218  *
219  *  @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
220  */
221 void
222 Job::descend (float a)
223 {
224         boost::mutex::scoped_lock lm (_progress_mutex);
225         _stack.push_back (Level (a));
226 }
227
228 string
229 Job::error_details () const
230 {
231         boost::mutex::scoped_lock lm (_state_mutex);
232         return _error_details;
233 }
234
235 /** @return A summary of any error that the job has generated */
236 string
237 Job::error_summary () const
238 {
239         boost::mutex::scoped_lock lm (_state_mutex);
240         return _error_summary;
241 }
242
243 /** Set the current error string.
244  *  @param e New error string.
245  */
246 void
247 Job::set_error (string s, string d)
248 {
249         boost::mutex::scoped_lock lm (_state_mutex);
250         _error_summary = s;
251         _error_details = d;
252 }
253
254 /** Say that this job's progress will be unknown until further notice */
255 void
256 Job::set_progress_unknown ()
257 {
258         boost::mutex::scoped_lock lm (_progress_mutex);
259         _progress_unknown = true;
260 }
261
262 /** @return Human-readable status of this job */
263 string
264 Job::status () const
265 {
266         float const p = overall_progress ();
267         int const t = elapsed_time ();
268         int const r = remaining_time ();
269
270         stringstream s;
271         if (!finished () && p >= 0 && t > 10 && r > 0) {
272                 s << rint (p * 100) << "%; " << seconds_to_approximate_hms (r) << " remaining";
273         } else if (!finished () && (t <= 10 || r == 0)) {
274                 s << rint (p * 100) << "%";
275         } else if (finished_ok ()) {
276                 s << "OK (ran for " << seconds_to_hms (_ran_for) << ")";
277         } else if (finished_in_error ()) {
278                 s << "Error (" << error_summary() << ")";
279         }
280
281         return s.str ();
282 }
283
284 /** @return An estimate of the remaining time for this job, in seconds */
285 int
286 Job::remaining_time () const
287 {
288         return elapsed_time() / overall_progress() - elapsed_time();
289 }