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