3 namespace Org\Mxchange\CoreFramework\Handler\Task;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Handler\BaseHandler;
8 use Org\Mxchange\CoreFramework\Lists\Listable;
9 use Org\Mxchange\CoreFramework\Registry\Registerable;
10 use Org\Mxchange\CoreFramework\Task\Taskable;
11 use Org\Mxchange\CoreFramework\Visitor\Visitable;
16 * @author Roland Haeder <webmaster@shipsimu.org>
18 <<<<<<< HEAD:framework/main/classes/handler/tasks/class_TaskHandler.php
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
22 >>>>>>> Some updates::inc/main/classes/handler/tasks/class_TaskHandler.php
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
40 // Exception constants
41 const EXCEPTION_TASK_IS_INVALID = 0xb00;
44 * Protected constructor
48 protected function __construct () {
49 // Call parent constructor
50 parent::__construct(__CLASS__);
53 $this->setHandlerName('task');
57 * Creates an instance of this class
59 * @return $handlerInstance An instance of a HandleableTask class
61 public static final function createTaskHandler () {
63 $handlerInstance = new TaskHandler();
65 // Output debug message
66 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Initializing task handler.');
69 $handlerInstance->setListInstance(ObjectFactory::createObjectByConfiguredName('task_list_class'));
71 // Get default instance
72 $handlerInstance->setIteratorInstance($handlerInstance->getListInstance()->getIterator());
74 // Init visitor instance for faster loop
75 $handlerInstance->setVisitorInstance(ObjectFactory::createObjectByConfiguredName('active_task_visitor_class'));
77 // Register the first (and generic) idle-loop task
78 $taskInstance = ObjectFactory::createObjectByConfiguredName('idle_task_class');
79 $handlerInstance->registerTask('idle_loop', $taskInstance);
81 // Output debug message
82 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Task handler initialized.');
84 // Return the prepared instance
85 return $handlerInstance;
89 * Tries to execute the given task. If as task should not be started (yet)
90 * or the interval time (see task_interval_delay) is not yet reached the
91 * task is quietly skipped.
94 * @throws InvalidTaskException If the current task is invalid
96 private function executeCurrentTask () {
97 // Update no task by default
100 // Is the current task valid?
101 if (!$this->getListInstance()->getIterator()->valid()) {
103 throw new InvalidTaskException($this, self::EXCEPTION_TASK_IS_INVALID);
107 $currentTask = $this->getListInstance()->getIterator()->current();
109 // Is the task not yet started?
110 if ($currentTask['task_started'] === false) {
111 // Determine difference between current time and registration
112 $diff = ($this->getMilliTime() - $currentTask['task_registered']) * 1000;
114 // Should we start now?
115 if ($diff < $currentTask['task_startup_delay']) {
116 // Skip this silently
117 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Task ' . $currentTask['id'] . ' not started: diff=' . $diff . ',task_startup_delay=' . $currentTask['task_startup_delay']);
121 // Launch the task and mark it as updated
122 $currentTask['task_started'] = true;
126 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Task ' . $currentTask['id'] . ' started with startup_delay=' . $currentTask['task_startup_delay'] . 'ms');
129 // Get time difference from interval delay
130 $diff = ($this->getMilliTime() - $currentTask['task_last_activity']) * 1000;
133 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: 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']);
135 // Is the interval delay reached?
136 if ((($diff < $currentTask['task_interval_delay']) && ($currentTask['task_max_runs'] == 0)) || (($currentTask['task_max_runs'] > 0) && ($currentTask['task_total_runs'] == $currentTask['task_max_runs']))) {
137 // Should we update the task from startup?
138 if ($updateTask === true) {
139 // Update the task before leaving
140 $this->updateTask($currentTask);
143 // Skip this silently
148 $currentTask['task_last_activity'] = $this->getMilliTime();
151 $currentTask['task_total_runs']++;
154 $this->updateTask($currentTask);
157 // @TODO Messurement can be added around this call
158 $currentTask['task_instance']->accept($this->getVisitorInstance());
162 * Updates given task by updating the underlaying list
164 * @param $taskEntry An array with a task
167 private function updateTask (array $taskEntry) {
168 // Get the key from current iteration
169 $key = $this->getListInstance()->getIterator()->key();
171 // Get the hash from key
172 $hash = $this->getListInstance()->getHash($key);
175 $this->getListInstance()->updateCurrentEntryByHash($hash, $taskEntry);
179 * Unregisters the given task
181 * @param $taskData Data of the task
184 private function unregisterTask (array $taskData) {
186 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Removing task ' . $taskData['id'] . ' from queue - CALLED!');
189 $this->getListInstance()->removeEntry('tasks', $taskData);
192 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Removing task ' . $taskData['id'] . ' from queue - EXIT!');
196 * Searches a task by given instance
198 * @param $taskInstanc An instanceof a Taskable class
199 * @return $taskName Name of the task as used while registration
201 public function searchTask (Taskable $taskInstance) {
202 // Default is an empty (not found) task name
206 $taskList = $this->getListInstance()->getArrayFromList('tasks');
208 // Search all instances
209 foreach ($taskList as $currentTask) {
210 // Does it match given task instance?
211 if ($currentTask['task_instance']->equals($taskInstance)) {
213 $taskName = $currentTask['id'];
225 * Registers a task with a task handler.
227 * @param $taskName A task name to register the task on
228 * @param $taskInstance The instance that should be registered as a task
231 public function registerTask ($taskName, Visitable $taskInstance) {
232 // Get interval delay
233 $intervalDelay = $this->getConfigInstance()->getConfigEntry('task_' . $taskName . '_interval_delay');
234 $startupDelay = $this->getConfigInstance()->getConfigEntry('task_' . $taskName . '_startup_delay');
236 // If the task is 'idle_loop', a deplay of zero seconds is fine
237 assert($intervalDelay >= 0);
238 assert(($taskName === 'idle_loop') || (($taskName != 'idle_loop') && ($intervalDelay > 0)));
239 assert(($taskName === 'idle_loop') || (($taskName != 'idle_loop') && ($startupDelay > 0)));
243 // Identifier for the generateHash() method
245 // Whether the task is started
246 'task_started' => false,
247 // Whether the task is paused (not yet implemented)
248 'task_paused' => false,
249 // Whether the task can be paused (not yet implemented)
250 'task_pauseable' => true,
251 // Timestamp of registration
252 'task_registered' => $this->getMilliTime(),
253 // Last activity timestamp
254 'task_last_activity' => 0,
255 // Total runs of this task
256 'task_total_runs' => 0,
257 // Task instance itself
258 'task_instance' => $taskInstance,
259 // Startup delay in milliseconds
260 'task_startup_delay' => $startupDelay,
261 // Interval time (delay) in milliseconds before this task is executed again
262 'task_interval_delay' => $intervalDelay,
263 // How often should this task run?
264 'task_max_runs' => $this->getConfigInstance()->getConfigEntry('task_' . $taskName . '_max_runs'),
268 $this->getListInstance()->addEntry('tasks', $taskEntry);
271 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Task registered: taskName=' . $taskName .
272 ' (taskInstance=' . $taskInstance->__toString() . ')' .
273 ', startupDelay=' . $taskEntry['task_startup_delay'] . 'ms' .
274 ', intervalDelay=' . $taskEntry['task_interval_delay'] . 'ms' .
275 ', maxRuns=' . $taskEntry['task_max_runs'] . ' ...'
280 * Checks whether tasks are left including idle task
282 * @return $tasksLeft Whether there are tasks left to handle
284 public function hasTasksLeft () {
285 // Do we have tasks there?
286 $tasksLeft = (($this->getListInstance() instanceof Listable) && ($this->getListInstance()->count() > 0));
293 * Handles all tasks by checking if they should startup or if it is their
294 * turn to run. You should use this method in a while() loop in conjuntion
295 * with hasTasksLeft() so you can e.g. shutdown by adding a ShutdownTask
296 * which will attempt to remove all tasks from the task handler.
300 public function handleTasks () {
302 if (!$this->getListInstance()->getIterator()->valid()) {
303 // Rewind to the beginning for next loop
304 $this->getListInstance()->getIterator()->rewind();
307 // Try to execute the task
308 $this->executeCurrentTask();
311 $this->getListInstance()->getIterator()->next();
315 * Shuts down all tasks and the task handler itself. This method should be
316 * called from a corresponding filter class.
320 public function doShutdown () {
321 // Always rewind to the beginning for next loop
322 $this->getListInstance()->getIterator()->rewind();
325 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Shutting down all ' . $this->getListInstance()->count() . ' tasks...');
327 // Remember all tasks that has been shutdown for removal
330 // Instance a visitor
331 $this->setVisitorInstance(ObjectFactory::createObjectByConfiguredName('shutdown_task_visitor_class'));
333 // Shutdown all tasks in once go
334 while ($this->getListInstance()->getIterator()->valid()) {
336 $currentTask = $this->getListInstance()->getIterator()->current();
338 // Output debug message
339 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Shutting down task ' . $currentTask['id'] . ' (taskInstance=' . $currentTask['task_instance']->__toString() . ') ...');
342 $currentTask['task_instance']->accept($this->getVisitorInstance());
344 // Remember this task
345 array_push($tasks, $currentTask);
347 // Advance to next one
348 $this->getListInstance()->getIterator()->next();
352 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Shutdown of all tasks completed.');
355 foreach ($tasks as $entry) {
356 $this->unregisterTask($entry);