Heart 1.3.842.34c2ab5
Heart is base back end library for your c++ Qt projects.
taskscheduler.cpp
Go to the documentation of this file.
1//#
2//# Copyright (C) 2021-2024 QuasarApp.
3//# Distributed under the lgplv3 software license, see the accompanying
4//# Everyone is permitted to copy and distribute verbatim copies
5//# of this license document, but changing it is not allowed.
6//#
7
8
9#include "taskscheduler.h"
10#include <QDateTime>
11
12namespace QH {
13
15 _timer = new QTimer();
16
17 connect(_timer, &QTimer::timeout, this, &TaskScheduler::handleTimeOut);
18}
19
21 _timer->stop();
22 delete _timer;
23}
24
25int QH::TaskScheduler::getTimeout(qint64 timeout) {
26 return std::max(std::min(static_cast<qint64>(AbstractTask::Day),
27 timeout), static_cast<qint64>(0));
28}
29
30bool TaskScheduler::shedule(const QSharedPointer<AbstractTask> &task) {
31 if (!task->isValid())
32 return false;
33
34 quint64 currentTime = QDateTime::currentMSecsSinceEpoch();
35 quint64 invokeTime = 0;
36
37 switch (task->mode()) {
39 invokeTime = currentTime + task->time();
40 break;
41 }
43 invokeTime = currentTime + task->time();
44 break;
45 }
47 invokeTime = task->time();
48
49 if (invokeTime < currentTime)
50 return false;
51 break;
52 }
53 }
54
55 _taskPool[task->taskId()] = task;
56 _taskQueue.insert(invokeTime, task->taskId());
57
58
59 qint64 top = _taskQueue.begin().key();
60 qint64 timeout = top - currentTime;
61
62 _timer->start(getTimeout(timeout));
63
64 return true;
65}
66
67bool TaskScheduler::remove(const QSharedPointer<AbstractTask> &task) {
68 return remove(task->taskId());
69}
70
71bool TaskScheduler::remove(int task) {
72 auto val = _taskQueue.key(task);
73
74 _taskQueue.remove(val);
75 _taskPool.remove(task);
76
77 return true;
78}
79
81 return _taskPool.size();
82}
83
84void TaskScheduler::handleTimeOut() {
85 auto top = _taskQueue.begin();
86
87 if (top == _taskQueue.end()) {
88 _timer->stop();
89 return;
90 }
91
92 unsigned long long currentTime = QDateTime::currentMSecsSinceEpoch();
93 if (top.key() > currentTime) {
94 _timer->start(getTimeout(top.key() - currentTime));
95 return;
96 }
97
98 auto toWork = _taskQueue.values(top.key());
99
100 for (int key: toWork) {
101 auto task = _taskPool.value(key);
102
103 if (!task) {
104 _taskPool.remove(key);
105 continue;
106 }
107
108 remove(task);
109
110 if (task->mode() == ScheduleMode::Repeat) {
111 shedule(task);
112 }
113
114 emit sigPushWork(task);
115 }
116}
117}
void sigPushWork(QSharedPointer< QH::AbstractTask > work)
sigPushWork This signal emited when the task work neet to execute.
bool shedule(const QSharedPointer< AbstractTask > &task)
shedule This method shedule new task in this node.
int taskCount() const
taskCount This method return tasks count.
bool remove(const QSharedPointer< AbstractTask > &task)
remove This method remove the task from a tasks queue.
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13
@ TimePoint
In this mode AbstractTask will be executed int time msecunds by Unix time.
@ Repeat
In this mode AbstractTask will be executed task every time from the moment of adding this task.
@ SingleWork
In this mode AbstractTask will be executed after time msecunds from the moment of adding this task.