3 namespace Org\Mxchange\CoreFramework\Handler\Task;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Handler\BaseHandler;
9 use Org\Mxchange\CoreFramework\Lists\Listable;
10 use Org\Mxchange\CoreFramework\Registry\Registerable;
11 use Org\Mxchange\CoreFramework\Task\Taskable;
12 use Org\Mxchange\CoreFramework\Visitor\Visitable;
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
37 // Exception constants
38 const EXCEPTION_TASK_IS_INVALID = 0xb00;
41 * Protected constructor
45 protected function __construct () {
46 // Call parent constructor
47 parent::__construct(__CLASS__);
50 $this->setHandlerName('task');
54 * Creates an instance of this class
56 * @return $handlerInstance An instance of a HandleableTask class
58 public static final function createTaskHandler () {
60 $handlerInstance = new TaskHandler();
62 // Output debug message
63 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Initializing task handler.');
66 $handlerInstance->setListInstance(ObjectFactory::createObjectByConfiguredName('task_list_class'));
68 // Get default instance
69 $handlerInstance->setIteratorInstance($handlerInstance->getListInstance()->getIterator());
71 // Init visitor instance for faster loop
72 $handlerInstance->setVisitorInstance(ObjectFactory::createObjectByConfiguredName('active_task_visitor_class'));
74 // Register the first (and generic) idle-loop task
75 $taskInstance = ObjectFactory::createObjectByConfiguredName('idle_task_class');
76 $handlerInstance->registerTask('idle_loop', $taskInstance);
78 // Output debug message
79 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Task handler initialized.');
81 // Return the prepared instance
82 return $handlerInstance;
86 * Tries to execute the given task. If as task should not be started (yet)
87 * or the interval time (see task_interval_delay) is not yet reached the
88 * task is quietly skipped.
91 * @throws InvalidTaskException If the current task is invalid
93 private function executeCurrentTask () {
94 // Update no task by default
97 // Is the current task valid?
98 if (!$this->getListInstance()->getIterator()->valid()) {
100 throw new InvalidTaskException($this, self::EXCEPTION_TASK_IS_INVALID);
104 $currentTask = $this->getListInstance()->getIterator()->current();
106 // Is the task not yet started?
107 if ($currentTask['task_started'] === false) {
108 // Determine difference between current time and registration
109 $diff = ($this->getMilliTime() - $currentTask['task_registered']) * 1000;
111 // Should we start now?
112 if ($diff < $currentTask['task_startup_delay']) {
113 // Skip this silently
114 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Task ' . $currentTask['id'] . ' not started: diff=' . $diff . ',task_startup_delay=' . $currentTask['task_startup_delay']);
118 // Launch the task and mark it as updated
119 $currentTask['task_started'] = true;
123 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Task ' . $currentTask['id'] . ' started with startup_delay=' . $currentTask['task_startup_delay'] . 'ms');
126 // Get time difference from interval delay
127 $diff = ($this->getMilliTime() - $currentTask['task_last_activity']) * 1000;
130 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Task ' . $currentTask['id'] . ' diff=' . $diff . ',task_interval_delay=' . $currentTask['task_interval_delay'] . ',task_max_runs=' . $currentTask['task_max_runs'] . ',task_total_runs=' . $currentTask['task_total_runs']);
132 // Is the interval delay reached?
133 if ((($diff < $currentTask['task_interval_delay']) && ($currentTask['task_max_runs'] == 0)) || (($currentTask['task_max_runs'] > 0) && ($currentTask['task_total_runs'] == $currentTask['task_max_runs']))) {
134 // Should we update the task from startup?
135 if ($updateTask === true) {
136 // Update the task before leaving
137 $this->updateTask($currentTask);
140 // Skip this silently
145 $currentTask['task_last_activity'] = $this->getMilliTime();
148 $currentTask['task_total_runs']++;
151 $this->updateTask($currentTask);
154 // @TODO Messurement can be added around this call
155 $currentTask['task_instance']->accept($this->getVisitorInstance());
159 * Updates given task by updating the underlaying list
161 * @param $taskEntry An array with a task
164 private function updateTask (array $taskEntry) {
165 // Get the key from current iteration
166 $key = $this->getListInstance()->getIterator()->key();
168 // Get the hash from key
169 $hash = $this->getListInstance()->getHash($key);
172 $this->getListInstance()->updateCurrentEntryByHash($hash, $taskEntry);
176 * Unregisters the given task
178 * @param $taskData Data of the task
181 private function unregisterTask (array $taskData) {
183 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Removing task ' . $taskData['id'] . ' from queue - CALLED!');
186 $this->getListInstance()->removeEntry('tasks', $taskData);
189 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Removing task ' . $taskData['id'] . ' from queue - EXIT!');
193 * Searches a task by given instance
195 * @param $taskInstanc An instanceof a Taskable class
196 * @return $taskName Name of the task as used while registration
198 public function searchTask (Taskable $taskInstance) {
199 // Default is an empty (not found) task name
203 $taskList = $this->getListInstance()->getArrayFromList('tasks');
205 // Search all instances
206 foreach ($taskList as $currentTask) {
207 // Does it match given task instance?
208 if ($currentTask['task_instance']->equals($taskInstance)) {
210 $taskName = $currentTask['id'];
222 * Registers a task with a task handler.
224 * @param $taskName A task name to register the task on
225 * @param $taskInstance The instance that should be registered as a task
228 public function registerTask ($taskName, Visitable $taskInstance) {
229 // Get interval delay
230 $intervalDelay = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('task_' . $taskName . '_interval_delay');
231 $startupDelay = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('task_' . $taskName . '_startup_delay');
233 // If the task is 'idle_loop', a deplay of zero seconds is fine
234 assert($intervalDelay >= 0);
235 assert(($taskName === 'idle_loop') || (($taskName != 'idle_loop') && ($intervalDelay > 0)));
236 assert(($taskName === 'idle_loop') || (($taskName != 'idle_loop') && ($startupDelay > 0)));
240 // Identifier for the generateHash() method
242 // Whether the task is started
243 'task_started' => false,
244 // Whether the task is paused (not yet implemented)
245 'task_paused' => false,
246 // Whether the task can be paused (not yet implemented)
247 'task_pauseable' => true,
248 // Timestamp of registration
249 'task_registered' => $this->getMilliTime(),
250 // Last activity timestamp
251 'task_last_activity' => 0,
252 // Total runs of this task
253 'task_total_runs' => 0,
254 // Task instance itself
255 'task_instance' => $taskInstance,
256 // Startup delay in milliseconds
257 'task_startup_delay' => $startupDelay,
258 // Interval time (delay) in milliseconds before this task is executed again
259 'task_interval_delay' => $intervalDelay,
260 // How often should this task run?
261 'task_max_runs' => FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('task_' . $taskName . '_max_runs'),
265 $this->getListInstance()->addEntry('tasks', $taskEntry);
268 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Task registered: taskName=' . $taskName .
269 ' (taskInstance=' . $taskInstance->__toString() . ')' .
270 ', startupDelay=' . $taskEntry['task_startup_delay'] . 'ms' .
271 ', intervalDelay=' . $taskEntry['task_interval_delay'] . 'ms' .
272 ', maxRuns=' . $taskEntry['task_max_runs'] . ' ...'
277 * Checks whether tasks are left including idle task
279 * @return $tasksLeft Whether there are tasks left to handle
281 public function hasTasksLeft () {
282 // Do we have tasks there?
283 $tasksLeft = (($this->getListInstance() instanceof Listable) && ($this->getListInstance()->count() > 0));
290 * Handles all tasks by checking if they should startup or if it is their
291 * turn to run. You should use this method in a while() loop in conjuntion
292 * with hasTasksLeft() so you can e.g. shutdown by adding a ShutdownTask
293 * which will attempt to remove all tasks from the task handler.
297 public function handleTasks () {
299 if (!$this->getListInstance()->getIterator()->valid()) {
300 // Rewind to the beginning for next loop
301 $this->getListInstance()->getIterator()->rewind();
304 // Try to execute the task
305 $this->executeCurrentTask();
308 $this->getListInstance()->getIterator()->next();
312 * Shuts down all tasks and the task handler itself. This method should be
313 * called from a corresponding filter class.
317 public function doShutdown () {
318 // Always rewind to the beginning for next loop
319 $this->getListInstance()->getIterator()->rewind();
322 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Shutting down all ' . $this->getListInstance()->count() . ' tasks...');
324 // Remember all tasks that has been shutdown for removal
327 // Instance a visitor
328 $this->setVisitorInstance(ObjectFactory::createObjectByConfiguredName('shutdown_task_visitor_class'));
330 // Shutdown all tasks in once go
331 while ($this->getListInstance()->getIterator()->valid()) {
333 $currentTask = $this->getListInstance()->getIterator()->current();
335 // Output debug message
336 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Shutting down task ' . $currentTask['id'] . ' (taskInstance=' . $currentTask['task_instance']->__toString() . ') ...');
339 $currentTask['task_instance']->accept($this->getVisitorInstance());
341 // Remember this task
342 array_push($tasks, $currentTask);
344 // Advance to next one
345 $this->getListInstance()->getIterator()->next();
349 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Shutdown of all tasks completed.');
352 foreach ($tasks as $entry) {
353 $this->unregisterTask($entry);