Line |
Branch |
Exec |
Source |
1 |
|
|
/* GATE PROJECT LICENSE: |
2 |
|
|
+----------------------------------------------------------------------------+ |
3 |
|
|
| Copyright(c) 2018-2025, Stefan Meislinger | |
4 |
|
|
| All rights reserved. | |
5 |
|
|
| | |
6 |
|
|
| Redistribution and use in source and binary forms, with or without | |
7 |
|
|
| modification, are permitted provided that the following conditions are met:| |
8 |
|
|
| | |
9 |
|
|
| 1. Redistributions of source code must retain the above copyright notice, | |
10 |
|
|
| this list of conditions and the following disclaimer. | |
11 |
|
|
| 2. Redistributions in binary form must reproduce the above copyright | |
12 |
|
|
| notice, this list of conditions and the following disclaimer in the | |
13 |
|
|
| documentation and/or other materials provided with the distribution. | |
14 |
|
|
| | |
15 |
|
|
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| |
16 |
|
|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
17 |
|
|
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
18 |
|
|
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
19 |
|
|
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
20 |
|
|
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
21 |
|
|
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
22 |
|
|
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
23 |
|
|
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
24 |
|
|
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
25 |
|
|
| THE POSSIBILITY OF SUCH DAMAGE. | |
26 |
|
|
+----------------------------------------------------------------------------+ |
27 |
|
|
*/ |
28 |
|
|
|
29 |
|
|
#include "gate/tech/tasks.hpp" |
30 |
|
|
#include "gate/queues.hpp" |
31 |
|
|
|
32 |
|
|
namespace gate |
33 |
|
|
{ |
34 |
|
|
namespace tech |
35 |
|
|
{ |
36 |
|
|
|
37 |
|
✗ |
ITaskFactory::~ITaskFactory() noexcept |
38 |
|
|
{ |
39 |
|
✗ |
} |
40 |
|
|
|
41 |
|
|
|
42 |
|
✗ |
TaskFactoryBase::TaskFactoryBase(String const& taskname, String const& descr, Property const& defaultParams) |
43 |
|
✗ |
: name(taskname.clone()), description(descr.clone()), defaultParameters(defaultParams) |
44 |
|
|
{ |
45 |
|
✗ |
} |
46 |
|
✗ |
TaskFactoryBase::~TaskFactoryBase() noexcept |
47 |
|
|
{ |
48 |
|
✗ |
} |
49 |
|
|
|
50 |
|
✗ |
String TaskFactoryBase::getName() const |
51 |
|
|
{ |
52 |
|
✗ |
return this->name; |
53 |
|
|
} |
54 |
|
✗ |
Property const& TaskFactoryBase::getDefaultParameters() const |
55 |
|
|
{ |
56 |
|
✗ |
return this->defaultParameters; |
57 |
|
|
} |
58 |
|
✗ |
String TaskFactoryBase::getDescription(Property const& parameters) |
59 |
|
|
{ |
60 |
|
|
//TODO |
61 |
|
✗ |
return this->description; |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
|
✗ |
ManagedTask::Statistics::Statistics() |
67 |
|
✗ |
: started(0), completed(0), failed(0) |
68 |
|
|
{ |
69 |
|
✗ |
} |
70 |
|
|
|
71 |
|
✗ |
ManagedTask::ManagedTask(id_t taskid, Ref<ThreadPool> workers, TaskFactory fact, Property const& params, time::Milliseconds autoRepeat) |
72 |
|
✗ |
: id(taskid), threadpool(workers), factory(fact), parameters(params), repeatInterval(autoRepeat), task(NULL) |
73 |
|
|
{ |
74 |
|
✗ |
} |
75 |
|
|
|
76 |
|
✗ |
ManagedTask::~ManagedTask() noexcept |
77 |
|
|
{ |
78 |
|
✗ |
} |
79 |
|
|
|
80 |
|
✗ |
ManagedTask::id_t ManagedTask::getId() |
81 |
|
|
{ |
82 |
|
✗ |
return this->id; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
✗ |
Time ManagedTask::getLastStarted() |
86 |
|
|
{ |
87 |
|
✗ |
MutexLock lock(this->mutex); |
88 |
|
✗ |
return this->stats.lastStartTime; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
✗ |
TimeCounter ManagedTask::getLastStartedTimestamp() |
92 |
|
|
{ |
93 |
|
✗ |
MutexLock lock(this->mutex); |
94 |
|
✗ |
return this->stats.lastStartTimestamp; |
95 |
|
|
} |
96 |
|
|
|
97 |
|
✗ |
Time ManagedTask::getNextPlannedStart() |
98 |
|
|
{ |
99 |
|
✗ |
MutexLock lock(this->mutex); |
100 |
|
✗ |
if (!this->stats.lastStartTime) |
101 |
|
|
{ |
102 |
|
✗ |
return this->stats.lastStartTime; |
103 |
|
|
} |
104 |
|
|
else |
105 |
|
|
{ |
106 |
|
✗ |
return this->stats.lastStartTime + this->repeatInterval; |
107 |
|
|
} |
108 |
|
|
} |
109 |
|
|
|
110 |
|
✗ |
TimeCounter ManagedTask::getNextPlannedStartTimestamp() |
111 |
|
|
{ |
112 |
|
✗ |
MutexLock lock(this->mutex); |
113 |
|
✗ |
if (!this->stats.lastStartTimestamp) |
114 |
|
|
{ |
115 |
|
✗ |
return this->stats.lastStartTimestamp; |
116 |
|
|
} |
117 |
|
|
else |
118 |
|
|
{ |
119 |
|
✗ |
return this->stats.lastStartTimestamp + this->repeatInterval; |
120 |
|
|
} |
121 |
|
|
} |
122 |
|
|
|
123 |
|
✗ |
Task::StatusEnum ManagedTask::getStatus() |
124 |
|
|
{ |
125 |
|
✗ |
MutexLock lock(this->mutex); |
126 |
|
✗ |
if (this->task.empty()) |
127 |
|
|
{ |
128 |
|
✗ |
return Task::Status_Prepared; |
129 |
|
|
} |
130 |
|
|
else |
131 |
|
|
{ |
132 |
|
✗ |
return this->task.status(); |
133 |
|
|
} |
134 |
|
|
} |
135 |
|
|
|
136 |
|
✗ |
void ManagedTask::run() |
137 |
|
|
{ |
138 |
|
✗ |
TimeCounter startTime = TimeCounter::now(); |
139 |
|
✗ |
Task task((NULL)); |
140 |
|
|
{ |
141 |
|
✗ |
MutexLock lock(this->mutex); |
142 |
|
✗ |
task = this->task; |
143 |
|
✗ |
this->stats.started++; |
144 |
|
✗ |
this->stats.lastStartTime = Time::now(); |
145 |
|
✗ |
this->stats.lastStartTimestamp = startTime; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
✗ |
Task::StatusEnum status = Task::Status_Failed; |
149 |
|
|
|
150 |
|
✗ |
time::Milliseconds executionTime; |
151 |
|
✗ |
if (!task.empty()) |
152 |
|
|
{ |
153 |
|
|
try |
154 |
|
|
{ |
155 |
|
✗ |
task.run(); |
156 |
|
✗ |
status = task.status(); |
157 |
|
|
} |
158 |
|
✗ |
catch (...) |
159 |
|
|
{ |
160 |
|
✗ |
status = Task::Status_Failed; |
161 |
|
|
} |
162 |
|
✗ |
TimeCounter endTime = TimeCounter::now(); |
163 |
|
|
|
164 |
|
✗ |
time::Microseconds duration = endTime - startTime; |
165 |
|
✗ |
executionTime = time::duration_cast<time::Milliseconds>(duration); |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
{ |
169 |
|
✗ |
MutexLock lock(this->mutex); |
170 |
|
✗ |
if (status == Task::Status_Failed) |
171 |
|
|
{ |
172 |
|
✗ |
this->stats.failed++; |
173 |
|
|
} |
174 |
|
|
else |
175 |
|
|
{ |
176 |
|
✗ |
this->stats.completed++; |
177 |
|
|
} |
178 |
|
✗ |
this->stats.lastExecutionTime = executionTime; |
179 |
|
✗ |
this->stats.totalExecutionTime += executionTime; |
180 |
|
|
|
181 |
|
✗ |
if (this->repeatInterval.value() == 0) |
182 |
|
|
{ |
183 |
|
|
// release task object |
184 |
|
✗ |
this->task = Task(NULL); |
185 |
|
|
} |
186 |
|
|
} |
187 |
|
✗ |
} |
188 |
|
|
|
189 |
|
|
class GATE_API_LOCAL RunnableManagedTask : public IRunnableBuilder |
190 |
|
|
{ |
191 |
|
|
private: |
192 |
|
|
Ref<ManagedTask> task; |
193 |
|
|
public: |
194 |
|
✗ |
RunnableManagedTask(Ref<ManagedTask> const& mtask) |
195 |
|
✗ |
: task(mtask) |
196 |
|
|
{ |
197 |
|
✗ |
} |
198 |
|
|
|
199 |
|
✗ |
~RunnableManagedTask() noexcept |
200 |
|
✗ |
{ |
201 |
|
✗ |
} |
202 |
|
|
|
203 |
|
✗ |
virtual void run() |
204 |
|
|
{ |
205 |
|
✗ |
if (!this->task.empty()) |
206 |
|
|
{ |
207 |
|
✗ |
this->task->run(); |
208 |
|
|
} |
209 |
|
✗ |
} |
210 |
|
|
}; |
211 |
|
|
|
212 |
|
✗ |
static Runnable createRunnableManagedTask(Ref<ManagedTask> const& task) |
213 |
|
|
{ |
214 |
|
✗ |
RunnableManagedTask* ptr = new RunnableManagedTask(task); |
215 |
|
✗ |
Runnable ret(ptr->c_impl()); |
216 |
|
✗ |
return ret; |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
|
220 |
|
|
|
221 |
|
✗ |
TaskManager::TaskManager() |
222 |
|
✗ |
: workers(Ref<ThreadPool>::fromPtr(new ThreadPool())) |
223 |
|
|
{ |
224 |
|
✗ |
} |
225 |
|
✗ |
TaskManager::~TaskManager() noexcept |
226 |
|
|
{ |
227 |
|
|
|
228 |
|
✗ |
} |
229 |
|
✗ |
void TaskManager::addTaskFactory(Ref<ITaskFactory> const& factory) |
230 |
|
|
{ |
231 |
|
|
//TODO |
232 |
|
✗ |
GATEXX_RAISE_ERROR(results::NotImplemented); |
233 |
|
✗ |
} |
234 |
|
✗ |
Property TaskManager::getTaskDefaultParameters(String const& taskName) |
235 |
|
|
{ |
236 |
|
✗ |
Property ret; |
237 |
|
|
//TODO |
238 |
|
✗ |
GATEXX_RAISE_ERROR(results::NotImplemented); |
239 |
|
✗ |
return ret; |
240 |
|
|
} |
241 |
|
|
|
242 |
|
✗ |
TaskManager::id_t TaskManager::addNewTask(String const& name, Property const& parameters, time::Milliseconds autoRepeat) |
243 |
|
|
{ |
244 |
|
✗ |
id_t ret = 0; |
245 |
|
|
//TODO |
246 |
|
✗ |
GATEXX_RAISE_ERROR(results::NotImplemented); |
247 |
|
✗ |
return ret; |
248 |
|
|
} |
249 |
|
✗ |
Array<TaskManager::id_t> TaskManager::getTasks() |
250 |
|
|
{ |
251 |
|
✗ |
Array<TaskManager::id_t> ret; |
252 |
|
|
//TODO |
253 |
|
✗ |
GATEXX_RAISE_ERROR(results::NotImplemented); |
254 |
|
✗ |
return ret; |
255 |
|
|
} |
256 |
|
✗ |
void TaskManager::removeTask(id_t taskId) |
257 |
|
|
{ |
258 |
|
|
//TODO |
259 |
|
✗ |
GATEXX_RAISE_ERROR(results::NotImplemented); |
260 |
|
✗ |
} |
261 |
|
✗ |
TaskManager::Info TaskManager::getTaskInfo(id_t taskId) |
262 |
|
|
{ |
263 |
|
✗ |
Info ret; |
264 |
|
|
//TODO |
265 |
|
✗ |
GATEXX_RAISE_ERROR(results::NotImplemented); |
266 |
|
✗ |
return ret; |
267 |
|
|
} |
268 |
|
|
|
269 |
|
✗ |
void TaskManager::startTask(id_t id) |
270 |
|
|
{ |
271 |
|
|
//TODO |
272 |
|
✗ |
GATEXX_RAISE_ERROR(results::NotImplemented); |
273 |
|
✗ |
} |
274 |
|
|
|
275 |
|
|
} // end of namespace tech |
276 |
|
|
} // end of namespace gate |
277 |
|
|
|