Heart
1.4.918.d927d9c
Heart is base back end library for your c++ Qt projects.
Toggle main menu visibility
Loading...
Searching...
No Matches
taskscheduler.cpp
Go to the documentation of this file.
1
//#
2
//# Copyright (C) 2021-2025 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
12
namespace
QH
{
13
14
TaskScheduler::TaskScheduler
() {
15
_timer =
new
QTimer();
16
17
connect(_timer, &QTimer::timeout,
this
, &TaskScheduler::handleTimeOut);
18
}
19
20
TaskScheduler::~TaskScheduler
() {
21
_timer->stop();
22
delete
_timer;
23
}
24
25
int
QH::TaskScheduler::getTimeout(qint64 timeout) {
26
return
std::max(std::min(
static_cast<
qint64
>
(
AbstractTask::Day
),
27
timeout),
static_cast<
qint64
>
(0));
28
}
29
30
bool
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()) {
38
case
ScheduleMode::SingleWork
: {
39
invokeTime = currentTime + task->time();
40
break
;
41
}
42
case
ScheduleMode::Repeat
: {
43
invokeTime = currentTime + task->time();
44
break
;
45
}
46
case
ScheduleMode::TimePoint
: {
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
67
bool
TaskScheduler::remove
(
const
QSharedPointer<AbstractTask> &task) {
68
return
remove
(task->taskId());
69
}
70
71
bool
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
80
int
TaskScheduler::taskCount
()
const
{
81
return
_taskPool.size();
82
}
83
84
void
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
}
QH::AbstractTask::Day
@ Day
Days.
Definition
abstracttask.h:50
QH::TaskScheduler::~TaskScheduler
~TaskScheduler()
Definition
taskscheduler.cpp:20
QH::TaskScheduler::sigPushWork
void sigPushWork(QSharedPointer< QH::AbstractTask > work)
sigPushWork This signal emited when the task work neet to execute.
QH::TaskScheduler::shedule
bool shedule(const QSharedPointer< AbstractTask > &task)
shedule This method shedule new task in this node.
Definition
taskscheduler.cpp:30
QH::TaskScheduler::TaskScheduler
TaskScheduler()
Definition
taskscheduler.cpp:14
QH::TaskScheduler::taskCount
int taskCount() const
taskCount This method return tasks count.
Definition
taskscheduler.cpp:80
QH::TaskScheduler::remove
bool remove(const QSharedPointer< AbstractTask > &task)
remove This method remove the task from a tasks queue.
Definition
taskscheduler.cpp:67
QH
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition
heart.cpp:13
QH::ScheduleMode::TimePoint
@ TimePoint
In this mode AbstractTask will be executed int time msecunds by Unix time.
Definition
abstracttask.h:26
QH::ScheduleMode::Repeat
@ Repeat
In this mode AbstractTask will be executed task every time from the moment of adding this task.
Definition
abstracttask.h:24
QH::ScheduleMode::SingleWork
@ SingleWork
In this mode AbstractTask will be executed after time msecunds from the moment of adding this task.
Definition
abstracttask.h:22
taskscheduler.h
src
private
taskscheduler.cpp
Generated by
1.18.0