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