15 _timer =
new QTimer();
17 connect(_timer, &QTimer::timeout,
this, &TaskScheduler::handleTimeOut);
25int QH::TaskScheduler::getTimeout(qint64 timeout) {
27 timeout),
static_cast<qint64
>(0));
34 quint64 currentTime = QDateTime::currentMSecsSinceEpoch();
35 quint64 invokeTime = 0;
37 switch (task->mode()) {
39 invokeTime = currentTime + task->time();
43 invokeTime = currentTime + task->time();
47 invokeTime = task->time();
49 if (invokeTime < currentTime)
55 _taskPool[task->taskId()] = task;
56 _taskQueue.insert(invokeTime, task->taskId());
59 qint64 top = _taskQueue.begin().key();
60 qint64 timeout = top - currentTime;
62 _timer->start(getTimeout(timeout));
68 return remove(task->taskId());
72 auto val = _taskQueue.key(task);
74 _taskQueue.remove(val);
75 _taskPool.remove(task);
81 return _taskPool.size();
84void TaskScheduler::handleTimeOut() {
85 auto top = _taskQueue.begin();
87 if (top == _taskQueue.end()) {
92 unsigned long long currentTime = QDateTime::currentMSecsSinceEpoch();
93 if (top.key() > currentTime) {
94 _timer->start(getTimeout(top.key() - currentTime));
98 auto toWork = _taskQueue.values(top.key());
100 for (
int key: toWork) {
101 auto task = _taskPool.value(key);
104 _taskPool.remove(key);
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...
@ 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.