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"
16 #include "libmythbase/mythchrono.h"
17 
18 class MThread;
19 class ProgramInfo;
20 class 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) ,
47 enum JobStatus {
49 };
50 
51 // NOLINTEND(cppcoreguidelines-macro-usage)
52 
53 enum JobCmds {
54  JOB_RUN = 0x0000,
55  JOB_PAUSE = 0x0001,
56  JOB_RESUME = 0x0002,
57  JOB_STOP = 0x0004,
58  JOB_RESTART = 0x0008
59 };
60 
61 enum JobFlags {
62  JOB_NO_FLAGS = 0x0000,
63  JOB_USE_CUTLIST = 0x0001,
64  JOB_LIVE_REC = 0x0002,
65  JOB_EXTERNAL = 0x0004,
66  JOB_REBUILD = 0x0008
67 };
68 
69 enum JobLists {
70  JOB_LIST_ALL = 0x0001,
71  JOB_LIST_DONE = 0x0002,
72  JOB_LIST_NOT_DONE = 0x0004,
73  JOB_LIST_ERROR = 0x0008,
74  JOB_LIST_RECENT = 0x0010
75 };
76 
77 enum JobTypes {
78  JOB_NONE = 0x0000,
79 
80  JOB_SYSTEMJOB = 0x00ff,
81  JOB_TRANSCODE = 0x0001,
82  JOB_COMMFLAG = 0x0002,
83  JOB_METADATA = 0x0004,
84  JOB_PREVIEW = 0x0008,
85 
86  JOB_USERJOB = 0xff00,
87  JOB_USERJOB1 = 0x0100,
88  JOB_USERJOB2 = 0x0200,
89  JOB_USERJOB3 = 0x0400,
90  JOB_USERJOB4 = 0x0800
91 };
92 
93 static 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 }
101 };
102 
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;
118 };
119 
121  int id {0};
122  int type {0};
123  int flag {0};
124  QString desc;
125  QString command;
126  ProgramInfo *pginfo {nullptr};
127 };
128 
129 class JobQueue;
130 
131 class MTV_PUBLIC JobQueue : public QObject, public QRunnable
132 {
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);
197 
198  static bool DeleteAllJobs(uint chanid, const QDateTime &recstartts);
199 
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);
257 
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;
270 
271  bool m_isMaster;
272 
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: */
JOB_USERJOB3
@ JOB_USERJOB3
Definition: jobqueue.h:86
JOB_USERJOB4
@ JOB_USERJOB4
Definition: jobqueue.h:87
build_compdb.args
args
Definition: build_compdb.py:11
JobQueueEntry::comment
QString comment
Definition: jobqueue.h:114
JobQueueEntry::hostname
QString hostname
Definition: jobqueue.h:112
QueueJob
static int QueueJob(const MythUtilCommandLineParser &cmdline)
Definition: jobutils.cpp:12
JobQueueEntry::inserttime
QDateTime inserttime
Definition: jobqueue.h:106
JOB_REBUILD
@ JOB_REBUILD
Definition: jobqueue.h:63
JobNameToType
static QMap< QString, int > JobNameToType
Definition: jobqueue.h:90
JobQueueEntry::type
int type
Definition: jobqueue.h:107
JOB_SYSTEMJOB
@ JOB_SYSTEMJOB
Definition: jobqueue.h:77
JobQueueEntry
Definition: jobqueue.h:100
JobCmds
JobCmds
Definition: jobqueue.h:50
RunningJobInfo
Definition: jobqueue.h:117
JOB_RUN
@ JOB_RUN
Definition: jobqueue.h:51
RecordingInfo
Holds information on a TV Program one might wish to record.
Definition: recordinginfo.h:35
RunningJobInfo::desc
QString desc
Definition: jobqueue.h:121
mythtvexp.h
JOB_COMMFLAG
@ JOB_COMMFLAG
Definition: jobqueue.h:79
JOB_USE_CUTLIST
@ JOB_USE_CUTLIST
Definition: jobqueue.h:60
JOB_PAUSE
@ JOB_PAUSE
Definition: jobqueue.h:52
JOB_RESUME
@ JOB_RESUME
Definition: jobqueue.h:53
JOB_NONE
@ JOB_NONE
Definition: jobqueue.h:75
JOB_LIST_NOT_DONE
@ JOB_LIST_NOT_DONE
Definition: jobqueue.h:69
JOB_STOP
@ JOB_STOP
Definition: jobqueue.h:54
jobID
int jobID
Definition: mythcommflag.cpp:80
JOBSTATUS_ENUM
#define JOBSTATUS_ENUM(A, B, C)
Definition: jobqueue.h:43
JOB_USERJOB2
@ JOB_USERJOB2
Definition: jobqueue.h:85
JOB_LIST_ALL
@ JOB_LIST_ALL
Definition: jobqueue.h:67
JOB_PREVIEW
@ JOB_PREVIEW
Definition: jobqueue.h:81
JOB_LIVE_REC
@ JOB_LIVE_REC
Definition: jobqueue.h:61
JobQueueEntry::schedruntime
QDateTime schedruntime
Definition: jobqueue.h:104
RunningJobInfo::command
QString command
Definition: jobqueue.h:122
RunningJobInfo::pginfo
ProgramInfo * pginfo
Definition: jobqueue.h:123
JOBSTATUS_MAP
#define JOBSTATUS_MAP(F)
Definition: jobqueue.h:25
JOB_USERJOB1
@ JOB_USERJOB1
Definition: jobqueue.h:84
JobTypes
JobTypes
Definition: jobqueue.h:74
JobFlags
JobFlags
Definition: jobqueue.h:58
JOB_RESTART
@ JOB_RESTART
Definition: jobqueue.h:55
JobQueueEntry::startts
QString startts
Definition: jobqueue.h:105
JobQueueEntry::args
QString args
Definition: jobqueue.h:113
JobQueueEntry::statustime
QDateTime statustime
Definition: jobqueue.h:111
JobQueueEntry::status
int status
Definition: jobqueue.h:110
MConcurrent::run
void run(const QString &name, Class *object, void(Class::*fn)())
Definition: mconcurrent.h:137
JobStatus
JobStatus
Definition: jobqueue.h:44
JobQueueEntry::chanid
uint chanid
Definition: jobqueue.h:102
JobQueueEntry::flags
int flags
Definition: jobqueue.h:109
uint
unsigned int uint
Definition: compat.h:81
RunningJobInfo::flag
int flag
Definition: jobqueue.h:120
RunningJobInfo::type
int type
Definition: jobqueue.h:119
JobLists
JobLists
Definition: jobqueue.h:66
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:108
musicbrainzngs.compat.bytes
bytes
Definition: compat.py:49
JOB_EXTERNAL
@ JOB_EXTERNAL
Definition: jobqueue.h:62
JOB_METADATA
@ JOB_METADATA
Definition: jobqueue.h:80
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:68
JobQueue
Definition: jobqueue.h:128
JOB_NO_FLAGS
@ JOB_NO_FLAGS
Definition: jobqueue.h:59
JOB_LIST_RECENT
@ JOB_LIST_RECENT
Definition: jobqueue.h:71
JobQueueEntry::recstartts
QDateTime recstartts
Definition: jobqueue.h:103
JOB_LIST_ERROR
@ JOB_LIST_ERROR
Definition: jobqueue.h:70
JOB_TRANSCODE
@ JOB_TRANSCODE
Definition: jobqueue.h:78
JOB_USERJOB
@ JOB_USERJOB
Definition: jobqueue.h:83
JobQueue::JobThreadStruct
Definition: jobqueue.h:220