TaskManager: Check expired tasks hourly.

This commit is contained in:
Nodir Temirkhodjaev 2017-09-21 19:16:50 +05:00
parent dc450a7318
commit 0a787a551b
4 changed files with 53 additions and 4 deletions

View File

@ -64,6 +64,11 @@ void TaskInfo::setLastRun(const QDateTime &lastRun)
}
}
QDateTime TaskInfo::plannedRun() const
{
return m_lastRun.addSecs(m_intervalHours * 60 * 60);
}
void TaskInfo::setLastSuccess(const QDateTime &lastSuccess)
{
if (m_lastSuccess != lastSuccess) {
@ -135,6 +140,11 @@ TaskInfo::TaskType TaskInfo::stringToType(const QString &name)
typeEnum.keyToValue(name.toLatin1()));
}
QDateTime TaskInfo::now()
{
return QDateTime::currentDateTime();
}
void TaskInfo::run()
{
cancel();
@ -163,9 +173,9 @@ void TaskInfo::handleFinished(bool success)
{
if (!m_taskWorker) return;
setLastRun(QDateTime::currentDateTime());
setLastRun(now());
if (success) {
setLastSuccess(QDateTime::currentDateTime());
setLastSuccess(lastRun());
}
emit workFinished(success);

View File

@ -41,6 +41,8 @@ public:
QDateTime lastRun() const { return m_lastRun; }
void setLastRun(const QDateTime &lastRun);
QDateTime plannedRun() const;
QDateTime lastSuccess() const { return m_lastSuccess; }
void setLastSuccess(const QDateTime &lastSuccess);
@ -55,6 +57,8 @@ public:
static QString typeToString(TaskInfo::TaskType type);
static TaskInfo::TaskType stringToType(const QString &name);
static QDateTime now();
signals:
void enabledChanged();
void intervalHoursChanged();

View File

@ -11,6 +11,11 @@ TaskManager::TaskManager(FortManager *fortManager,
m_fortManager(fortManager)
{
setupTasks();
connect(&m_timer, &QTimer::timeout,
this, &TaskManager::runExpiredTasks);
m_timer.setSingleShot(true);
}
QQmlListProperty<TaskInfo> TaskManager::taskInfos()
@ -43,9 +48,11 @@ void TaskManager::loadSettings(const FortSettings *fortSettings)
taskInfo->setRawData(taskData);
}
}
runExpiredTasks();
}
bool TaskManager::saveSettings(FortSettings *fortSettings) const
bool TaskManager::saveSettings(FortSettings *fortSettings)
{
TasksMap tasksMap;
QByteArray taskData;
@ -59,6 +66,8 @@ bool TaskManager::saveSettings(FortSettings *fortSettings) const
tasksMap.insert(taskName, taskData);
}
runExpiredTasks();
return fortSettings->setTasks(tasksMap);
}
@ -73,3 +82,25 @@ void TaskManager::handleTaskFinished(bool success)
saveSettings(m_fortManager->fortSettings());
}
void TaskManager::runExpiredTasks()
{
const QDateTime now = TaskInfo::now();
bool anyTaskEnabled = false;
foreach (TaskInfo *taskInfo, m_taskInfos) {
if (taskInfo->enabled()) {
anyTaskEnabled = true;
if (now > taskInfo->plannedRun()) {
taskInfo->run();
}
}
}
if (anyTaskEnabled) {
m_timer.start(60 * 60 * 1000); // hour
} else {
m_timer.stop();
}
}

View File

@ -3,6 +3,7 @@
#include <QObject>
#include <QQmlListProperty>
#include <QTimer>
class FortManager;
class FortSettings;
@ -28,10 +29,11 @@ signals:
public slots:
void loadSettings(const FortSettings *fortSettings);
bool saveSettings(FortSettings *fortSettings) const;
bool saveSettings(FortSettings *fortSettings);
private slots:
void handleTaskFinished(bool success);
void runExpiredTasks();
private:
void setupTasks();
@ -42,6 +44,8 @@ private:
FortManager *m_fortManager;
QList<TaskInfo *> m_taskInfos;
QTimer m_timer;
};
#endif // TASKMANAGER_H