3 namespace CoreFramework\Handler\Task;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Handler\BaseHandler;
8 use CoreFramework\Registry\Registerable;
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
33 // Exception constants
34 const EXCEPTION_TASK_IS_INVALID = 0xb00;
37 * Protected constructor
41 protected function __construct () {
42 // Call parent constructor
43 parent::__construct(__CLASS__);
46 $this->setHandlerName('task');
50 * Creates an instance of this class
52 * @return $handlerInstance An instance of a HandleableTask class
54 public static final function createTaskHandler () {
56 $handlerInstance = new TaskHandler();
58 // Output debug message
59 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Initializing task handler.');
62 $handlerInstance->setListInstance(ObjectFactory::createObjectByConfiguredName('task_list_class'));
64 // Get default instance
65 $handlerInstance->setIteratorInstance($handlerInstance->getListInstance()->getIterator());
67 // Init visitor instance for faster loop
68 $handlerInstance->setVisitorInstance(ObjectFactory::createObjectByConfiguredName('active_task_visitor_class'));
70 // Register the first (and generic) idle-loop task
71 $taskInstance = ObjectFactory::createObjectByConfiguredName('idle_task_class');
72 $handlerInstance->registerTask('idle_loop', $taskInstance);
74 // Output debug message
75 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Task handler initialized.');
77 // Return the prepared instance
78 return $handlerInstance;
82 * Tries to execute the given task. If as task should not be started (yet)
83 * or the interval time (see task_interval_delay) is not yet reached the
84 * task is quietly skipped.
87 * @throws InvalidTaskException If the current task is invalid
89 private function executeCurrentTask () {
90 // Update no task by default
93 // Is the current task valid?
94 if (!$this->getListInstance()->getIterator()->valid()) {
96 throw new InvalidTaskException($this, self::EXCEPTION_TASK_IS_INVALID);
100 $currentTask = $this->getListInstance()->getIterator()->current();
102 // Is the task not yet started?
103 if ($currentTask['task_started'] === FALSE) {
104 // Determine difference between current time and registration
105 $diff = ($this->getMilliTime() - $currentTask['task_registered']) * 1000;
107 // Should we start now?
108 if ($diff < $currentTask['task_startup_delay']) {
109 // Skip this silently
110 //* 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']);
114 // Launch the task and mark it as updated
115 $currentTask['task_started'] = TRUE;
119 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Task ' . $currentTask['id'] . ' started with startup_delay=' . $currentTask['task_startup_delay'] . 'ms');
122 // Get time difference from interval delay
123 $diff = ($this->getMilliTime() - $currentTask['task_last_activity']) * 1000;
126 //* 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']);
128 // Is the interval delay reached?
129 if ((($diff < $currentTask['task_interval_delay']) && ($currentTask['task_max_runs'] == 0)) || (($currentTask['task_max_runs'] > 0) && ($currentTask['task_total_runs'] == $currentTask['task_max_runs']))) {
130 // Should we update the task from startup?
131 if ($updateTask === TRUE) {
132 // Update the task before leaving
133 $this->updateTask($currentTask);
136 // Skip this silently
141 $currentTask['task_last_activity'] = $this->getMilliTime();
144 $currentTask['task_total_runs']++;
147 $this->updateTask($currentTask);
150 // @TODO Messurement can be added around this call
151 $currentTask['task_instance']->accept($this->getVisitorInstance());
155 * Updates given task by updating the underlaying list
157 * @param $taskEntry An array with a task
160 private function updateTask (array $taskEntry) {
161 // Get the key from current iteration
162 $key = $this->getListInstance()->getIterator()->key();
164 // Get the hash from key
165 $hash = $this->getListInstance()->getHash($key);
168 $this->getListInstance()->updateCurrentEntryByHash($hash, $taskEntry);
172 * Unregisters the given task
174 * @param $taskData Data of the task
177 private function unregisterTask (array $taskData) {
179 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Removing task ' . $taskData['id'] . ' from queue - CALLED!');
182 $this->getListInstance()->removeEntry('tasks', $taskData);
185 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Removing task ' . $taskData['id'] . ' from queue - EXIT!');
189 * Searches a task by given instance
191 * @param $taskInstanc An instanceof a Taskable class
192 * @return $taskName Name of the task as used while registration
194 public function searchTask (Taskable $taskInstance) {
195 // Default is an empty (not found) task name
199 $taskList = $this->getListInstance()->getArrayFromList('tasks');
201 // Search all instances
202 foreach ($taskList as $currentTask) {
203 // Does it match given task instance?
204 if ($currentTask['task_instance']->equals($taskInstance)) {
206 $taskName = $currentTask['id'];
218 * Registers a task with a task handler.
220 * @param $taskName A task name to register the task on
221 * @param $taskInstance The instance we should register as a task
224 public function registerTask ($taskName, Visitable $taskInstance) {
225 // Get interval delay
226 $intervalDelay = $this->getConfigInstance()->getConfigEntry('task_' . $taskName . '_interval_delay');
227 $startupDelay = $this->getConfigInstance()->getConfigEntry('task_' . $taskName . '_startup_delay');
229 // If the task is 'idle_loop', a deplay of zero seconds is fine
230 assert($intervalDelay >= 0);
231 assert(($taskName === 'idle_loop') || (($taskName != 'idle_loop') && ($intervalDelay > 0)));
232 assert(($taskName === 'idle_loop') || (($taskName != 'idle_loop') && ($startupDelay > 0)));
236 // Identifier for the generateHash() method
238 // Whether the task is started
239 'task_started' => FALSE,
240 // Whether the task is paused (not yet implemented)
241 'task_paused' => FALSE,
242 // Whether the task can be paused (not yet implemented)
243 'task_pauseable' => TRUE,
244 // Timestamp of registration
245 'task_registered' => $this->getMilliTime(),
246 // Last activity timestamp
247 'task_last_activity' => 0,
248 // Total runs of this task
249 'task_total_runs' => 0,
250 // Task instance itself
251 'task_instance' => $taskInstance,
252 // Startup delay in milliseconds
253 'task_startup_delay' => $startupDelay,
254 // Interval time (delay) in milliseconds before this task is executed again
255 'task_interval_delay' => $intervalDelay,
256 // How often should this task run?
257 'task_max_runs' => $this->getConfigInstance()->getConfigEntry('task_' . $taskName . '_max_runs'),
261 $this->getListInstance()->addEntry('tasks', $taskEntry);
264 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Task registered: taskName=' . $taskName .
265 ' (taskInstance=' . $taskInstance->__toString() . ')' .
266 ', startupDelay=' . $taskEntry['task_startup_delay'] . 'ms' .
267 ', intervalDelay=' . $taskEntry['task_interval_delay'] . 'ms' .
268 ', maxRuns=' . $taskEntry['task_max_runs'] . ' ...'
273 * Checks whether tasks are left including idle task
275 * @return $tasksLeft Whether there are tasks left to handle
277 public function hasTasksLeft () {
278 // Do we have tasks there?
279 $tasksLeft = (($this->getListInstance() instanceof Listable) && ($this->getListInstance()->count() > 0));
286 * Handles all tasks by checking if they should startup or if it is their
287 * turn to run. You should use this method in a while() loop in conjuntion
288 * with hasTasksLeft() so you can e.g. shutdown by adding a ShutdownTask
289 * which will attempt to remove all tasks from the task handler.
293 public function handleTasks () {
295 if (!$this->getListInstance()->getIterator()->valid()) {
296 // Rewind to the beginning for next loop
297 $this->getListInstance()->getIterator()->rewind();
300 // Try to execute the task
301 $this->executeCurrentTask();
304 $this->getListInstance()->getIterator()->next();
308 * Shuts down all tasks and the task handler itself. This method should be
309 * called from a corresponding filter class.
313 public function doShutdown () {
314 // Always rewind to the beginning for next loop
315 $this->getListInstance()->getIterator()->rewind();
318 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Shutting down all ' . $this->getListInstance()->count() . ' tasks...');
320 // Remember all tasks that has been shutdown for removal
323 // Instance a visitor
324 $this->setVisitorInstance(ObjectFactory::createObjectByConfiguredName('shutdown_task_visitor_class'));
326 // Shutdown all tasks in once go
327 while ($this->getListInstance()->getIterator()->valid()) {
329 $currentTask = $this->getListInstance()->getIterator()->current();
331 // Output debug message
332 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Shutting down task ' . $currentTask['id'] . ' (taskInstance=' . $currentTask['task_instance']->__toString() . ') ...');
335 $currentTask['task_instance']->accept($this->getVisitorInstance());
337 // Remember this task
338 array_push($tasks, $currentTask);
340 // Advance to next one
341 $this->getListInstance()->getIterator()->next();
345 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Shutdown of all tasks completed.');
348 foreach ($tasks as $entry) {
349 $this->unregisterTask($entry);