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