-
Notifications
You must be signed in to change notification settings - Fork 341
/
housekeeper.h
181 lines (148 loc) · 6.15 KB
/
housekeeper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#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 <QPair>
#include "mthread.h"
#include "mythchrono.h"
#include "mythdate.h"
#include "mythevent.h"
#include "mythbaseexp.h"
#include "referencecounter.h"
class Scheduler;
class HouseKeeper;
enum HouseKeeperScope {
kHKGlobal = 0, ///< task should only run once per cluster
///< e.g. mythfilldatabase
kHKLocal, ///< task should only run once per machine
///< e.g. hardware profile update
kHKInst ///< task should run on every process
///< e.g. ServerPool update
};
enum HouseKeeperStartup {
kHKNormal = 0, ///< task is checked normally
kHKRunOnStartup, ///< task is queued when HouseKeeper is started
kHKRunImmediateOnStartup ///< task is run during HouseKeeper startup
};
class MBASE_PUBLIC HouseKeeperTask : public ReferenceCounter
{
public:
explicit HouseKeeperTask(const QString &dbTag, HouseKeeperScope scope=kHKGlobal,
HouseKeeperStartup startup=kHKNormal);
~HouseKeeperTask() override = default;
bool CheckRun(const QDateTime& now);
bool Run(void);
bool ConfirmRun(void) const { return m_confirm; }
bool IsRunning(void) const { return m_running; }
bool CheckImmediate(void);
bool CheckStartup(void);
QString GetTag(void) { return m_dbTag; }
QDateTime GetLastRun(void) { return m_lastRun; }
QDateTime GetLastSuccess(void) { return m_lastSuccess; }
HouseKeeperScope GetScope(void) { return m_scope; }
QDateTime QueryLastRun(void);
QDateTime QueryLastSuccess(void);
QDateTime UpdateLastRun(bool successful=true)
{ return UpdateLastRun(MythDate::current(), successful); }
virtual QDateTime UpdateLastRun(const QDateTime& last, bool successful=true);
virtual void SetLastRun(const QDateTime& last, bool successful=true);
virtual bool DoCheckRun(const QDateTime& /*now*/) { return false; }
virtual bool DoRun(void) { return false; }
virtual void Terminate(void) {}
private:
void QueryLast(void);
QString m_dbTag;
bool m_confirm {false};
HouseKeeperScope m_scope;
HouseKeeperStartup m_startup;
bool m_running {false};
QDateTime m_lastRun;
QDateTime m_lastSuccess;
QDateTime m_lastUpdate;
};
class MBASE_PUBLIC PeriodicHouseKeeperTask : public HouseKeeperTask
{
public:
PeriodicHouseKeeperTask(const QString &dbTag, std::chrono::seconds period, float min=0.5,
float max=1.1, std::chrono::seconds retry=0s, HouseKeeperScope scope=kHKGlobal,
HouseKeeperStartup startup=kHKNormal);
bool DoCheckRun(const QDateTime& now) override; // HouseKeeperTask
virtual bool InWindow(const QDateTime& now);
virtual bool PastWindow(const QDateTime& now);
QDateTime UpdateLastRun(const QDateTime& last, bool successful=true) override; // HouseKeeperTask
void SetLastRun(const QDateTime& last, bool successful=true) override; // HouseKeeperTask
virtual void SetWindow(float min, float max);
protected:
virtual void CalculateWindow(void);
std::chrono::seconds m_period;
std::chrono::seconds m_retry;
QPair<float,float> m_windowPercent;
QPair<std::chrono::seconds,std::chrono::seconds> m_windowElapsed;
double m_currentProb;
};
class MBASE_PUBLIC DailyHouseKeeperTask : public PeriodicHouseKeeperTask
{
public:
explicit DailyHouseKeeperTask(const QString &dbTag,
HouseKeeperScope scope=kHKGlobal,
HouseKeeperStartup startup=kHKNormal);
DailyHouseKeeperTask(const QString &dbTag,
std::chrono::hours minhour, std::chrono::hours maxhour,
HouseKeeperScope scope=kHKGlobal,
HouseKeeperStartup startup=kHKNormal);
virtual void SetHourWindow(std::chrono::hours min, std::chrono::hours max);
bool InWindow(const QDateTime& now) override; // PeriodicHouseKeeperTask
protected:
void CalculateWindow(void) override; // PeriodicHouseKeeperTask
private:
QPair<std::chrono::hours, std::chrono::hours> m_windowHour;
};
class HouseKeepingThread : public MThread
{
public:
explicit HouseKeepingThread(HouseKeeper *p) :
MThread("HouseKeeping"), m_idle(true), m_keepRunning(true),
m_parent(p) {}
~HouseKeepingThread() override = default;
void run(void) override; // MThread
void Discard(void) { m_keepRunning = false; }
bool isIdle(void) const { 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() override;
void RegisterTask(HouseKeeperTask *task);
void Start(void);
void StartThread(void);
HouseKeeperTask* GetQueuedTask(void);
void customEvent(QEvent *e) override; // QObject
public slots:
void Run(void);
private:
QTimer *m_timer { nullptr };
QQueue<HouseKeeperTask*> m_taskQueue;
QMutex m_queueLock;
QMap<QString, HouseKeeperTask*> m_taskMap;
QMutex m_mapLock;
QList<HouseKeepingThread*> m_threadList;
QMutex m_threadLock;
};
#endif