]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/handler/tasks/class_TaskHandler.php
Some rewriting, TODOs.txt updated:
[hub.git] / application / hub / main / handler / tasks / class_TaskHandler.php
index b413ab7bab9c20e4bab2f86fd1ea208a0bb7224b..c90e6c7723c31513418ea91e08e211994e3a2dd7 100644 (file)
@@ -25,11 +25,6 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
        // Exception constants
        const EXCEPTION_TASK_IS_INVALID = 0xb00;
 
-       /**
-        * Visitor instance for all tasks while they are active
-        */
-       private $visitorInstance = NULL;
-
        /**
         * Protected constructor
         *
@@ -41,15 +36,6 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
 
                // Set handler name
                $this->setHandlerName('task');
-
-               // Init the task list
-               $this->setListInstance(ObjectFactory::createObjectByConfiguredName('task_list_class'));
-
-               // Get default instance
-               $this->setIteratorInstance($this->getListInstance()->getIterator());
-
-               // Init visitor instance for faster loop
-               $this->visitorInstance = ObjectFactory::createObjectByConfiguredName('active_task_visitor_class');
        }
 
        /**
@@ -62,12 +48,24 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
                $handlerInstance = new TaskHandler();
 
                // Output debug message
-               $handlerInstance->debugOutput('TASK-HANDLER: Task handler initialized.');
+               $handlerInstance->debugOutput('TASK-HANDLER: Initializing task handler.');
+
+               // Init the task list
+               $handlerInstance->setListInstance(ObjectFactory::createObjectByConfiguredName('task_list_class'));
+
+               // Get default instance
+               $handlerInstance->setIteratorInstance($handlerInstance->getListInstance()->getIterator());
+
+               // Init visitor instance for faster loop
+               $handlerInstance->setVisitorInstance(ObjectFactory::createObjectByConfiguredName('active_task_visitor_class'));
 
                // Register the first (and generic) idle-loop task
                $taskInstance = ObjectFactory::createObjectByConfiguredName('idle_task_class');
                $handlerInstance->registerTask('idle_loop', $taskInstance);
 
+               // Output debug message
+               $handlerInstance->debugOutput('TASK-HANDLER: Task handler initialized.');
+
                // Return the prepared instance
                return $handlerInstance;
        }
@@ -85,13 +83,13 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
                $updateTask = false;
 
                // Is the current task valid?
-               if (!$this->getIteratorInstance()->valid()) {
+               if (!$this->getListInstance()->getIterator()->valid()) {
                        // Not valid!
                        throw new InvalidTaskException($this, self::EXCEPTION_TASK_IS_INVALID);
                } // END - if
 
                // Get current task
-               $currentTask = $this->getIteratorInstance()->current();
+               $currentTask = $this->getListInstance()->getIterator()->current();
 
                // Is the task not yet started?
                if ($currentTask['task_started'] === false) {
@@ -138,7 +136,7 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
 
                // And visit/run it
                // @TODO Messurement can be added around this call
-               $currentTask['task_instance']->accept($this->visitorInstance);
+               $currentTask['task_instance']->accept($this->getVisitorInstance());
        }
 
        /**
@@ -149,7 +147,7 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
         */
        private function updateTask (array $taskEntry) {
                // Get the key from current iteration
-               $key = $this->getIteratorInstance()->key();
+               $key = $this->getListInstance()->getIterator()->key();
 
                // Get the hash from key
                $hash = $this->getListInstance()->getHash($key);
@@ -244,16 +242,16 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
         */
        public function handleTasks () {
                // Should we rewind?
-               if (!$this->getIteratorInstance()->valid()) {
+               if (!$this->getListInstance()->getIterator()->valid()) {
                        // Rewind to the beginning for next loop
-                       $this->getIteratorInstance()->rewind();
+                       $this->getListInstance()->getIterator()->rewind();
                } // END - if
 
                // Try to execute the task
                $this->executeCurrentTask();
 
                // Go to next entry
-               $this->getIteratorInstance()->next();
+               $this->getListInstance()->getIterator()->next();
        }
 
        /**
@@ -264,7 +262,7 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
         */
        public function doShutdown () {
                // Always rewind to the beginning for next loop
-               $this->getIteratorInstance()->rewind();
+               $this->getListInstance()->getIterator()->rewind();
 
                // Debug message
                $this->debugOutput('TASK-HANDLER: Shutting down all ' . $this->getListInstance()->count() . ' tasks...');
@@ -273,24 +271,24 @@ class TaskHandler extends BaseHandler implements Registerable, HandleableTask {
                $tasks = array();
 
                // Instance a visitor
-               $this->visitorInstance = ObjectFactory::createObjectByConfiguredName('shutdown_task_visitor_class');
+               $this->setVisitorInstance(ObjectFactory::createObjectByConfiguredName('shutdown_task_visitor_class'));
 
                // Shutdown all tasks in once go
-               while ($this->getIteratorInstance()->valid()) {
+               while ($this->getListInstance()->getIterator()->valid()) {
                        // Get current entry
-                       $currentTask = $this->getIteratorInstance()->current();
+                       $currentTask = $this->getListInstance()->getIterator()->current();
 
                        // Output debug message
                        $this->debugOutput('TASK-HANDLER: Shutting down task ' . $currentTask['id'] . ' (taskInstance=' . $currentTask['task_instance']->__toString() . ') ...');
 
                        // Shutdown the task
-                       $currentTask['task_instance']->accept($this->visitorInstance);
+                       $currentTask['task_instance']->accept($this->getVisitorInstance());
 
                        // Remember this task
                        $tasks[] = $currentTask;
 
                        // Advance to next one
-                       $this->getIteratorInstance()->next();
+                       $this->getListInstance()->getIterator()->next();
                } // END - while
 
                // Debug message