Ticket #3842: 3842-housekeeper-cleanup-v1.patch

File 3842-housekeeper-cleanup-v1.patch, 3.5 KB (added by danielk, 18 years ago)

This cleans up the housekeeper code which starts mythfilldatabase -- more bullet proof in case of db problems..

  • programs/mythbackend/housekeeper.cpp

     
    6262    QDateTime lastrun;
    6363    lastrun.setTime_t(0);
    6464
    65     if (minhour < 0)
    66         minhour = 0;
    67     if (maxhour > 23)
    68         maxhour = 23;
     65    minhour = (minhour < 0)  ? 0  : minhour;
     66    maxhour = (maxhour > 23) ? 23 : maxhour;
    6967
    70     MSqlQuery result(MSqlQuery::InitCon());
    71     if (result.isConnected())
     68    MSqlQuery query(MSqlQuery::InitCon());
     69    if (!query.isConnected())
     70        return runOK;
     71
     72    query.prepare("SELECT lastrun FROM housekeeping WHERE tag = :TAG ;");
     73    query.bindValue(":TAG", dbTag);
     74    bool ok = query.exec();
     75
     76    if (ok && query.next())
    7277    {
    73         result.prepare("SELECT lastrun FROM housekeeping WHERE tag = :TAG ;");
    74         result.bindValue(":TAG", dbTag);
     78        lastrun = query.value(0).toDateTime();
    7579
    76         if (result.exec() && result.isActive() && result.size() > 0)
     80        if ((lastrun.secsTo(now) > longEnough) &&
     81            (now.date().day() != lastrun.date().day()))
    7782        {
    78             result.next();
    79             lastrun = result.value(0).toDateTime();
     83            int hour = now.time().hour();
    8084
    81             if ((lastrun.secsTo(now) > longEnough) &&
    82                 (now.date().day() != lastrun.date().day()))
     85            if (((minhour > maxhour) &&
     86                 ((hour <= maxhour) || (hour >= minhour))) ||
     87                ((hour >= minhour) && (hour <= maxhour)))
    8388            {
    84                 int hour = now.time().hour();
    85 
    86                 if (((minhour > maxhour) &&
    87                      ((hour <= maxhour) || (hour >= minhour))) ||
    88                     ((hour >= minhour) && (hour <= maxhour)))
     89                int minute = now.time().minute();
     90                if ((hour == maxhour && minute > 30) ||
     91                    ((random()%(((maxhour-hour)*12+
     92                                 (60-minute)/5 - 6) + 1)) == 0))
    8993                {
    90                     int minute = now.time().minute();
    91                     if ((hour == maxhour && minute > 30) ||
    92                         ((random()%(((maxhour-hour)*12+(60-minute)/5 - 6) + 1)) == 0))
    9394                        runOK = true;
    9495                }
    9596            }
    9697        }
    97         else
    98         {
    99             result.prepare("INSERT INTO housekeeping(tag,lastrun) "
    100                            "values(:TAG ,now());");
    101             result.bindValue(":TAG", dbTag);
    102             result.exec();
     98    }
     99    else
     100    {
     101        if (!ok)
     102            MythContext::DBError("wantToRun failure", query);
    103103
    104             runOK = true;
    105         }
     104        query.prepare("INSERT INTO housekeeping(tag,lastrun) "
     105                       "VALUES(:TAG ,now());");
     106        query.bindValue(":TAG", dbTag);
     107        query.exec();
     108
     109        runOK = true;
    106110    }
    107111
    108112    return runOK;
     
    222226                        QDateTime lastRun = getLastRun("MythFillDB");
    223227                        QDateTime now = QDateTime::currentDateTime();
    224228
    225                         if ((nextRun < now) &&
    226                             (lastRun.secsTo(now) > (3 * 60 * 60)))
    227                             runMythFill = true;
     229                        runMythFill |= ((nextRun < now) &&
     230                                        (lastRun.secsTo(now) > (3 * 60 * 60)));
     231                           
    228232                    }
    229233                    else if (wantToRun("MythFillDB", period, minhr, maxhr))
    230234                    {