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