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 
26  kHKGlobal = 0,
27  kHKLocal,
29  kHKInst
31 };
33 
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;
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_idle(true), m_keepRunning(true),
133  m_parent(p) {}
134  ~HouseKeepingThread() override = default;
135  void run(void) override; // MThread
136  void Discard(void) { m_keepRunning = false; }
137  bool isIdle(void) const { return m_idle; }
138  void Wake(void) { m_waitCondition.wakeAll(); }
139 
140  void Terminate(void);
141 
142  private:
143  bool m_idle;
146  QMutex m_waitMutex;
147  QWaitCondition m_waitCondition;
148 };
149 
150 class MBASE_PUBLIC HouseKeeper : public QObject
151 {
152  Q_OBJECT
153 
154  public:
155  HouseKeeper(void);
156  ~HouseKeeper() override;
157 
158  void RegisterTask(HouseKeeperTask *task);
159  void Start(void);
160  void StartThread(void);
161  HouseKeeperTask* GetQueuedTask(void);
162 
163  void customEvent(QEvent *e) override; // QObject
164 
165  public slots:
166  void Run(void);
167 
168  private:
169  QTimer *m_timer { nullptr };
170 
171  QQueue<HouseKeeperTask*> m_taskQueue;
172  QMutex m_queueLock;
173 
174  QMap<QString, HouseKeeperTask*> m_taskMap;
175  QMutex m_mapLock;
176 
177  QList<HouseKeepingThread*> m_threadList;
178  QMutex m_threadLock;
179 };
180 
181 #endif
Scheduler
Definition: scheduler.h:45
mythevent.h
HouseKeeper
Manages registered HouseKeeperTasks and queues tasks for operation.
Definition: housekeeper.h:150
HouseKeepingThread::m_waitMutex
QMutex m_waitMutex
Definition: housekeeper.h:146
HouseKeepingThread::Discard
void Discard(void)
Definition: housekeeper.h:136
HouseKeeperTask::m_startup
HouseKeeperStartup m_startup
Definition: housekeeper.h:77
HouseKeepingThread::m_keepRunning
bool m_keepRunning
Definition: housekeeper.h:144
PeriodicHouseKeeperTask::m_retry
std::chrono::seconds m_retry
Definition: housekeeper.h:102
HouseKeeper::m_threadList
QList< HouseKeepingThread * > m_threadList
Definition: housekeeper.h:177
HouseKeepingThread::isIdle
bool isIdle(void) const
Definition: housekeeper.h:137
PeriodicHouseKeeperTask::InWindow
virtual bool InWindow(const QDateTime &now)
Definition: housekeeper.cpp:416
mythbaseexp.h
HouseKeeper::m_threadLock
QMutex m_threadLock
Definition: housekeeper.h:178
HouseKeeperTask::DoCheckRun
virtual bool DoCheckRun(const QDateTime &)
Definition: housekeeper.h:66
HouseKeepingThread::HouseKeepingThread
HouseKeepingThread(HouseKeeper *p)
Definition: housekeeper.h:131
HouseKeeper::m_taskQueue
QQueue< HouseKeeperTask * > m_taskQueue
Definition: housekeeper.h:171
startup
static int startup()
Definition: mythtv/programs/mythshutdown/main.cpp:771
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:10
true
VERBOSE_PREAMBLE Most true
Definition: verbosedefs.h:91
mythdate.h
HouseKeepingThread::Terminate
void Terminate(void)
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
HouseKeeperTask::SetLastRun
virtual void SetLastRun(const QDateTime &last, bool successful=true)
Definition: housekeeper.cpp:302
PeriodicHouseKeeperTask::CalculateWindow
virtual void CalculateWindow(void)
Definition: housekeeper.cpp:335
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:138
HouseKeeper::m_queueLock
QMutex m_queueLock
Definition: housekeeper.h:172
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
kHKRunImmediateOnStartup
@ kHKRunImmediateOnStartup
task is run during HouseKeeper startup
Definition: housekeeper.h:37
HouseKeeperTask::GetScope
HouseKeeperScope GetScope(void)
Definition: housekeeper.h:58
HouseKeeperTask::GetTag
QString GetTag(void)
Definition: housekeeper.h:55
HouseKeeperScope
HouseKeeperScope
Definition: housekeeper.h:25
HouseKeeperStartup
HouseKeeperStartup
Definition: housekeeper.h:34
referencecounter.h
HouseKeeperTask::m_lastSuccess
QDateTime m_lastSuccess
Definition: housekeeper.h:81
HouseKeepingThread::m_idle
bool m_idle
Definition: housekeeper.h:143
HouseKeepingThread::m_parent
HouseKeeper * m_parent
Definition: housekeeper.h:145
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:174
DailyHouseKeeperTask::m_windowHour
QPair< std::chrono::hours, std::chrono::hours > m_windowHour
Definition: housekeeper.h:125
kHKNormal
@ kHKNormal
task is checked normally
Definition: housekeeper.h:35
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
kHKInst
@ kHKInst
task should run on every process e.g.
Definition: housekeeper.h:30
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:511
kHKRunOnStartup
@ kHKRunOnStartup
task is queued when HouseKeeper is started
Definition: housekeeper.h:36
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:147
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
kHKLocal
@ kHKLocal
task should only run once per machine e.g.
Definition: housekeeper.h:28
HouseKeeperTask::m_scope
HouseKeeperScope m_scope
Definition: housekeeper.h:76
HouseKeeper::m_mapLock
QMutex m_mapLock
Definition: housekeeper.h:175
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
PeriodicHouseKeeperTask::m_currentProb
double m_currentProb
Definition: housekeeper.h:105
kHKGlobal
@ kHKGlobal
task should only run once per cluster e.g.
Definition: housekeeper.h:26
HouseKeeperTask::IsRunning
bool IsRunning(void) const
Definition: housekeeper.h:50