00001 #ifndef JOBQUEUE_H_
00002 #define JOBQUEUE_H_
00003
00004 #include <qobject.h>
00005 #include <qmap.h>
00006 #include <qmutex.h>
00007 #include <qobject.h>
00008 #include <qsqldatabase.h>
00009 #include <qwaitcondition.h>
00010 #include <pthread.h>
00011
00012
00013 #include "programinfo.h"
00014
00015 using namespace std;
00016
00017
00018 #define JOBSTATUS_MAP(F) \
00019 F(JOB_UNKNOWN, 0x0000, tr("Unknown")) \
00020 F(JOB_QUEUED, 0x0001, tr("Queued")) \
00021 F(JOB_PENDING, 0x0002, tr("Pending")) \
00022 F(JOB_STARTING, 0x0003, tr("Starting")) \
00023 F(JOB_RUNNING, 0x0004, tr("Running")) \
00024 F(JOB_STOPPING, 0x0005, tr("Stopping")) \
00025 F(JOB_PAUSED, 0x0006, tr("Paused")) \
00026 F(JOB_RETRY, 0x0007, tr("Retrying")) \
00027 F(JOB_ERRORING, 0x0008, tr("Erroring")) \
00028 F(JOB_ABORTING, 0x0009, tr("Aborting")) \
00029
00030
00031
00032 \
00033 F(JOB_DONE, 0x0100, tr("Done (Invalid status!)")) \
00034 F(JOB_FINISHED, 0x0110, tr("Finished")) \
00035 F(JOB_ABORTED, 0x0120, tr("Aborted")) \
00036 F(JOB_ERRORED, 0x0130, tr("Errored")) \
00037 F(JOB_CANCELLED, 0x0140, tr("Cancelled")) \
00038
00039 enum JobStatus {
00040 #define JOBSTATUS_ENUM(A,B,C) A = B ,
00041 JOBSTATUS_MAP(JOBSTATUS_ENUM)
00042 };
00043
00044 enum JobCmds {
00045 JOB_RUN = 0x0000,
00046 JOB_PAUSE = 0x0001,
00047 JOB_RESUME = 0x0002,
00048 JOB_STOP = 0x0004,
00049 JOB_RESTART = 0x0008
00050 };
00051
00052 enum JobFlags {
00053 JOB_NO_FLAGS = 0x0000,
00054 JOB_USE_CUTLIST = 0x0001,
00055 JOB_LIVE_REC = 0x0002,
00056 JOB_EXTERNAL = 0x0004
00057 };
00058
00059 enum JobLists {
00060 JOB_LIST_ALL = 0x0001,
00061 JOB_LIST_DONE = 0x0002,
00062 JOB_LIST_NOT_DONE = 0x0004,
00063 JOB_LIST_ERROR = 0x0008,
00064 JOB_LIST_RECENT = 0x0010
00065 };
00066
00067 enum JobTypes {
00068 JOB_NONE = 0x0000,
00069
00070 JOB_SYSTEMJOB = 0x00ff,
00071 JOB_TRANSCODE = 0x0001,
00072 JOB_COMMFLAG = 0x0002,
00073
00074 JOB_USERJOB = 0xff00,
00075 JOB_USERJOB1 = 0x0100,
00076 JOB_USERJOB2 = 0x0200,
00077 JOB_USERJOB3 = 0x0400,
00078 JOB_USERJOB4 = 0x0800
00079 };
00080
00081 typedef struct jobqueueentry {
00082 int id;
00083 QString chanid;
00084 QDateTime starttime;
00085 QDateTime schedruntime;
00086 QString startts;
00087 QDateTime inserttime;
00088 int type;
00089 int cmds;
00090 int flags;
00091 int status;
00092 QDateTime statustime;
00093 QString hostname;
00094 QString args;
00095 QString comment;
00096 } JobQueueEntry;
00097
00098 class MPUBLIC JobQueue : public QObject
00099 {
00100 Q_OBJECT
00101 public:
00102 JobQueue(bool master);
00103 ~JobQueue(void);
00104 void customEvent(QCustomEvent *e);
00105
00106 static bool QueueRecordingJobs(ProgramInfo *pinfo, int jobTypes = JOB_NONE);
00107 static bool QueueJob(int jobType, QString chanid,
00108 QDateTime starttime, QString args = "",
00109 QString comment = "", QString host = "",
00110 int flags = 0, int status = JOB_QUEUED,
00111 QDateTime schedruntime = QDateTime());
00112
00113 static bool QueueJobs(int jobTypes, QString chanid,
00114 QDateTime starttime, QString args = "",
00115 QString comment = "", QString host = "");
00116
00117 static int GetJobID(int jobType, QString chanid,
00118 QDateTime starttime);
00119 static bool GetJobInfoFromID(int jobID, int &jobType,
00120 QString &chanid, QDateTime &starttime);
00121 static bool GetJobInfoFromID(int jobID, int &jobType,
00122 QString &chanid, QString &starttime);
00123
00124 static bool ChangeJobCmds(int jobID, int newCmds);
00125 static bool ChangeJobCmds(int jobType, QString chanid,
00126 QDateTime starttime, int newCmds);
00127 static bool ChangeJobFlags(int jobID, int newFlags);
00128 static bool ChangeJobStatus(int jobID, int newStatus,
00129 QString comment = "");
00130 static bool ChangeJobHost(int jobID, QString newHostname);
00131 static bool ChangeJobComment(int jobID,
00132 QString comment = "");
00133 static bool ChangeJobArgs(int jobID,
00134 QString args = "");
00135 static bool IsJobQueuedOrRunning(int jobType, QString chanid,
00136 QDateTime starttime);
00137 static bool IsJobRunning(int jobType, QString chanid,
00138 QDateTime starttime);
00139 static bool IsJobRunning(int jobType, ProgramInfo *pginfo);
00140 static bool IsJobQueued(int jobType, QString chanid, QDateTime starttime);
00141 static bool PauseJob(int jobID);
00142 static bool ResumeJob(int jobID);
00143 static bool RestartJob(int jobID);
00144 static bool StopJob(int jobID);
00145 static bool DeleteJob(int jobID);
00146
00147 static enum JobCmds GetJobCmd(int jobID);
00148 static enum JobFlags GetJobFlags(int jobID);
00149 static enum JobStatus GetJobStatus(int jobID);
00150 static enum JobStatus GetJobStatus(int jobType, QString chanid,
00151 QDateTime starttime);
00152 static QString GetJobArgs(int jobID);
00153 static int UserJobTypeToIndex(int JobType);
00154
00155 static bool DeleteAllJobs(QString chanid, QDateTime starttime);
00156
00157 static void ClearJobMask(int &mask) { mask = JOB_NONE; }
00158 static bool JobIsInMask(int job, int mask) { return (bool)(job & mask); }
00159 static bool JobIsNotInMask(int job, int mask)
00160 { return ! JobIsInMask(job, mask); }
00161 static void AddJobsToMask(int jobs, int &mask) { mask |= jobs; }
00162 static void RemoveJobsFromMask(int jobs, int &mask) { mask &= ~jobs; }
00163
00164 static QString JobText(int jobType);
00165 static QString StatusText(int status);
00166
00167 static bool HasRunningOrPendingJobs(int startingWithinMins = 0);
00168
00169 static int GetJobsInQueue(QMap<int, JobQueueEntry> &jobs,
00170 int findJobs = JOB_LIST_NOT_DONE);
00171
00172 static void RecoverQueue(bool justOld = false);
00173 static void RecoverOldJobsInQueue()
00174 { RecoverQueue(true); }
00175 static void CleanupOldJobsInQueue();
00176
00177 private:
00178 static void *QueueProcesserThread(void *param);
00179 void RunQueueProcesser(void);
00180 void ProcessQueue(void);
00181
00182 void ProcessJob(int id, int jobType, QString chanid, QDateTime starttime);
00183
00184 bool AllowedToRun(JobQueueEntry job);
00185
00186 static bool InJobRunWindow(int orStartingWithinMins = 0);
00187
00188 void StartChildJob(void *(*start_routine)(void *), ProgramInfo *tmpInfo);
00189
00190 static QString GetJobQueueKey(QString chanid, QString startts);
00191 static QString GetJobQueueKey(QString chanid, QDateTime starttime);
00192 static QString GetJobQueueKey(ProgramInfo *pginfo);
00193
00194 QString GetJobDescription(int jobType);
00195 QString GetJobCommand(int id, int jobType, ProgramInfo *tmpInfo);
00196
00197 static void *TranscodeThread(void *param);
00198 static QString PrettyPrint(off_t bytes);
00199 void DoTranscodeThread(void);
00200
00201 static void *FlagCommercialsThread(void *param);
00202 void DoFlagCommercialsThread(void);
00203
00204 static void *UserJobThread(void *param);
00205 void DoUserJobThread(void);
00206
00207 QString m_hostname;
00208
00209 int jobsRunning;
00210 int jobQueueCPU;
00211
00212 ProgramInfo *m_pginfo;
00213
00214 QMutex controlFlagsLock;
00215 QMap<QString, int *> jobControlFlags;
00216
00217 QMap<QString, int> runningJobIDs;
00218 QMap<QString, int> runningJobTypes;
00219 QMap<QString, QString> runningJobDescs;
00220 QMap<QString, QString> runningJobCommands;
00221
00222 bool childThreadStarted;
00223 bool isMaster;
00224
00225 pthread_t queueThread;
00226 QWaitCondition queueThreadCond;
00227 QMutex queueThreadCondLock;
00228 };
00229
00230 #endif
00231
00232