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