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