Skip to content

Commit

Permalink
Initial rewrite and migration of housekeeper to libraries.
Browse files Browse the repository at this point in the history
This rewrites the existing housekeeper found in mythbackend, and places
it in libmythbase to make it available for use by other programs. The
structure is modular, with tasks subclassing one of the base task
classes, and then being registered against the housekeeper instance. The
housekeeper itself is threadless, looping through its list of tasks on a
one minute timer, and queuing up what tasks will need to be run.

The queue is handled by one or more threads independent of the
housekeeper. There will always be one primary thread waiting to be woken
up by the housekeeper, that will work through the queue of tasks
available. If a task is taking longer than one minute, such that the
housekeeper has made another pass and found items waiting in the queue,
an additional thread will be started to begin working through the queue,
while the first will be set to terminate after finishing its current
task. This is so long running tasks such as guide data downloading does
not disrupt any other tasks in the queue.

Currently, the existing housekeeping tasks in the backend housekeeper
have been re-implemented using this mechanism. Additional immediately
planned tasks include a monthly Smolt run to replace the existing
startup check in mythfrontend, and a rescan of available addresses every
few minutes to allow ServerPool to adjust to dynamic network interfaces,
such as frontends on a wireless network, or distributions that have not
finished initing networks prior to the backend starting.

This compiles, but it has not yet been tested in any form. Further, this
is not yet ready for use, as it relies on a not-yet-written schema
update to create a new table and adapt data from the settings table into
it.
  • Loading branch information
wagnerrp committed May 30, 2013
1 parent fa2210f commit 6122951
Show file tree
Hide file tree
Showing 8 changed files with 1,453 additions and 11 deletions.
629 changes: 629 additions & 0 deletions mythtv/libs/libmythbase/housekeeper.cpp

Large diffs are not rendered by default.

165 changes: 165 additions & 0 deletions mythtv/libs/libmythbase/housekeeper.h
@@ -0,0 +1,165 @@
#ifndef HOUSEKEEPER_H_
#define HOUSEKEEPER_H_

#include <QWaitCondition>
#include <QDateTime>
#include <QString>
#include <QEvent>
#include <QTimer>
#include <QMutex>
#include <QQueue>
#include <QList>
#include <QMap>

#include "mthread.h"
#include "mythdate.h"
#include "mythevent.h"
#include "mythbaseexp.h"
#include "referencecounter.h"

class Scheduler;
class HouseKeeper;

enum HouseKeeperScope {
kHKGlobal = 0,
kHKLocal,
kHKInst
};

enum HouseKeeperStartup {
kHKNormal = 0,
kHKRunOnStartup,
kHKRunImmediateOnStartup
};

class MBASE_PUBLIC HouseKeeperTask : public ReferenceCounter
{
public:
HouseKeeperTask(const QString &dbTag, HouseKeeperScope scope=kHKGlobal,
HouseKeeperStartup startup=kHKNormal);
~HouseKeeperTask() {}

bool CheckRun(QDateTime now);
bool Run(void);
bool ConfirmRun(void) { return m_confirm; }

bool CheckImmediate(void);
bool CheckStartup(void);

QString GetTag(void) { return m_dbTag; }
QDateTime GetLastRun(void) { return m_lastRun; }
QDateTime QueryLastRun(void);
QDateTime UpdateLastRun(void)
{ return UpdateLastRun(MythDate::current()); }
virtual QDateTime UpdateLastRun(QDateTime last);
virtual void SetLastRun(QDateTime last) { m_lastRun = last;
m_confirm = false; }

virtual bool DoCheckRun(QDateTime now) { return false; }
virtual bool DoRun(void) { return false; }

virtual void Terminate(void) {}

private:

QString m_dbTag;
bool m_confirm;
HouseKeeperScope m_scope;
HouseKeeperStartup m_startup;

QDateTime m_lastRun;
};

class MBASE_PUBLIC PeriodicHouseKeeperTask : public HouseKeeperTask
{
public:
PeriodicHouseKeeperTask(const QString &dbTag, int period, float min=0.5,
float max=1.1, HouseKeeperScope scope=kHKGlobal,
HouseKeeperStartup startup=kHKNormal);
virtual bool DoCheckRun(QDateTime now);
virtual bool InWindow(QDateTime now);
virtual QDateTime UpdateLastRun(QDateTime last);
virtual void SetLastRun(QDateTime last);
virtual void SetWindow(float min, float max);

protected:
virtual void CalculateWindow(void);

int m_period;
QPair<float,float> m_windowPercent;
QPair<int,int> m_windowElapsed;
float m_currentProb;
};

class MBASE_PUBLIC DailyHouseKeeperTask : public PeriodicHouseKeeperTask
{
public:
DailyHouseKeeperTask(const QString &dbTag,
HouseKeeperScope scope=kHKGlobal,
HouseKeeperStartup startup=kHKNormal);
DailyHouseKeeperTask(const QString &dbTag, int minhour, int maxhour,
HouseKeeperScope scope=kHKGlobal,
HouseKeeperStartup startup=kHKNormal);
virtual void SetHourWindow(int min, int max);

protected:
virtual void CalculateWindow(void);

private:
QPair<int, int> m_windowHour;
};

class HouseKeepingThread : public MThread
{
public:
HouseKeepingThread(HouseKeeper *p) :
MThread("HouseKeeping"), m_idle(true), m_keepRunning(true),
m_parent(p) {}
~HouseKeepingThread() {}
virtual void run(void);
void Discard(void) { m_keepRunning = false; }
bool isIdle(void) { return m_idle; }
void Wake(void) { m_waitCondition.wakeAll(); }

void Terminate(void);

private:
bool m_idle;
bool m_keepRunning;
HouseKeeper *m_parent;
QMutex m_waitMutex;
QWaitCondition m_waitCondition;
};

class MBASE_PUBLIC HouseKeeper : public QObject
{
Q_OBJECT

public:
HouseKeeper(void);
~HouseKeeper();

void RegisterTask(HouseKeeperTask *task);
void Start(void);
void StartThread(void);
HouseKeeperTask* GetQueuedTask(void);

void customEvent(QEvent *e);

public slots:
void Run(void);

private:
QTimer *m_timer;

QQueue<HouseKeeperTask*> m_taskQueue;
QMutex m_queueLock;

QMap<QString, HouseKeeperTask*> m_taskMap;
QMutex m_mapLock;

QList<HouseKeepingThread*> m_threadList;
QMutex m_threadLock;
};

#endif
4 changes: 2 additions & 2 deletions mythtv/libs/libmythbase/libmythbase.pro
Expand Up @@ -26,7 +26,7 @@ HEADERS += mythdeque.h mythlogging.h
HEADERS += mythbaseutil.h referencecounter.h version.h mythcommandlineparser.h
HEADERS += mythscheduler.h filesysteminfo.h hardwareprofile.h serverpool.h
HEADERS += plist.h bswap.h signalhandling.h mythtimezone.h mythdate.h
HEADERS += mythplugin.h mythpluginapi.h
HEADERS += mythplugin.h mythpluginapi.h housekeeper.h
HEADERS += ffmpeg-mmx.h

SOURCES += mthread.cpp mthreadpool.cpp
Expand All @@ -43,7 +43,7 @@ SOURCES += logging.cpp loggingserver.cpp
SOURCES += referencecounter.cpp mythcommandlineparser.cpp
SOURCES += filesysteminfo.cpp hardwareprofile.cpp serverpool.cpp
SOURCES += plist.cpp signalhandling.cpp mythtimezone.cpp mythdate.cpp
SOURCES += mythplugin.cpp
SOURCES += mythplugin.cpp housekeeper.cpp

# This stuff is not Qt5 compatible..
contains(QT_VERSION, ^4\\.[0-9]\\..*) {
Expand Down

0 comments on commit 6122951

Please sign in to comment.