Skip to content

Commit

Permalink
Housekeeper: allows initial INSERTs to the housekeeping table
Browse files Browse the repository at this point in the history
Fresh DBs have no entries in housekeeping. INSERTs weren't
being made because the test that decided to do it always
failed and caused an UPDATE. That did nothing because a
matching tag wasn't found.

Now, always do an UPDATE and if the no rows were affected,
then do an INSERT.
  • Loading branch information
Bill Meek committed Jul 27, 2022
1 parent 50c58fb commit e0996cb
Showing 1 changed file with 50 additions and 47 deletions.
97 changes: 50 additions & 47 deletions mythtv/libs/libmythbase/housekeeper.cpp
Expand Up @@ -107,14 +107,15 @@ HouseKeeperTask::HouseKeeperTask(const QString &dbTag, HouseKeeperScope scope,

bool HouseKeeperTask::CheckRun(const QDateTime& now)
{
LOG(VB_GENERAL, LOG_DEBUG, QString("Checking to run %1").arg(GetTag()));
bool check = false;
if (!m_confirm && !m_running && (check = DoCheckRun(now)))
{
// if m_confirm is already set, the task is already in the queue
// and should not be queued a second time
m_confirm = true;
}
LOG(VB_GENERAL, LOG_DEBUG, QString("%1 Running? %2/In window? %3.")
.arg(GetTag()).arg(m_running ? "Yes" : "No").arg(check ? "Yes" : "No"));
return check;
}

Expand Down Expand Up @@ -227,52 +228,19 @@ QDateTime HouseKeeperTask::UpdateLastRun(const QDateTime& last, bool successful)
if (!query.isConnected())
return last;

if (m_lastRun == MythDate::fromSecsSinceEpoch(0))
{
// not previously set, perform insert

if (m_scope == kHKGlobal)
{
query.prepare("INSERT INTO housekeeping"
" (tag, lastrun, lastsuccess)"
" VALUES (:TAG, :TIME, :STIME)");
}
else
{
query.prepare("INSERT INTO housekeeping"
" (tag, hostname, lastrun, lastsuccess)"
" VALUES (:TAG, :HOST, :TIME, :STIME)");
}
}
else
{
// previously set, perform update

if (m_scope == kHKGlobal)
{
query.prepare("UPDATE housekeeping SET lastrun=:TIME,"
" lastsuccess=:STIME"
" WHERE tag = :TAG"
" AND hostname IS NULL");
}
else
{
query.prepare("UPDATE housekeeping SET lastrun=:TIME,"
" lastsuccess=:STIME"
" WHERE tag = :TAG"
" AND hostname = :HOST");
}
}

if (m_scope == kHKGlobal)
{
LOG(VB_GENERAL, LOG_DEBUG,
QString("Updating global run time for %1").arg(m_dbTag));
query.prepare("UPDATE `housekeeping` SET `lastrun`=:TIME,"
" `lastsuccess`=:STIME"
" WHERE `tag` = :TAG"
" AND `hostname` IS NULL");
}
else
{
LOG(VB_GENERAL, LOG_DEBUG,
QString("Updating local run time for %1").arg(m_dbTag));
query.prepare("UPDATE `housekeeping` SET `lastrun`=:TIME,"
" `lastsuccess`=:STIME"
" WHERE `tag` = :TAG"
" AND `hostname` = :HOST");
}

if (m_scope == kHKLocal)
Expand All @@ -282,7 +250,42 @@ QDateTime HouseKeeperTask::UpdateLastRun(const QDateTime& last, bool successful)
query.bindValue(":STIME", MythDate::as_utc(m_lastSuccess));

if (!query.exec())
MythDB::DBError("HouseKeeperTask::updateLastRun", query);
MythDB::DBError("HouseKeeperTask::updateLastRun, UPDATE", query);

if (VERBOSE_LEVEL_CHECK(VB_GENERAL, LOG_DEBUG) &&
query.numRowsAffected() > 0)
{
LOG(VB_GENERAL, LOG_DEBUG, QString("%1: UPDATEd %2 run time.")
.arg(m_dbTag).arg(m_scope == kHKGlobal ? "global" : "local"));
}

if (query.numRowsAffected() == 0)
{
if (m_scope == kHKGlobal)
{
query.prepare("INSERT INTO `housekeeping`"
" (`tag`, `lastrun`, `lastsuccess`)"
" VALUES (:TAG, :TIME, :STIME)");
}
else
{
query.prepare("INSERT INTO `housekeeping`"
" (`tag`, `hostname`, `lastrun`, `lastsuccess`)"
" VALUES (:TAG, :HOST, :TIME, :STIME)");
}

if (m_scope == kHKLocal)
query.bindValue(":HOST", gCoreContext->GetHostName());
query.bindValue(":TAG", m_dbTag);
query.bindValue(":TIME", MythDate::as_utc(m_lastRun));
query.bindValue(":STIME", MythDate::as_utc(m_lastSuccess));

if (!query.exec())
MythDB::DBError("HouseKeeperTask::updateLastRun INSERT", query);

LOG(VB_GENERAL, LOG_DEBUG, QString("%1: INSERTed %2 run time.")
.arg(m_dbTag).arg(m_scope == kHKGlobal ? "global" : "local"));
}
}

QString msg;
Expand Down Expand Up @@ -676,10 +679,10 @@ void HouseKeeper::Start(void)
return;

MSqlQuery query(MSqlQuery::InitCon());
query.prepare("SELECT tag,lastrun"
" FROM housekeeping"
" WHERE hostname = :HOST"
" OR hostname IS NULL");
query.prepare("SELECT `tag`,`lastrun`"
" FROM `housekeeping`"
" WHERE `hostname` = :HOST"
" OR `hostname` IS NULL");
query.bindValue(":HOST", gCoreContext->GetHostName());

if (!query.exec())
Expand Down

0 comments on commit e0996cb

Please sign in to comment.