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,
30 };
32 
34  kHKNormal = 0,
37 };
38 
40 {
41  public:
42  explicit HouseKeeperTask(const QString &dbTag, HouseKeeperScope scope=kHKGlobal,
44  ~HouseKeeperTask() override = default;
45 
46  bool CheckRun(const QDateTime& now);
47  bool Run(void);
48  bool ConfirmRun(void) const { return m_confirm; }
49  bool IsRunning(void) const { 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(const 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(const 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  explicit 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() override = default;
133  void run(void) override; // MThread
134  void Discard(void) { m_keepRunning = false; }
135  bool isIdle(void) const { 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() override;
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 { nullptr };
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
Scheduler
Definition: scheduler.h:46
e
QDomElement e
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:1420
mythevent.h
HouseKeeper
Manages registered HouseKeeperTasks and queues tasks for operation.
Definition: housekeeper.h:149
HouseKeepingThread::m_waitMutex
QMutex m_waitMutex
Definition: housekeeper.h:144
HouseKeepingThread::Discard
void Discard(void)
Definition: housekeeper.h:134
HouseKeeperTask::m_startup
HouseKeeperStartup m_startup
Definition: housekeeper.h:76
HouseKeepingThread::m_keepRunning
bool m_keepRunning
Definition: housekeeper.h:142
DailyHouseKeeperTask::m_windowHour
QPair< int, int > m_windowHour
Definition: housekeeper.h:123
HouseKeeper::m_threadList
QList< HouseKeepingThread * > m_threadList
Definition: housekeeper.h:175
HouseKeepingThread::isIdle
bool isIdle(void) const
Definition: housekeeper.h:135
PeriodicHouseKeeperTask::InWindow
virtual bool InWindow(const QDateTime &now)
Definition: housekeeper.cpp:420
mythbaseexp.h
HouseKeeper::m_threadLock
QMutex m_threadLock
Definition: housekeeper.h:176
HouseKeeperTask::DoCheckRun
virtual bool DoCheckRun(const QDateTime &)
Definition: housekeeper.h:65
HouseKeepingThread::HouseKeepingThread
HouseKeepingThread(HouseKeeper *p)
Definition: housekeeper.h:129
HouseKeeper::m_taskQueue
QQueue< HouseKeeperTask * > m_taskQueue
Definition: housekeeper.h:169
startup
static int startup()
Definition: mythtv/programs/mythshutdown/main.cpp:771
HouseKeeperTask::m_lastUpdate
QDateTime m_lastUpdate
Definition: housekeeper.h:81
HouseKeeperTask::UpdateLastRun
QDateTime UpdateLastRun(bool successful=true)
Definition: housekeeper.h:60
HouseKeeperTask::Terminate
virtual void Terminate(void)
Definition: housekeeper.h:68
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:304
PeriodicHouseKeeperTask::CalculateWindow
virtual void CalculateWindow(void)
Definition: housekeeper.cpp:337
hardwareprofile.config.p
p
Definition: config.py:33
HouseKeepingThread::~HouseKeepingThread
~HouseKeepingThread() override=default
HouseKeeperTask::GetLastRun
QDateTime GetLastRun(void)
Definition: housekeeper.h:55
HouseKeepingThread::Wake
void Wake(void)
Definition: housekeeper.h:136
HouseKeeper::m_queueLock
QMutex m_queueLock
Definition: housekeeper.h:170
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:127
PeriodicHouseKeeperTask::m_period
int m_period
Definition: housekeeper.h:100
kHKRunImmediateOnStartup
@ kHKRunImmediateOnStartup
task is run during HouseKeeper startup
Definition: housekeeper.h:36
HouseKeeperTask::GetScope
HouseKeeperScope GetScope(void)
Definition: housekeeper.h:57
HouseKeeperTask::GetTag
QString GetTag(void)
Definition: housekeeper.h:54
HouseKeeperScope
HouseKeeperScope
Definition: housekeeper.h:24
HouseKeeperStartup
HouseKeeperStartup
Definition: housekeeper.h:33
referencecounter.h
HouseKeeperTask::m_lastSuccess
QDateTime m_lastSuccess
Definition: housekeeper.h:80
PeriodicHouseKeeperTask::m_windowElapsed
QPair< int, int > m_windowElapsed
Definition: housekeeper.h:103
HouseKeepingThread::m_idle
bool m_idle
Definition: housekeeper.h:141
HouseKeepingThread::m_parent
HouseKeeper * m_parent
Definition: housekeeper.h:143
HouseKeeper::m_taskMap
QMap< QString, HouseKeeperTask * > m_taskMap
Definition: housekeeper.h:172
kHKNormal
@ kHKNormal
task is checked normally
Definition: housekeeper.h:34
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
mthread.h
PeriodicHouseKeeperTask::m_retry
int m_retry
Definition: housekeeper.h:101
HouseKeeperTask::~HouseKeeperTask
~HouseKeeperTask() override=default
kHKInst
@ kHKInst
task should run on every process e.g.
Definition: housekeeper.h:29
PeriodicHouseKeeperTask::m_currentProb
float m_currentProb
Definition: housekeeper.h:104
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:514
kHKRunOnStartup
@ kHKRunOnStartup
task is queued when HouseKeeper is started
Definition: housekeeper.h:35
PeriodicHouseKeeperTask::m_windowPercent
QPair< float, float > m_windowPercent
Definition: housekeeper.h:102
HouseKeeperTask::GetLastSuccess
QDateTime GetLastSuccess(void)
Definition: housekeeper.h:56
HouseKeepingThread::m_waitCondition
QWaitCondition m_waitCondition
Definition: housekeeper.h:145
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:66
kHKLocal
@ kHKLocal
task should only run once per machine e.g.
Definition: housekeeper.h:27
HouseKeeperTask::m_scope
HouseKeeperScope m_scope
Definition: housekeeper.h:75
HouseKeeper::m_mapLock
QMutex m_mapLock
Definition: housekeeper.h:173
DailyHouseKeeperTask
Modified PeriodicHouseKeeperTask for tasks to be run once daily.
Definition: housekeeper.h:108
HouseKeeperTask::m_dbTag
QString m_dbTag
Definition: housekeeper.h:73
HouseKeeperTask::m_lastRun
QDateTime m_lastRun
Definition: housekeeper.h:79
ReferenceCounter
General purpose reference counter.
Definition: referencecounter.h:27
HouseKeeperTask::ConfirmRun
bool ConfirmRun(void) const
Definition: housekeeper.h:48
kHKGlobal
@ kHKGlobal
task should only run once per cluster e.g.
Definition: housekeeper.h:25
HouseKeeperTask::IsRunning
bool IsRunning(void) const
Definition: housekeeper.h:49