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#include <QRecursiveMutex>
13#include <QMap>
14
15#include "mythtvexp.h"
17
18class MThread;
19class ProgramInfo;
20class RecordingInfo;
21
22// NOLINTBEGIN(cppcoreguidelines-macro-usage)
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#define JOBSTATUS_ENUM(A,B,C) A = (B) ,
49};
51// NOLINTEND(cppcoreguidelines-macro-usage)
53enum JobCmds : std::uint8_t {
54 JOB_RUN = 0x0000,
55 JOB_PAUSE = 0x0001,
56 JOB_RESUME = 0x0002,
57 JOB_STOP = 0x0004,
58 JOB_RESTART = 0x0008
59};
61enum JobFlags : std::uint8_t {
62 JOB_NO_FLAGS = 0x0000,
63 JOB_USE_CUTLIST = 0x0001,
64 JOB_LIVE_REC = 0x0002,
65 JOB_EXTERNAL = 0x0004,
66 JOB_REBUILD = 0x0008
67};
69enum JobLists : std::uint8_t {
70 JOB_LIST_ALL = 0x0001,
71 JOB_LIST_DONE = 0x0002,
73 JOB_LIST_ERROR = 0x0008,
75};
76
78 JOB_NONE = 0x0000,
80 JOB_SYSTEMJOB = 0x00ff,
81 JOB_TRANSCODE = 0x0001,
82 JOB_COMMFLAG = 0x0002,
83 JOB_METADATA = 0x0004,
84 JOB_PREVIEW = 0x0008,
86 JOB_USERJOB = 0xff00,
87 JOB_USERJOB1 = 0x0100,
88 JOB_USERJOB2 = 0x0200,
89 JOB_USERJOB3 = 0x0400,
90 JOB_USERJOB4 = 0x0800
91};
92
93static QMap< QString, int > JobNameToType {
94 { "Transcode", JOB_TRANSCODE },
95 { "Commflag", JOB_COMMFLAG },
96 { "Metadata", JOB_METADATA },
97 { "UserJob1", JOB_USERJOB1 },
98 { "UserJob2", JOB_USERJOB2 },
99 { "UserJob3", JOB_USERJOB3 },
100 { "UserJob4", JOB_USERJOB4 }
104 int id {0};
106 QDateTime recstartts;
107 QDateTime schedruntime;
108 QString startts;
109 QDateTime inserttime;
110 int type {0};
111 int cmds {0};
112 int flags {0};
113 int status {0};
114 QDateTime statustime;
115 QString hostname;
116 QString args;
117 QString comment;
121 int id {0};
122 int type {0};
123 int flag {0};
124 QString desc;
125 QString command;
126 ProgramInfo *pginfo {nullptr};
127};
129class JobQueue;
130
131class MTV_PUBLIC JobQueue : public QObject, public QRunnable
133 Q_OBJECT
134
135 friend class QueueProcessorThread;
136 public:
137 explicit JobQueue(bool master);
138 ~JobQueue(void) override;
139 void customEvent(QEvent *e) override; // QObject
140
141 static bool QueueRecordingJobs(
142 const RecordingInfo &recinfo, int jobTypes = JOB_NONE);
143 static bool QueueJob(int jobType, uint chanid,
144 const QDateTime &recstartts, const QString& args = "",
145 const QString& comment = "", QString host = "",
146 int flags = 0, int status = JOB_QUEUED,
147 QDateTime schedruntime = QDateTime());
148
149 static bool QueueJobs(int jobTypes, uint chanid,
150 const QDateTime &recstartts, const QString& args = "",
151 const QString& comment = "", const QString& host = "");
152
153 static int GetJobID(int jobType, uint chanid,
154 const QDateTime &recstartts);
155 static bool GetJobInfoFromID(int jobID, int &jobType,
156 uint &chanid, QDateTime &recstartts);
157 static bool GetJobInfoFromID(int jobID, int &jobType,
158 uint &chanid, QString &recstartts);
159 static int GetJobTypeFromName(const QString &name);
160
161 static bool ChangeJobCmds(int jobID, int newCmds);
162 static bool ChangeJobCmds(int jobType, uint chanid,
163 const QDateTime &recstartts, int newCmds);
164 static bool ChangeJobFlags(int jobID, int newFlags);
165 static bool ChangeJobStatus(int jobID, int newStatus,
166 const QString& comment = "");
167 static bool ChangeJobHost(int jobID, const QString& newHostname);
168 static bool ChangeJobComment(int jobID,
169 const QString& comment = "");
170 static bool ChangeJobArgs(int jobID,
171 const QString& args = "");
172 static bool IsJobQueuedOrRunning(int jobType, uint chanid,
173 const QDateTime &recstartts);
174 int GetRunningJobID(uint chanid, const QDateTime &recstartts);
175 static bool IsJobRunning(int jobType, uint chanid,
176 const QDateTime &recstartts);
177 static bool IsJobRunning(int jobType, const ProgramInfo &pginfo);
178 static bool IsJobQueued(int jobType,
179 uint chanid, const QDateTime &recstartts);
180 static bool IsJobStatusQueued(int status);
181 static bool IsJobStatusRunning(int status);
182 static bool PauseJob(int jobID);
183 static bool ResumeJob(int jobID);
184 static bool RestartJob(int jobID);
185 static bool StopJob(int jobID);
186 static bool DeleteJob(int jobID);
187 static bool SafeDeleteJob(int jobID, int jobType, int chanid,
188 const QDateTime& recstartts);
189
190 static enum JobCmds GetJobCmd(int jobID);
191 static enum JobFlags GetJobFlags(int jobID);
192 static enum JobStatus GetJobStatus(int jobID);
193 static enum JobStatus GetJobStatus(int jobType, uint chanid,
194 const QDateTime &recstartts);
195 static QString GetJobArgs(int jobID);
196 static int UserJobTypeToIndex(int JobType);
198 static bool DeleteAllJobs(uint chanid, const QDateTime &recstartts);
200 static void ClearJobMask(int &mask) { mask = JOB_NONE; }
201 static bool JobIsInMask(int job, int mask) { return (bool)(job & mask); }
202 static bool JobIsNotInMask(int job, int mask)
203 { return ! JobIsInMask(job, mask); }
204 static void AddJobsToMask(int jobs, int &mask) { mask |= jobs; }
205 static void RemoveJobsFromMask(int jobs, int &mask) { mask &= ~jobs; }
206
207 static QString JobText(int jobType);
208 static QString StatusText(int status);
209
210 static bool HasRunningOrPendingJobs(std::chrono::minutes startingWithinMins = 0min);
211
212 static int GetJobsInQueue(QMap<int, JobQueueEntry> &jobs,
213 int findJobs = JOB_LIST_NOT_DONE);
214
215 static void RecoverQueue(bool justOld = false);
216 static void RecoverOldJobsInQueue()
217 { RecoverQueue(true); }
218 static void CleanupOldJobsInQueue();
219
220 static bool InJobRunWindow(QDateTime jobstarttsRaw);
221
222 private:
224 {
225 JobQueue *jq;
226 int jobID;
227 };
228
229 void run(void) override; // QRunnable
230 void ProcessQueue(void);
231
232 void ProcessJob(const JobQueueEntry& job);
233
234 bool AllowedToRun(const JobQueueEntry& job);
235
236 static bool InJobRunWindow(std::chrono::minutes orStartsWithinMins = 0min);
237
238 void StartChildJob(void *(*ChildThreadRoutine)(void *), int jobID);
239
240 static QString GetJobDescription(int jobType);
241 static QString GetJobCommand(int id, int jobType, ProgramInfo *tmpInfo);
242 void RemoveRunningJob(int id);
243
244 static QString PrettyPrint(off_t bytes);
245
246 static void *TranscodeThread(void *param);
247 void DoTranscodeThread(int jobID);
248
249 static void *MetadataLookupThread(void *param);
250 void DoMetadataLookupThread(int jobID);
251
252 static void *FlagCommercialsThread(void *param);
253 void DoFlagCommercialsThread(int jobID);
254
255 static void *UserJobThread(void *param);
256 void DoUserJobThread(int jobID);
258 QString m_hostname;
259
260 int m_jobsRunning {0};
261 int m_jobQueueCPU {0};
262
263 ProgramInfo *m_pginfo {nullptr};
264
265// QMutex m_controlFlagsLock;
266// QMap<QString, int *> m_jobControlFlags;
267
268 QRecursiveMutex *m_runningJobsLock {nullptr};
269 QMap<int, RunningJobInfo> m_runningJobs;
271 bool m_isMaster;
273 MThread *m_queueThread {nullptr};
274 QWaitCondition m_queueThreadCond;
275 QMutex m_queueThreadCondLock;
276 bool m_processQueue {false};
277};
278
279#endif
280
281/* vim: set expandtab tabstop=4 shiftwidth=4: */
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
Holds information on recordings and videos.
Definition: programinfo.h:68
Holds information on a TV Program one might wish to record.
Definition: recordinginfo.h:36
unsigned int uint
Definition: freesurround.h:24
unsigned short uint16_t
Definition: iso6937tables.h:3
JobLists
Definition: jobqueue.h:66
@ JOB_LIST_DONE
Definition: jobqueue.h:68
@ JOB_LIST_ALL
Definition: jobqueue.h:67
@ JOB_LIST_RECENT
Definition: jobqueue.h:71
@ JOB_LIST_NOT_DONE
Definition: jobqueue.h:69
@ JOB_LIST_ERROR
Definition: jobqueue.h:70
#define JOBSTATUS_MAP(F)
Definition: jobqueue.h:25
JobTypes
Definition: jobqueue.h:74
@ JOB_USERJOB3
Definition: jobqueue.h:86
@ JOB_METADATA
Definition: jobqueue.h:80
@ JOB_USERJOB1
Definition: jobqueue.h:84
@ JOB_USERJOB
Definition: jobqueue.h:83
@ JOB_USERJOB2
Definition: jobqueue.h:85
@ JOB_PREVIEW
Definition: jobqueue.h:81
@ JOB_NONE
Definition: jobqueue.h:75
@ JOB_SYSTEMJOB
Definition: jobqueue.h:77
@ JOB_COMMFLAG
Definition: jobqueue.h:79
@ JOB_USERJOB4
Definition: jobqueue.h:87
@ JOB_TRANSCODE
Definition: jobqueue.h:78
static QMap< QString, int > JobNameToType
Definition: jobqueue.h:90
JobStatus
Definition: jobqueue.h:44
JobCmds
Definition: jobqueue.h:50
@ JOB_STOP
Definition: jobqueue.h:54
@ JOB_RESTART
Definition: jobqueue.h:55
@ JOB_RESUME
Definition: jobqueue.h:53
@ JOB_RUN
Definition: jobqueue.h:51
@ JOB_PAUSE
Definition: jobqueue.h:52
#define JOBSTATUS_ENUM(A, B, C)
Definition: jobqueue.h:43
JobFlags
Definition: jobqueue.h:58
@ JOB_REBUILD
Definition: jobqueue.h:63
@ JOB_USE_CUTLIST
Definition: jobqueue.h:60
@ JOB_EXTERNAL
Definition: jobqueue.h:62
@ JOB_NO_FLAGS
Definition: jobqueue.h:59
@ JOB_LIVE_REC
Definition: jobqueue.h:61
static int QueueJob(const MythUtilCommandLineParser &cmdline)
Definition: jobutils.cpp:12
int jobID
#define off_t
#define MTV_PUBLIC
Definition: mythtvexp.h:15
void run(const QString &name, Class *object, void(Class::*fn)())
Definition: mconcurrent.h:137
QDateTime schedruntime
Definition: jobqueue.h:104
QString hostname
Definition: jobqueue.h:112
QDateTime statustime
Definition: jobqueue.h:111
QString comment
Definition: jobqueue.h:114
QDateTime recstartts
Definition: jobqueue.h:103
QString args
Definition: jobqueue.h:113
QString startts
Definition: jobqueue.h:105
QDateTime inserttime
Definition: jobqueue.h:106
ProgramInfo * pginfo
Definition: jobqueue.h:123
QString command
Definition: jobqueue.h:122
QString desc
Definition: jobqueue.h:121