21d8df0f667215863cfeb1dd30ad4446d77892a6
[dcpomatic.git] / src / lib / job.h
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file src/job.h
22  *  @brief A parent class to represent long-running tasks which are run in their own thread.
23  */
24
25 #ifndef DCPOMATIC_JOB_H
26 #define DCPOMATIC_JOB_H
27
28 #include "signaller.h"
29 #include <boost/thread/mutex.hpp>
30 #include <boost/signals2.hpp>
31 #include <boost/thread.hpp>
32 #include <string>
33
34 class Film;
35
36 /** @class Job
37  *  @brief A parent class to represent long-running tasks which are run in their own thread.
38  */
39 class Job : public std::enable_shared_from_this<Job>, public Signaller
40 {
41 public:
42         explicit Job (std::shared_ptr<const Film> film);
43         virtual ~Job ();
44
45         Job (Job const&) = delete;
46         Job& operator= (Job const&) = delete;
47
48         /** @return user-readable name of this job */
49         virtual std::string name () const = 0;
50         virtual std::string json_name () const = 0;
51         /** Run this job in the current thread. */
52         virtual void run () = 0;
53         /** @return true if it should be possible to notify when this job finishes */
54         virtual bool enable_notify () const {
55                 return false;
56         }
57
58         void start ();
59         virtual void pause() {}
60         bool pause_by_user ();
61         void pause_by_priority ();
62         virtual void resume ();
63         void cancel ();
64
65         bool is_new () const;
66         bool running () const;
67         bool finished () const;
68         bool finished_ok () const;
69         bool finished_in_error () const;
70         bool finished_cancelled () const;
71         bool paused_by_user () const;
72         bool paused_by_priority () const;
73
74         std::string error_summary () const;
75         std::string error_details () const;
76
77         boost::optional<std::string> message () const;
78
79         virtual std::string status () const;
80         std::string json_status () const;
81         std::string sub_name () const {
82                 return _sub_name;
83         }
84
85         void set_progress_unknown ();
86         void set_progress (float, bool force = false);
87         void sub (std::string);
88         boost::optional<float> progress () const;
89         boost::optional<float> seconds_since_last_progress_update () const;
90
91         std::shared_ptr<const Film> film () const {
92                 return _film;
93         }
94
95         enum class Result {
96                 RESULT_OK,
97                 RESULT_ERROR,     // we can't have plain ERROR on Windows
98                 RESULT_CANCELLED
99         };
100
101         void when_finished(boost::signals2::connection& connection, std::function<void(Result)> finished);
102
103         boost::signals2::signal<void()> Progress;
104         /** Emitted from the UI thread when the job is finished */
105         boost::signals2::signal<void (Result)> Finished;
106         /** Emitted from the job thread when the job is finished */
107         boost::signals2::signal<void (Result)> FinishedImmediate;
108
109 protected:
110
111         virtual int remaining_time () const;
112
113         /** Description of a job's state */
114         enum State {
115                 NEW,            ///< the job hasn't been started yet
116                 RUNNING,        ///< the job is running
117                 PAUSED_BY_USER, ///< the job has been paused
118                 PAUSED_BY_PRIORITY, ///< the job has been paused
119                 FINISHED_OK,    ///< the job has finished successfully
120                 FINISHED_ERROR, ///< the job has finished in error
121                 FINISHED_CANCELLED ///< the job was cancelled
122         };
123
124         Result state_to_result(State state) const;
125         void set_state (State);
126         void set_error (std::string s, std::string d = "");
127         void set_message (std::string m);
128         int elapsed_sub_time () const;
129         void check_for_interruption_or_pause ();
130         void stop_thread ();
131
132         std::shared_ptr<const Film> _film;
133
134 private:
135
136         void run_wrapper ();
137         void set_progress_common (boost::optional<float> p);
138
139         boost::thread _thread;
140
141         /** mutex for _state, _error*, _message */
142         mutable boost::mutex _state_mutex;
143         /** current state of the job */
144         State _state;
145         /** summary of an error that has occurred (when state == FINISHED_ERROR) */
146         std::string _error_summary;
147         std::string _error_details;
148         /** a message that should be given to the user when the job finishes */
149         boost::optional<std::string> _message;
150
151         /** time that this job was started */
152         time_t _start_time;
153         /** time that this sub-job was started */
154         time_t _sub_start_time;
155         std::string _sub_name;
156
157         /** mutex for _progress and _last_progress_update */
158         mutable boost::mutex _progress_mutex;
159         boost::optional<float> _progress;
160         boost::optional<struct timeval> _last_progress_update;
161
162         /** condition to signal changes to pause/resume so that we know when to wake;
163             this could be a general _state_change if it made more sense.
164         */
165         boost::condition_variable _pause_changed;
166
167         int _ran_for;
168 };
169
170 #endif