Ticket #3403: shutdown-jobs.diff

File shutdown-jobs.diff, 10.6 KB (added by matt.doran@…, 18 years ago)
  • libs/libmythtv/jobqueue.cpp

     
    168168
    169169    QMap<QString, int> jobStatus;
    170170    int maxJobs;
    171     QString queueStartTimeStr;
    172     QString queueEndTimeStr;
    173     int queueStartTime;
    174     int queueEndTime;
    175     QTime curQTime;
    176     int curTime;
    177171    QString message;
    178     QString tmpStr;
    179172    QMap<int, JobQueueEntry> jobs;
    180173    bool atMax = false;
    181174    bool inTimeWindow = true;
     
    187180
    188181        startedJobAlready = false;
    189182        sleepTime = gContext->GetNumSetting("JobQueueCheckFrequency", 30);
    190         queueStartTimeStr =
    191             gContext->GetSetting("JobQueueWindowStart", "00:00");
    192         queueEndTimeStr =
    193             gContext->GetSetting("JobQueueWindowEnd", "23:59");
    194 
    195183        maxJobs = gContext->GetNumSetting("JobQueueMaxSimultaneousJobs", 3);
    196184        VERBOSE(VB_JOBQUEUE, LOC +
    197                 QString("Currently set at %1 job(s) max and to run new jobs "
    198                         "from %2 to %3").arg(maxJobs).arg(queueStartTimeStr)
    199                                         .arg(queueEndTimeStr));
     185                QString("Currently set to run up to %1 job(s) max.")
     186                        .arg(maxJobs));
    200187
    201188        jobStatus.clear();
    202189
     
    204191       
    205192        if (jobs.size())
    206193        {
    207             tmpStr = queueStartTimeStr;
    208             queueStartTime = tmpStr.replace(QRegExp(":"), "").toInt();
    209             tmpStr = queueEndTimeStr;
    210             queueEndTime = tmpStr.replace(QRegExp(":"), "").toInt();
    211             curQTime = QTime::currentTime();
    212             curTime = curQTime.hour() * 100 + curQTime.minute();
    213             inTimeWindow = false;
    214 
    215             if ((queueStartTime <= curTime) && (curTime < queueEndTime))
    216             {
    217                 inTimeWindow = true;
    218             }
    219             else if ((queueStartTime > queueEndTime) &&
    220                      ((curTime < queueEndTime) || (queueStartTime <= curTime)))
    221             {
    222                 inTimeWindow = true;
    223             }
    224 
     194            inTimeWindow = InJobRunWindow();
    225195            jobsRunning = 0;
    226196            for (unsigned int x = 0; x < jobs.size(); x++)
    227197            {
     
    241211            {
    242212                message += QString("  Jobs in Queue, but we are outside of the "
    243213                                   "Job Queue time window, no new jobs can be "
    244                                    "started, next window starts at %1.")
    245                                    .arg(queueStartTimeStr);
     214                                   "started.");
    246215                VERBOSE(VB_JOBQUEUE, LOC + message);
    247216            }
    248217            else if (jobsRunning >= maxJobs)
     
    11211090    return JobQueue::GetJobQueueKey(pginfo->chanid, pginfo->recstartts);
    11221091}
    11231092
     1093bool JobQueue::InJobRunWindow(int orStartsWithinMins)
     1094{
     1095    QString queueStartTimeStr;
     1096    QString queueEndTimeStr;
     1097    QTime queueStartTime;
     1098    QTime queueEndTime;
     1099    QTime curTime = QTime::currentTime();
     1100    bool inTimeWindow = false;
     1101    orStartsWithinMins = orStartsWithinMins < 0 ? 0 : orStartsWithinMins;
     1102
     1103    queueStartTimeStr = gContext->GetSetting("JobQueueWindowStart", "00:00");
     1104    queueEndTimeStr = gContext->GetSetting("JobQueueWindowEnd", "23:59");
     1105
     1106    VERBOSE(VB_JOBQUEUE, LOC +
     1107            QString("Currently set to run new jobs from %1 to %2")
     1108                    .arg(queueStartTimeStr).arg(queueEndTimeStr));
     1109
     1110    queueStartTime = QTime::fromString(queueStartTimeStr);
     1111    if (!queueStartTime.isValid())
     1112    {
     1113        VERBOSE(VB_IMPORTANT, "Invalid JobQueueWindowStart time, using 00:00");
     1114        queueStartTime = QTime::QTime(0, 0);
     1115    }
     1116
     1117    queueEndTime = QTime::fromString(queueEndTimeStr);
     1118    if (!queueEndTime.isValid())
     1119    {
     1120        VERBOSE(VB_IMPORTANT, "Invalid JobQueueWindowEnd time, using 23:59");
     1121        queueEndTime = QTime::QTime(23, 59);
     1122    }
     1123   
     1124    if ((queueStartTime <= curTime) && (curTime < queueEndTime))
     1125    {
     1126        inTimeWindow = true;
     1127    }
     1128    else if ((queueStartTime > queueEndTime) &&
     1129             ((curTime < queueEndTime) || (queueStartTime <= curTime)))
     1130    {
     1131        inTimeWindow = true;
     1132    }
     1133    else if (orStartsWithinMins > 0)
     1134    {
     1135        // Check if the window starts soon
     1136        if (curTime <= queueStartTime)
     1137        {
     1138            // Start time hasn't passed yet today
     1139            if (queueStartTime.secsTo(curTime) <= (orStartsWithinMins * 60))
     1140            {
     1141                VERBOSE(VB_JOBQUEUE, LOC +
     1142                    QString("Job run window will start within %1 minutes")
     1143                            .arg(orStartsWithinMins));
     1144                inTimeWindow = true;
     1145            }
     1146        }
     1147        else
     1148        {
     1149            // We passed the start time for today, try tomorrow
     1150            QDateTime curDateTime = QDateTime::currentDateTime();
     1151            QDateTime startDateTime = QDateTime(QDate::currentDate(), queueStartTime).addDays(1);
     1152
     1153            if (curDateTime.secsTo(startDateTime) <= (orStartsWithinMins * 60))
     1154            {
     1155                VERBOSE(VB_JOBQUEUE, LOC +
     1156                    QString("Job run window will start within %1 minutes (tomorrow)")
     1157                            .arg(orStartsWithinMins));
     1158                inTimeWindow = true;
     1159            }
     1160        }
     1161    }
     1162
     1163    return inTimeWindow;
     1164}
     1165
     1166bool JobQueue::HasRunningOrPendingJobs(int startingWithinMins)
     1167{
     1168    /* startingWithinMins <= 0 - look for any pending jobs
     1169           > 0 -  only consider pending starting within this time */
     1170    QMap<int, JobQueueEntry> jobs;
     1171    QMap<int, JobQueueEntry>::Iterator it;
     1172    QDateTime maxSchedRunTime = QDateTime::currentDateTime();
     1173    int tmpStatus = 0;
     1174    bool checkForQueuedJobs = (startingWithinMins <= 0
     1175                                || InJobRunWindow(startingWithinMins));
     1176
     1177    if (checkForQueuedJobs && startingWithinMins > 0) {
     1178        maxSchedRunTime = maxSchedRunTime.addSecs(startingWithinMins * 60);
     1179        VERBOSE(VB_JOBQUEUE, LOC +
     1180            QString("HasRunningOrPendingJobs: checking for jobs "
     1181                    "starting before: %1").arg(maxSchedRunTime.toString()));
     1182    }
     1183
     1184    JobQueue::GetJobsInQueue(jobs, JOB_LIST_NOT_DONE);
     1185
     1186    if (jobs.size()) {
     1187        for (it = jobs.begin(); it != jobs.end(); ++it)
     1188        {
     1189            tmpStatus = it.data().status;
     1190            if (tmpStatus == JOB_RUNNING) {
     1191                VERBOSE(VB_JOBQUEUE, LOC +
     1192                        QString("HasRunningOrPendingJobs: found running job"));
     1193                return true;
     1194            }
     1195           
     1196            if (checkForQueuedJobs) {
     1197                if ((tmpStatus != JOB_UNKNOWN) && (!(tmpStatus & JOB_DONE))) {
     1198                    if (startingWithinMins <= 0) {
     1199                        VERBOSE(VB_JOBQUEUE, LOC +
     1200                            QString("HasRunningOrPendingJobs: "
     1201                                    "found pending job"));
     1202                        return true;
     1203                    }
     1204                    else if (it.data().schedruntime <= maxSchedRunTime) {
     1205                        VERBOSE(VB_JOBQUEUE, LOC +
     1206                            QString("HasRunningOrPendingJobs: found pending "
     1207                                    "job scheduled to start at: %1")
     1208                                    .arg(it.data().schedruntime.toString()));
     1209                        return true;
     1210                    }
     1211                }
     1212            }
     1213        }
     1214    }
     1215    return false;
     1216}
     1217
     1218
    11241219int JobQueue::GetJobsInQueue(QMap<int, JobQueueEntry> &jobs, int findJobs)
    11251220{
    11261221    JobQueueEntry thisJob;
  • libs/libmythtv/jobqueue.h

     
    161161    static QString JobText(int jobType);
    162162    static QString StatusText(int status);
    163163
     164    static bool HasRunningOrPendingJobs(int startingWithinMins = 0);
     165
    164166    static int GetJobsInQueue(QMap<int, JobQueueEntry> &jobs,
    165167                              int findJobs = JOB_LIST_NOT_DONE);
    166168
     
    178180
    179181    bool AllowedToRun(JobQueueEntry job);
    180182
     183    static bool InJobRunWindow(int orStartingWithinMins = 0);
     184
    181185    void StartChildJob(void *(*start_routine)(void *), ProgramInfo *tmpInfo);
    182186
    183187    static QString GetJobQueueKey(QString chanid, QString startts);
  • programs/mythwelcome/welcomedialog.cpp

     
    633633        m_statusList.append(tr("MythTV is busy grabbing EPG data."));
    634634    if (statusCode & 16)
    635635        m_statusList.append(tr("MythTV is locked by a user."));
     636    if (statusCode & 32)
     637        m_statusList.append(tr("MythTV is has running or pending jobs."));
    636638    if (statusCode & 64)
    637639        m_statusList.append(tr("MythTV is in a daily wakeup/shutdown period."));
    638640    if (statusCode & 128)
  • programs/mythshutdown/main.cpp

     
    1212#include <mythcontext.h>
    1313#include <mythdbcon.h>
    1414#include "libmythtv/programinfo.h"
     15#include "libmythtv/jobqueue.h"
    1516#include "tv.h"
    1617
    1718void setGlobalSetting(const QString &key, const QString &value)
     
    201202        res += 16;
    202203    }
    203204
     205    if (JobQueue::HasRunningOrPendingJobs(15))
     206    {
     207        VERBOSE(VB_IMPORTANT, "Has queued or pending jobs");
     208        res += 32;
     209    }
     210
    204211    QDateTime dtPeriod1Start = getDailyWakeupTime("DailyWakeupStartPeriod1");
    205212    QDateTime dtPeriod1End = getDailyWakeupTime("DailyWakeupEndPeriod1");
    206213    QDateTime dtPeriod2Start = getDailyWakeupTime("DailyWakeupStartPeriod2");
     
    715722  cout << "                          4 - Grabbing EPG data\n";
    716723  cout << "                          8 - Recording - only valid if flag is 1\n";
    717724  cout << "                         16 - Locked\n";
    718   cout << "                         32 - Not used\n";
     725  cout << "                         32 - Jobs running or pending\n";
    719726  cout << "                         64 - In a daily wakeup/shutdown period\n";
    720727  cout << "                        128 - Less than 15 minutes to next wakeup period\n";
    721728  cout << "-v/--verbose debug-level (Use '-v help' for level info\n";