201397f08e0a79c5a75725163ba059bcb231404b
[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                 set_error (String::compose ("%1 (%2)", e.what(), boost::filesystem::path (e.filename()).leaf()));
71                 
72         } catch (std::exception& e) {
73
74                 set_progress (1);
75                 set_state (FINISHED_ERROR);
76                 set_error (e.what ());
77
78         }
79 }
80
81 /** @return true if this job is new (ie has not started running) */
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                 Finished ();
133         }
134 }
135
136 /** @return Time (in seconds) that this job has been running */
137 int
138 Job::elapsed_time () const
139 {
140         if (_start_time == 0) {
141                 return 0;
142         }
143         
144         return time (0) - _start_time;
145 }
146
147 /** Set the progress of the current part of the job.
148  *  @param p Progress (from 0 to 1)
149  */
150 void
151 Job::set_progress (float p)
152 {
153         boost::mutex::scoped_lock lm (_progress_mutex);
154         _progress_unknown = false;
155         _stack.back().normalised = p;
156 }
157
158 /** @return fractional overall progress, or -1 if not known */
159 float
160 Job::overall_progress () const
161 {
162         boost::mutex::scoped_lock lm (_progress_mutex);
163         if (_progress_unknown) {
164                 return -1;
165         }
166
167         float overall = 0;
168         float factor = 1;
169         for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
170                 factor *= i->allocation;
171                 overall += i->normalised * factor;
172         }
173
174         if (overall > 1) {
175                 overall = 1;
176         }
177         
178         return overall;
179 }
180
181 /** Ascend up one level in terms of progress reporting; see descend() */
182 void
183 Job::ascend ()
184 {
185         boost::mutex::scoped_lock lm (_progress_mutex);
186         
187         assert (!_stack.empty ());
188         float const a = _stack.back().allocation;
189         _stack.pop_back ();
190         _stack.back().normalised += a;
191 }
192
193 /** Descend down one level in terms of progress reporting; e.g. if
194  *  there is a task which is split up into N subtasks, each of which
195  *  report their progress from 0 to 100%, call descend() before executing
196  *  each subtask, and ascend() afterwards to ensure that overall progress
197  *  is reported correctly.
198  *
199  *  @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
200  */
201 void
202 Job::descend (float a)
203 {
204         boost::mutex::scoped_lock lm (_progress_mutex);
205         _stack.push_back (Level (a));
206 }
207
208 /** @return Any error string that the job has generated */
209 string
210 Job::error () const
211 {
212         boost::mutex::scoped_lock lm (_state_mutex);
213         return _error;
214 }
215
216 /** Set the current error string.
217  *  @param e New error string.
218  */
219 void
220 Job::set_error (string e)
221 {
222         boost::mutex::scoped_lock lm (_state_mutex);
223         _error = e;
224 }
225
226 /** Say that this job's progress will be unknown until further notice */
227 void
228 Job::set_progress_unknown ()
229 {
230         boost::mutex::scoped_lock lm (_progress_mutex);
231         _progress_unknown = true;
232 }
233
234 /** @return Human-readable status of this job */
235 string
236 Job::status () const
237 {
238         float const p = overall_progress ();
239         int const t = elapsed_time ();
240         int const r = remaining_time ();
241
242         stringstream s;
243         if (!finished () && p >= 0 && t > 10 && r > 0) {
244                 s << rint (p * 100) << "%; " << seconds_to_approximate_hms (r) << " remaining";
245         } else if (!finished () && (t <= 10 || r == 0)) {
246                 s << rint (p * 100) << "%";
247         } else if (finished_ok ()) {
248                 s << "OK (ran for " << seconds_to_hms (_ran_for) << ")";
249         } else if (finished_in_error ()) {
250                 s << "Error (" << error() << ")";
251         }
252
253         return s.str ();
254 }
255
256 /** @return An estimate of the remaining time for this job, in seconds */
257 int
258 Job::remaining_time () const
259 {
260         return elapsed_time() / overall_progress() - elapsed_time();
261 }