MythTV  master
jobqueue.h
Go to the documentation of this file.
1 #ifndef JOBQUEUE_H_
2 #define JOBQUEUE_H_
3 
4 #include <sys/types.h>
5 
6 #include <QWaitCondition>
7 #include <QDateTime>
8 #include <QRunnable>
9 #include <QObject>
10 #include <QEvent>
11 #include <QMutex>
12 #if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
13 #include <QRecursiveMutex>
14 #endif
15 #include <QMap>
16 
17 #include "mythtvexp.h"
18 #include "mythchrono.h"
19 
20 class MThread;
21 class ProgramInfo;
22 class RecordingInfo;
23 
24 // Strings are used by JobQueue::StatusText()
25 #define JOBSTATUS_MAP(F) \
26  F(JOB_UNKNOWN, 0x0000, JobQueue::tr("Unknown")) \
27  F(JOB_QUEUED, 0x0001, JobQueue::tr("Queued")) \
28  F(JOB_PENDING, 0x0002, JobQueue::tr("Pending")) \
29  F(JOB_STARTING, 0x0003, JobQueue::tr("Starting")) \
30  F(JOB_RUNNING, 0x0004, JobQueue::tr("Running")) \
31  F(JOB_STOPPING, 0x0005, JobQueue::tr("Stopping")) \
32  F(JOB_PAUSED, 0x0006, JobQueue::tr("Paused")) \
33  F(JOB_RETRY, 0x0007, JobQueue::tr("Retrying")) \
34  F(JOB_ERRORING, 0x0008, JobQueue::tr("Erroring")) \
35  F(JOB_ABORTING, 0x0009, JobQueue::tr("Aborting")) \
36  /* \
37  * JOB_DONE is a mask to indicate the job is done no matter what the \
38  * status \
39  */ \
40  F(JOB_DONE, 0x0100, JobQueue::tr("Done (Invalid status!)")) \
41  F(JOB_FINISHED, 0x0110, JobQueue::tr("Finished")) \
42  F(JOB_ABORTED, 0x0120, JobQueue::tr("Aborted")) \
43  F(JOB_ERRORED, 0x0130, JobQueue::tr("Errored")) \
44  F(JOB_CANCELLED, 0x0140, JobQueue::tr("Cancelled")) \
45 
46 enum JobStatus {
47 #define JOBSTATUS_ENUM(A,B,C) A = (B) ,
49 };
50 
51 enum JobCmds {
52  JOB_RUN = 0x0000,
53  JOB_PAUSE = 0x0001,
54  JOB_RESUME = 0x0002,
55  JOB_STOP = 0x0004,
56  JOB_RESTART = 0x0008
57 };
58 
59 enum JobFlags {
60  JOB_NO_FLAGS = 0x0000,
61  JOB_USE_CUTLIST = 0x0001,
62  JOB_LIVE_REC = 0x0002,
63  JOB_EXTERNAL = 0x0004,
64  JOB_REBUILD = 0x0008
65 };
66 
67 enum JobLists {
68  JOB_LIST_ALL = 0x0001,
69  JOB_LIST_DONE = 0x0002,
70  JOB_LIST_NOT_DONE = 0x0004,
71  JOB_LIST_ERROR = 0x0008,
72  JOB_LIST_RECENT = 0x0010
73 };
74 
75 enum JobTypes {
76  JOB_NONE = 0x0000,
77 
78  JOB_SYSTEMJOB = 0x00ff,
79  JOB_TRANSCODE = 0x0001,
80  JOB_COMMFLAG = 0x0002,
81  JOB_METADATA = 0x0004,
82  JOB_PREVIEW = 0x0008,
83 
84  JOB_USERJOB = 0xff00,
85  JOB_USERJOB1 = 0x0100,
86  JOB_USERJOB2 = 0x0200,
87  JOB_USERJOB3 = 0x0400,
88  JOB_USERJOB4 = 0x0800
89 };
90 
91 static QMap< QString, int > JobNameToType {
92  { "Transcode", JOB_TRANSCODE },
93  { "Commflag", JOB_COMMFLAG },
94  { "Metadata", JOB_METADATA },
95  { "UserJob1", JOB_USERJOB1 },
96  { "UserJob2", JOB_USERJOB2 },
97  { "UserJob3", JOB_USERJOB3 },
98  { "UserJob4", JOB_USERJOB4 }
99 };
100 
102  int id {0};
104  QDateTime recstartts;
105  QDateTime schedruntime;
106  QString startts;
107  QDateTime inserttime;
108  int type {0};
109  int cmds {0};
110  int flags {0};
111  int status {0};
112  QDateTime statustime;
113  QString hostname;
114  QString args;
115  QString comment;
116 };
117 
119  int id {0};
120  int type {0};
121  int flag {0};
122  QString desc;
123  QString command;
124  ProgramInfo *pginfo {nullptr};
125 };
126 
127 class JobQueue;
128 
129 class MTV_PUBLIC JobQueue : public QObject, public QRunnable
130 {
131  Q_OBJECT
132 
133  friend class QueueProcessorThread;
134  public:
135  explicit JobQueue(bool master);
136  ~JobQueue(void) override;
137  void customEvent(QEvent *e) override; // QObject
138 
139  static bool QueueRecordingJobs(
140  const RecordingInfo &recinfo, int jobTypes = JOB_NONE);
141  static bool QueueJob(int jobType, uint chanid,
142  const QDateTime &recstartts, const QString& args = "",
143  const QString& comment = "", QString host = "",
144  int flags = 0, int status = JOB_QUEUED,
145  QDateTime schedruntime = QDateTime());
146 
147  static bool QueueJobs(int jobTypes, uint chanid,
148  const QDateTime &recstartts, const QString& args = "",
149  const QString& comment = "", const QString& host = "");
150 
151  static int GetJobID(int jobType, uint chanid,
152  const QDateTime &recstartts);
153  static bool GetJobInfoFromID(int jobID, int &jobType,
154  uint &chanid, QDateTime &recstartts);
155  static bool GetJobInfoFromID(int jobID, int &jobType,
156  uint &chanid, QString &recstartts);
157  static int GetJobTypeFromName(const QString &name);
158 
159  static bool ChangeJobCmds(int jobID, int newCmds);
160  static bool ChangeJobCmds(int jobType, uint chanid,
161  const QDateTime &recstartts, int newCmds);
162  static bool ChangeJobFlags(int jobID, int newFlags);
163  static bool ChangeJobStatus(int jobID, int newStatus,
164  const QString& comment = "");
165  static bool ChangeJobHost(int jobID, const QString& newHostname);
166  static bool ChangeJobComment(int jobID,
167  const QString& comment = "");
168  static bool ChangeJobArgs(int jobID,
169  const QString& args = "");
170  static bool IsJobQueuedOrRunning(int jobType, uint chanid,
171  const QDateTime &recstartts);
172  int GetRunningJobID(uint chanid, const QDateTime &recstartts);
173  static bool IsJobRunning(int jobType, uint chanid,
174  const QDateTime &recstartts);
175  static bool IsJobRunning(int jobType, const ProgramInfo &pginfo);
176  static bool IsJobQueued(int jobType,
177  uint chanid, const QDateTime &recstartts);
178  static bool IsJobStatusQueued(int status);
179  static bool IsJobStatusRunning(int status);
180  static bool PauseJob(int jobID);
181  static bool ResumeJob(int jobID);
182  static bool RestartJob(int jobID);
183  static bool StopJob(int jobID);
184  static bool DeleteJob(int jobID);
185  static bool SafeDeleteJob(int jobID, int jobType, int chanid,
186  const QDateTime& recstartts);
187 
188  static enum JobCmds GetJobCmd(int jobID);
189  static enum JobFlags GetJobFlags(int jobID);
190  static enum JobStatus GetJobStatus(int jobID);
191  static enum JobStatus GetJobStatus(int jobType, uint chanid,
192  const QDateTime &recstartts);
193  static QString GetJobArgs(int jobID);
194  static int UserJobTypeToIndex(int JobType);
195 
196  static bool DeleteAllJobs(uint chanid, const QDateTime &recstartts);
197 
198  static void ClearJobMask(int &mask) { mask = JOB_NONE; }
199  static bool JobIsInMask(int job, int mask) { return (bool)(job & mask); }
200  static bool JobIsNotInMask(int job, int mask)
201  { return ! JobIsInMask(job, mask); }
202  static void AddJobsToMask(int jobs, int &mask) { mask |= jobs; }
203  static void RemoveJobsFromMask(int jobs, int &mask) { mask &= ~jobs; }
204 
205  static QString JobText(int jobType);
206  static QString StatusText(int status);
207 
208  static bool HasRunningOrPendingJobs(std::chrono::minutes startingWithinMins = 0min);
209 
210  static int GetJobsInQueue(QMap<int, JobQueueEntry> &jobs,
211  int findJobs = JOB_LIST_NOT_DONE);
212 
213  static void RecoverQueue(bool justOld = false);
214  static void RecoverOldJobsInQueue()
215  { RecoverQueue(true); }
216  static void CleanupOldJobsInQueue();
217 
218  static bool InJobRunWindow(QDateTime jobstarttsRaw);
219 
220  private:
222  {
223  JobQueue *jq;
224  int jobID;
225  };
226 
227  void run(void) override; // QRunnable
228  void ProcessQueue(void);
229 
230  void ProcessJob(const JobQueueEntry& job);
231 
232  bool AllowedToRun(const JobQueueEntry& job);
233 
234  static bool InJobRunWindow(std::chrono::minutes orStartsWithinMins = 0min);
235 
236  void StartChildJob(void *(*ChildThreadRoutine)(void *), int jobID);
237 
238  static QString GetJobDescription(int jobType);
239  static QString GetJobCommand(int id, int jobType, ProgramInfo *tmpInfo);
240  void RemoveRunningJob(int id);
241 
242  static QString PrettyPrint(off_t bytes);
243 
244  static void *TranscodeThread(void *param);
245  void DoTranscodeThread(int jobID);
246 
247  static void *MetadataLookupThread(void *param);
248  void DoMetadataLookupThread(int jobID);
249 
250  static void *FlagCommercialsThread(void *param);
251  void DoFlagCommercialsThread(int jobID);
252 
253  static void *UserJobThread(void *param);
254  void DoUserJobThread(int jobID);
255 
256  QString m_hostname;
257 
258  int m_jobsRunning {0};
259  int m_jobQueueCPU {0};
260 
261  ProgramInfo *m_pginfo {nullptr};
262 
263 // QMutex m_controlFlagsLock;
264 // QMap<QString, int *> m_jobControlFlags;
265 
266 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
267  QMutex *m_runningJobsLock {nullptr};
268 #else
269  QRecursiveMutex *m_runningJobsLock {nullptr};
270 #endif
271  QMap<int, RunningJobInfo> m_runningJobs;
272 
273  bool m_isMaster;
274 
275  MThread *m_queueThread {nullptr};
276  QWaitCondition m_queueThreadCond;
277  QMutex m_queueThreadCondLock;
278  bool m_processQueue {false};
279 };
280 
281 #endif
282 
283 /* vim: set expandtab tabstop=4 shiftwidth=4: */
JOB_USERJOB3
@ JOB_USERJOB3
Definition: jobqueue.h:84
JOB_USERJOB4
@ JOB_USERJOB4
Definition: jobqueue.h:85
build_compdb.args
args
Definition: build_compdb.py:11
JobQueueEntry::comment
QString comment
Definition: jobqueue.h:112
JobQueueEntry::hostname
QString hostname
Definition: jobqueue.h:110
QueueJob
static int QueueJob(const MythUtilCommandLineParser &cmdline)
Definition: jobutils.cpp:12
JobQueueEntry::inserttime
QDateTime inserttime
Definition: jobqueue.h:104
JOB_REBUILD
@ JOB_REBUILD
Definition: jobqueue.h:61
JobNameToType
static QMap< QString, int > JobNameToType
Definition: jobqueue.h:88
JobQueueEntry::type
int type
Definition: jobqueue.h:105
JOB_SYSTEMJOB
@ JOB_SYSTEMJOB
Definition: jobqueue.h:75
JobQueueEntry
Definition: jobqueue.h:98
JobCmds
JobCmds
Definition: jobqueue.h:48
RunningJobInfo
Definition: jobqueue.h:115
JOB_RUN
@ JOB_RUN
Definition: jobqueue.h:49
RecordingInfo
Holds information on a TV Program one might wish to record.
Definition: recordinginfo.h:35
RunningJobInfo::desc
QString desc
Definition: jobqueue.h:119
mythtvexp.h
JOB_COMMFLAG
@ JOB_COMMFLAG
Definition: jobqueue.h:77
JOB_USE_CUTLIST
@ JOB_USE_CUTLIST
Definition: jobqueue.h:58
JOB_PAUSE
@ JOB_PAUSE
Definition: jobqueue.h:50
JOB_RESUME
@ JOB_RESUME
Definition: jobqueue.h:51
JOB_NONE
@ JOB_NONE
Definition: jobqueue.h:73
JOB_LIST_NOT_DONE
@ JOB_LIST_NOT_DONE
Definition: jobqueue.h:67
JOB_STOP
@ JOB_STOP
Definition: jobqueue.h:52
JOBSTATUS_ENUM
#define JOBSTATUS_ENUM(A, B, C)
Definition: jobqueue.h:44
JOB_USERJOB2
@ JOB_USERJOB2
Definition: jobqueue.h:83
JOB_LIST_ALL
@ JOB_LIST_ALL
Definition: jobqueue.h:65
JOB_PREVIEW
@ JOB_PREVIEW
Definition: jobqueue.h:79
JOB_LIVE_REC
@ JOB_LIVE_REC
Definition: jobqueue.h:59
JobQueueEntry::schedruntime
QDateTime schedruntime
Definition: jobqueue.h:102
RunningJobInfo::command
QString command
Definition: jobqueue.h:120
RunningJobInfo::pginfo
ProgramInfo * pginfo
Definition: jobqueue.h:121
JOBSTATUS_MAP
#define JOBSTATUS_MAP(F)
Definition: jobqueue.h:25
JOB_USERJOB1
@ JOB_USERJOB1
Definition: jobqueue.h:82
JobTypes
JobTypes
Definition: jobqueue.h:72
JobFlags
JobFlags
Definition: jobqueue.h:56
JOB_RESTART
@ JOB_RESTART
Definition: jobqueue.h:53
JobQueueEntry::startts
QString startts
Definition: jobqueue.h:103
JobQueueEntry::args
QString args
Definition: jobqueue.h:111
JobQueueEntry::statustime
QDateTime statustime
Definition: jobqueue.h:109
JobQueueEntry::status
int status
Definition: jobqueue.h:108
JobStatus
JobStatus
Definition: jobqueue.h:43
JobQueueEntry::chanid
uint chanid
Definition: jobqueue.h:100
JobQueueEntry::flags
int flags
Definition: jobqueue.h:107
uint
unsigned int uint
Definition: compat.h:140
RunningJobInfo::flag
int flag
Definition: jobqueue.h:118
RunningJobInfo::type
int type
Definition: jobqueue.h:117
JobLists
JobLists
Definition: jobqueue.h:64
off_t
#define off_t
Definition: mythiowrapper.cpp:240
MTV_PUBLIC
#define MTV_PUBLIC
Definition: mythtvexp.h:15
ProgramInfo
Holds information on recordings and videos.
Definition: programinfo.h:67
JobQueueEntry::cmds
int cmds
Definition: jobqueue.h:106
musicbrainzngs.compat.bytes
bytes
Definition: compat.py:49
JOB_EXTERNAL
@ JOB_EXTERNAL
Definition: jobqueue.h:60
JOB_METADATA
@ JOB_METADATA
Definition: jobqueue.h:78
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
mythchrono.h
JOB_LIST_DONE
@ JOB_LIST_DONE
Definition: jobqueue.h:66
JobQueue
Definition: jobqueue.h:126
JOB_NO_FLAGS
@ JOB_NO_FLAGS
Definition: jobqueue.h:57
JOB_LIST_RECENT
@ JOB_LIST_RECENT
Definition: jobqueue.h:69
JobQueueEntry::recstartts
QDateTime recstartts
Definition: jobqueue.h:101
jobID
int jobID
Definition: mythtv/programs/mythcommflag/main.cpp:82
JOB_LIST_ERROR
@ JOB_LIST_ERROR
Definition: jobqueue.h:68
JOB_TRANSCODE
@ JOB_TRANSCODE
Definition: jobqueue.h:76
JOB_USERJOB
@ JOB_USERJOB
Definition: jobqueue.h:81
JobQueue::JobThreadStruct
Definition: jobqueue.h:218