<?php
// Own namespace
-namespace CoreFramework\Handler\Task;
+namespace Org\Mxchange\CoreFramework\Handler\Task;
// Import framework stuff
-use CoreFramework\Factory\ObjectFactory;
-use CoreFramework\Handler\BaseHandler;
-use CoreFramework\Lists\Listable;
-use CoreFramework\Registry\Registerable;
+use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
+use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
+use Org\Mxchange\CoreFramework\Handler\BaseHandler;
+use Org\Mxchange\CoreFramework\Lists\Listable;
+use Org\Mxchange\CoreFramework\Registry\Registerable;
+use Org\Mxchange\CoreFramework\Task\Taskable;
+use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait;
+use Org\Mxchange\CoreFramework\Visitor\Visitable;
+use Org\Mxchange\CoreFramework\Visitor\Visitor;
/**
* A Task handler
*
* @author Roland Haeder <webmaster@shipsimu.org>
* @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
* @license GNU GPL 3.0 or any newer version
* @link http://www.shipsimu.org
*
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
+ // Load traits
+ use IteratorTrait;
+
// Exception constants
const EXCEPTION_TASK_IS_INVALID = 0xb00;
+ /**
+ * Visitor handler instance
+ */
+ private $visitorInstance = NULL;
+
+ /**
+ * Instance of the list
+ */
+ private $listInstance = NULL;
+
/**
* Protected constructor
*
$handlerInstance = new TaskHandler();
// Output debug message
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Initializing task handler.');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Initializing task handler.');
// Init the task list
$handlerInstance->setListInstance(ObjectFactory::createObjectByConfiguredName('task_list_class'));
$handlerInstance->registerTask('idle_loop', $taskInstance);
// Output debug message
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Task handler initialized.');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Task handler initialized.');
// Return the prepared instance
return $handlerInstance;
}
+ /**
+ * Setter for visitor instance
+ *
+ * @param $visitorInstance An instance of a Visitor class
+ * @return void
+ */
+ protected final function setVisitorInstance (Visitor $visitorInstance) {
+ $this->visitorInstance = $visitorInstance;
+ }
+
+ /**
+ * Getter for visitor instance
+ *
+ * @return $visitorInstance An instance of a Visitor class
+ */
+ protected final function getVisitorInstance () {
+ return $this->visitorInstance;
+ }
+
+ /**
+ * Setter for the list instance
+ *
+ * @param $listInstance A list of Listable
+ * @return void
+ */
+ protected final function setListInstance (Listable $listInstance) {
+ $this->listInstance = $listInstance;
+ }
+
+ /**
+ * Getter for the list instance
+ *
+ * @return $listInstance A list of Listable
+ */
+ protected final function getListInstance () {
+ return $this->listInstance;
+ }
+
/**
* Tries to execute the given task. If as task should not be started (yet)
* or the interval time (see task_interval_delay) is not yet reached the
*/
private function executeCurrentTask () {
// Update no task by default
- $updateTask = FALSE;
+ $updateTask = false;
// Is the current task valid?
if (!$this->getListInstance()->getIterator()->valid()) {
$currentTask = $this->getListInstance()->getIterator()->current();
// Is the task not yet started?
- if ($currentTask['task_started'] === FALSE) {
+ if ($currentTask['task_started'] === false) {
// Determine difference between current time and registration
$diff = ($this->getMilliTime() - $currentTask['task_registered']) * 1000;
// Should we start now?
if ($diff < $currentTask['task_startup_delay']) {
// Skip this silently
- //* 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']);
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Task ' . $currentTask['id'] . ' not started: diff=' . $diff . ',task_startup_delay=' . $currentTask['task_startup_delay']);
return;
} // END - if
// Launch the task and mark it as updated
- $currentTask['task_started'] = TRUE;
- $updateTask = TRUE;
+ $currentTask['task_started'] = true;
+ $updateTask = true;
// Debug message
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Task ' . $currentTask['id'] . ' started with startup_delay=' . $currentTask['task_startup_delay'] . 'ms');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Task ' . $currentTask['id'] . ' started with startup_delay=' . $currentTask['task_startup_delay'] . 'ms');
} // END - if
// Get time difference from interval delay
$diff = ($this->getMilliTime() - $currentTask['task_last_activity']) * 1000;
// Debug message
- //* 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']);
+ //* 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']);
// Is the interval delay reached?
if ((($diff < $currentTask['task_interval_delay']) && ($currentTask['task_max_runs'] == 0)) || (($currentTask['task_max_runs'] > 0) && ($currentTask['task_total_runs'] == $currentTask['task_max_runs']))) {
// Should we update the task from startup?
- if ($updateTask === TRUE) {
+ if ($updateTask === true) {
// Update the task before leaving
$this->updateTask($currentTask);
} // END - if
*/
private function unregisterTask (array $taskData) {
// Debug output
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Removing task ' . $taskData['id'] . ' from queue - CALLED!');
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Removing task ' . $taskData['id'] . ' from queue - CALLED!');
// Remove the entry
$this->getListInstance()->removeEntry('tasks', $taskData);
// Debug output
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Removing task ' . $taskData['id'] . ' from queue - EXIT!');
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Removing task ' . $taskData['id'] . ' from queue - EXIT!');
}
/**
* Registers a task with a task handler.
*
* @param $taskName A task name to register the task on
- * @param $taskInstance The instance we should register as a task
+ * @param $taskInstance The instance that should be registered as a task
* @return void
*/
public function registerTask ($taskName, Visitable $taskInstance) {
// Get interval delay
- $intervalDelay = $this->getConfigInstance()->getConfigEntry('task_' . $taskName . '_interval_delay');
- $startupDelay = $this->getConfigInstance()->getConfigEntry('task_' . $taskName . '_startup_delay');
+ $intervalDelay = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('task_' . $taskName . '_interval_delay');
+ $startupDelay = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('task_' . $taskName . '_startup_delay');
// If the task is 'idle_loop', a deplay of zero seconds is fine
assert($intervalDelay >= 0);
// Identifier for the generateHash() method
'id' => $taskName,
// Whether the task is started
- 'task_started' => FALSE,
+ 'task_started' => false,
// Whether the task is paused (not yet implemented)
- 'task_paused' => FALSE,
+ 'task_paused' => false,
// Whether the task can be paused (not yet implemented)
- 'task_pauseable' => TRUE,
+ 'task_pauseable' => true,
// Timestamp of registration
'task_registered' => $this->getMilliTime(),
// Last activity timestamp
// Interval time (delay) in milliseconds before this task is executed again
'task_interval_delay' => $intervalDelay,
// How often should this task run?
- 'task_max_runs' => $this->getConfigInstance()->getConfigEntry('task_' . $taskName . '_max_runs'),
+ 'task_max_runs' => FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('task_' . $taskName . '_max_runs'),
);
// Add the entry
$this->getListInstance()->addEntry('tasks', $taskEntry);
// Debug message
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Task registered: taskName=' . $taskName .
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Task registered: taskName=' . $taskName .
' (taskInstance=' . $taskInstance->__toString() . ')' .
', startupDelay=' . $taskEntry['task_startup_delay'] . 'ms' .
', intervalDelay=' . $taskEntry['task_interval_delay'] . 'ms' .
*/
public function hasTasksLeft () {
// Do we have tasks there?
- $tasksLeft = (($this->getListInstance() instanceof Listable) && ($this->getListInstance()->count() > 0));
+ $tasksLeft = ($this->getListInstance()->count() > 0);
// Return result
return $tasksLeft;
$this->getListInstance()->getIterator()->rewind();
// Debug message
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Shutting down all ' . $this->getListInstance()->count() . ' tasks...');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Shutting down all ' . $this->getListInstance()->count() . ' tasks...');
// Remember all tasks that has been shutdown for removal
- $tasks = array();
+ $tasks = [];
// Instance a visitor
$this->setVisitorInstance(ObjectFactory::createObjectByConfiguredName('shutdown_task_visitor_class'));
$currentTask = $this->getListInstance()->getIterator()->current();
// Output debug message
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Shutting down task ' . $currentTask['id'] . ' (taskInstance=' . $currentTask['task_instance']->__toString() . ') ...');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Shutting down task ' . $currentTask['id'] . ' (taskInstance=' . $currentTask['task_instance']->__toString() . ') ...');
// Shutdown the task
$currentTask['task_instance']->accept($this->getVisitorInstance());
} // END - while
// Debug message
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Shutdown of all tasks completed.');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('TASK-HANDLER: Shutdown of all tasks completed.');
// Remove all tasks
foreach ($tasks as $entry) {