Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial rewrite and migration of housekeeper to libraries.
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
Showing
8 changed files
with
1,453 additions
and
11 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.