MythTV  master
housekeeper.h
Go to the documentation of this file.
1 #ifndef HOUSEKEEPER_H_
2 #define HOUSEKEEPER_H_
3 
4 #include <QWaitCondition>
5 #include <QDateTime>
6 #include <QString>
7 #include <QEvent>
8 #include <QTimer>
9 #include <QMutex>
10 #include <QQueue>
11 #include <QList>
12 #include <QMap>
13 #include <QPair>
14 
15 #include "mthread.h"
16 #include "mythdate.h"
17 #include "mythevent.h"
18 #include "mythbaseexp.h"
19 #include "referencecounter.h"
20 
21 class Scheduler;
22 class HouseKeeper;
23 
25  kHKGlobal = 0,
26  kHKLocal,
28  kHKInst
30 };
32 
34  kHKNormal = 0,
37 };
38 
40 {
41  public:
42  HouseKeeperTask(const QString &dbTag, HouseKeeperScope scope=kHKGlobal,
44  ~HouseKeeperTask() = default;
45 
46  bool CheckRun(QDateTime now);
47  bool Run(void);
48  bool ConfirmRun(void) { return m_confirm; }
49  bool IsRunning(void) { return m_running; }
50 
51  bool CheckImmediate(void);
52  bool CheckStartup(void);
53 
54  QString GetTag(void) { return m_dbTag; }
55  QDateTime GetLastRun(void) { return m_lastRun; }
56  QDateTime GetLastSuccess(void) { return m_lastSuccess; }
57  HouseKeeperScope GetScope(void) { return m_scope; }
58  QDateTime QueryLastRun(void);
59  QDateTime QueryLastSuccess(void);
60  QDateTime UpdateLastRun(bool successful=true)
61  { return UpdateLastRun(MythDate::current(), successful); }
62  virtual QDateTime UpdateLastRun(const QDateTime& last, bool successful=true);
63  virtual void SetLastRun(const QDateTime& last, bool successful=true);
64 
65  virtual bool DoCheckRun(QDateTime /*now*/) { return false; }
66  virtual bool DoRun(void) { return false; }
67 
68  virtual void Terminate(void) {}
69 
70  private:
71  void QueryLast(void);
72 
73  QString m_dbTag;
74  bool m_confirm {false};
77  bool m_running {false};
78 
79  QDateTime m_lastRun;
80  QDateTime m_lastSuccess;
81  QDateTime m_lastUpdate;
82 };
83 
85 {
86  public:
87  PeriodicHouseKeeperTask(const QString &dbTag, int period, float min=0.5,
88  float max=1.1, int retry=0, HouseKeeperScope scope=kHKGlobal,
90  bool DoCheckRun(QDateTime now) override; // HouseKeeperTask
91  virtual bool InWindow(const QDateTime& now);
92  virtual bool PastWindow(const QDateTime& now);
93  QDateTime UpdateLastRun(const QDateTime& last, bool successful=true) override; // HouseKeeperTask
94  void SetLastRun(const QDateTime& last, bool successful=true) override; // HouseKeeperTask
95  virtual void SetWindow(float min, float max);
96 
97  protected:
98  virtual void CalculateWindow(void);
99 
100  int m_period;
101  int m_retry;
102  QPair<float,float> m_windowPercent;
103  QPair<int,int> m_windowElapsed;
105 };
106 
108 {
109  public:
110  DailyHouseKeeperTask(const QString &dbTag,
113  DailyHouseKeeperTask(const QString &dbTag, int minhour, int maxhour,
116  virtual void SetHourWindow(int min, int max);
117  bool InWindow(const QDateTime& now) override; // PeriodicHouseKeeperTask
118 
119  protected:
120  void CalculateWindow(void) override; // PeriodicHouseKeeperTask
121 
122  private:
123  QPair<int, int> m_windowHour;
124 };
125 
127 {
128  public:
130  MThread("HouseKeeping"), m_idle(true), m_keepRunning(true),
131  m_parent(p) {}
132  ~HouseKeepingThread() = default;
133  void run(void) override; // MThread
134  void Discard(void) { m_keepRunning = false; }
135  bool isIdle(void) { return m_idle; }
136  void Wake(void) { m_waitCondition.wakeAll(); }
137 
138  void Terminate(void);
139 
140  private:
141  bool m_idle;
144  QMutex m_waitMutex;
145  QWaitCondition m_waitCondition;
146 };
147 
148 class MBASE_PUBLIC HouseKeeper : public QObject
149 {
150  Q_OBJECT
151 
152  public:
153  HouseKeeper(void);
154  ~HouseKeeper();
155 
156  void RegisterTask(HouseKeeperTask *task);
157  void Start(void);
158  void StartThread(void);
159  HouseKeeperTask* GetQueuedTask(void);
160 
161  void customEvent(QEvent *e) override; // QObject
162 
163  public slots:
164  void Run(void);
165 
166  private:
167  QTimer *m_timer;
168 
169  QQueue<HouseKeeperTask*> m_taskQueue;
170  QMutex m_queueLock;
171 
172  QMap<QString, HouseKeeperTask*> m_taskMap;
173  QMutex m_mapLock;
174 
175  QList<HouseKeepingThread*> m_threadList;
176  QMutex m_threadLock;
177 };
178 
179 #endif
~HouseKeepingThread()=default
QDateTime m_lastRun
Definition: housekeeper.h:79
Definition for a single task to be run by the HouseKeeper.
Definition: housekeeper.h:39
HouseKeeperScope m_scope
Definition: housekeeper.h:75
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
unsigned int slots[4]
Definition: element.c:38
QList< HouseKeepingThread * > m_threadList
Definition: housekeeper.h:175
struct exc__state * last
Definition: pxsup2dast.c:98
QMutex m_mapLock
Definition: housekeeper.h:173
VERBOSE_PREAMBLE Most true
Definition: verbosedefs.h:91
QMutex m_threadLock
Definition: housekeeper.h:176
bool IsRunning(void)
Definition: housekeeper.h:49
QDateTime GetLastRun(void)
Definition: housekeeper.h:55
General purpose reference counter.
static int startup()
virtual void Terminate(void)
Definition: housekeeper.h:68
bool isIdle(void)
Definition: housekeeper.h:135
void Discard(void)
Definition: housekeeper.h:134
virtual void SetLastRun(const QDateTime &last, bool successful=true)
HouseKeepingThread(HouseKeeper *p)
Definition: housekeeper.h:129
QMutex m_queueLock
Definition: housekeeper.h:170
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
virtual void CalculateWindow(void)
QQueue< HouseKeeperTask * > m_taskQueue
Definition: housekeeper.h:169
task should only run once per cluster e.g.
Definition: housekeeper.h:25
Thread used to perform queued HouseKeeper tasks.
Definition: housekeeper.h:126
task should run on every process e.g.
Definition: housekeeper.h:29
HouseKeeperScope
Definition: housekeeper.h:24
task is queued when HouseKeeper is started
Definition: housekeeper.h:35
HouseKeeperScope GetScope(void)
Definition: housekeeper.h:57
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
QPair< int, int > m_windowHour
Definition: housekeeper.h:123
HouseKeeper * m_parent
Definition: housekeeper.h:143
task is checked normally
Definition: housekeeper.h:34
QTimer * m_timer
Definition: housekeeper.h:167
Manages registered HouseKeeperTasks and queues tasks for operation.
Definition: housekeeper.h:148
Modified HouseKeeperTask for tasks to be run at a regular interval.
Definition: housekeeper.h:84
HouseKeeperStartup m_startup
Definition: housekeeper.h:76
QPair< int, int > m_windowElapsed
Definition: housekeeper.h:103
QMap< QString, HouseKeeperTask * > m_taskMap
Definition: housekeeper.h:172
virtual bool DoCheckRun(QDateTime)
Definition: housekeeper.h:65
void run(void) override
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
bool ConfirmRun(void)
Definition: housekeeper.h:48
QString m_dbTag
Definition: housekeeper.h:73
void Terminate(void)
Modified PeriodicHouseKeeperTask for tasks to be run once daily.
Definition: housekeeper.h:107
QString GetTag(void)
Definition: housekeeper.h:54
QPair< float, float > m_windowPercent
Definition: housekeeper.h:102
QWaitCondition m_waitCondition
Definition: housekeeper.h:145
task is run during HouseKeeper startup
Definition: housekeeper.h:36
QDateTime UpdateLastRun(bool successful=true)
Definition: housekeeper.h:60
QDateTime m_lastUpdate
Definition: housekeeper.h:81
HouseKeeperStartup
Definition: housekeeper.h:33
QDateTime m_lastSuccess
Definition: housekeeper.h:80
virtual bool InWindow(const QDateTime &now)
task should only run once per machine e.g.
Definition: housekeeper.h:27
virtual bool DoRun(void)
Definition: housekeeper.h:66
QDateTime GetLastSuccess(void)
Definition: housekeeper.h:56