Tidy up progress reporting for examine content.
[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 namespace std;
31 using namespace boost;
32
33 /** @param s FilmState for the film that we are operating on.
34  *  @param l A log that we can write to.
35  */
36 Job::Job (shared_ptr<const FilmState> s, Log* l, shared_ptr<Job> req)
37         : _fs (s)
38         , _log (l)
39         , _required (req)
40         , _state (NEW)
41         , _start_time (0)
42         , _progress_unknown (false)
43         , _ran_for (0)
44 {
45         assert (_log);
46         
47         descend (1);
48 }
49
50 /** Start the job in a separate thread, returning immediately */
51 void
52 Job::start ()
53 {
54         set_state (RUNNING);
55         _start_time = time (0);
56         boost::thread (boost::bind (&Job::run_wrapper, this));
57 }
58
59 /** A wrapper for the ::run() method to catch exceptions */
60 void
61 Job::run_wrapper ()
62 {
63         try {
64
65                 run ();
66
67         } catch (libdcp::FileError& e) {
68                 
69                 set_progress (1);
70                 set_state (FINISHED_ERROR);
71                 set_error (String::compose ("%1 (%2)", e.what(), filesystem::path (e.filename()).leaf()));
72                 
73         } catch (std::exception& e) {
74
75                 set_progress (1);
76                 set_state (FINISHED_ERROR);
77                 set_error (e.what ());
78
79         }
80 }
81
82 bool
83 Job::is_new () const
84 {
85         boost::mutex::scoped_lock lm (_state_mutex);
86         return _state == NEW;
87 }
88
89 /** @return true if the job is running */
90 bool
91 Job::running () const
92 {
93         boost::mutex::scoped_lock lm (_state_mutex);
94         return _state == RUNNING;
95 }
96
97 /** @return true if the job has finished (either successfully or unsuccessfully) */
98 bool
99 Job::finished () const
100 {
101         boost::mutex::scoped_lock lm (_state_mutex);
102         return _state == FINISHED_OK || _state == FINISHED_ERROR;
103 }
104
105 /** @return true if the job has finished successfully */
106 bool
107 Job::finished_ok () const
108 {
109         boost::mutex::scoped_lock lm (_state_mutex);
110         return _state == FINISHED_OK;
111 }
112
113 /** @return true if the job has finished unsuccessfully */
114 bool
115 Job::finished_in_error () const
116 {
117         boost::mutex::scoped_lock lm (_state_mutex);
118         return _state == FINISHED_ERROR;
119 }
120
121 /** Set the state of this job.
122  *  @param s New state.
123  */
124 void
125 Job::set_state (State s)
126 {
127         boost::mutex::scoped_lock lm (_state_mutex);
128         _state = s;
129
130         if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
131                 _ran_for = elapsed_time ();
132         }
133 }
134
135 /** A hack to work around our lack of cross-thread
136  *  signalling; this emits Finished, and listeners
137  *  assume that it will be emitted in the GUI thread,
138  *  so this method must be called from the GUI thread.
139  */
140 void
141 Job::emit_finished ()
142 {
143         Finished ();
144 }
145
146 /** @return Time (in seconds) that this job has been running */
147 int
148 Job::elapsed_time () const
149 {
150         if (_start_time == 0) {
151                 return 0;
152         }
153         
154         return time (0) - _start_time;
155 }
156
157 /** Set the progress of the current part of the job.
158  *  @param p Progress (from 0 to 1)
159  */
160 void
161 Job::set_progress (float p)
162 {
163         boost::mutex::scoped_lock lm (_progress_mutex);
164         _progress_unknown = false;
165         _stack.back().normalised = p;
166 }
167
168 /** @return fractional overall progress, or -1 if not known */
169 float
170 Job::overall_progress () const
171 {
172         boost::mutex::scoped_lock lm (_progress_mutex);
173         if (_progress_unknown) {
174                 return -1;
175         }
176
177         float overall = 0;
178         float factor = 1;
179         for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
180                 factor *= i->allocation;
181                 overall += i->normalised * factor;
182         }
183
184         if (overall > 1) {
185                 overall = 1;
186         }
187         
188         return overall;
189 }
190
191 /** Ascend up one level in terms of progress reporting; see descend() */
192 void
193 Job::ascend ()
194 {
195         boost::mutex::scoped_lock lm (_progress_mutex);
196         
197         assert (!_stack.empty ());
198         float const a = _stack.back().allocation;
199         _stack.pop_back ();
200         _stack.back().normalised += a;
201 }
202
203 /** Descend down one level in terms of progress reporting; e.g. if
204  *  there is a task which is split up into N subtasks, each of which
205  *  report their progress from 0 to 100%, call descend() before executing
206  *  each subtask, and ascend() afterwards to ensure that overall progress
207  *  is reported correctly.
208  *
209  *  @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
210  */
211 void
212 Job::descend (float a)
213 {
214         boost::mutex::scoped_lock lm (_progress_mutex);
215         _stack.push_back (Level (a));
216 }
217
218 /** @return Any error string that the job has generated */
219 string
220 Job::error () const
221 {
222         boost::mutex::scoped_lock lm (_state_mutex);
223         return _error;
224 }
225
226 /** Set the current error string.
227  *  @param e New error string.
228  */
229 void
230 Job::set_error (string e)
231 {
232         boost::mutex::scoped_lock lm (_state_mutex);
233         _error = e;
234 }
235
236 /** Say that this job's progress will be unknown until further notice */
237 void
238 Job::set_progress_unknown ()
239 {
240         boost::mutex::scoped_lock lm (_progress_mutex);
241         _progress_unknown = true;
242 }
243
244 /** @return Human-readable status of this job */
245 string
246 Job::status () const
247 {
248         float const p = overall_progress ();
249         int const t = elapsed_time ();
250         int const r = remaining_time ();
251         
252         stringstream s;
253         if (!finished () && p >= 0 && t > 10 && r > 0) {
254                 s << rint (p * 100) << "%; " << seconds_to_approximate_hms (r) << " remaining";
255         } else if (!finished () && (t <= 10 || r == 0)) {
256                 s << rint (p * 100) << "%";
257         } else if (finished_ok ()) {
258                 s << "OK (ran for " << seconds_to_hms (_ran_for) << ")";
259         } else if (finished_in_error ()) {
260                 s << "Error (" << error() << ")";
261         }
262
263         return s.str ();
264 }
265
266 /** @return An estimate of the remaining time for this job, in seconds */
267 int
268 Job::remaining_time () const
269 {
270         return elapsed_time() / overall_progress() - elapsed_time();
271 }