From 04fe265538d5934d92f29bccab14dac8ef062ef2 Mon Sep 17 00:00:00 2001 From: Duncan Sommerville Date: Tue, 30 Jun 2015 11:46:58 -0400 Subject: [PATCH] Task manager update to run individual tasks --- airtime_mvc/application/Bootstrap.php | 4 ++- .../application/common/TaskManager.php | 36 ++++++++++++++----- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/airtime_mvc/application/Bootstrap.php b/airtime_mvc/application/Bootstrap.php index 0c1911d55..9ea77cdf9 100644 --- a/airtime_mvc/application/Bootstrap.php +++ b/airtime_mvc/application/Bootstrap.php @@ -130,9 +130,11 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap protected function _initTasks() { /* We need to wrap this here so that we aren't checking when we're running the unit test suite */ + $taskManager = TaskManager::getInstance(); + $taskManager->runTask(AirtimeTask::UPGRADE); // Run the upgrade on each request (if it needs to be run) if (getenv("AIRTIME_UNIT_TEST") != 1) { //This will do the upgrade too if it's needed... - TaskManager::getInstance()->runTasks(); + $taskManager->runTasks(); } } diff --git a/airtime_mvc/application/common/TaskManager.php b/airtime_mvc/application/common/TaskManager.php index aa250f294..c1687defa 100644 --- a/airtime_mvc/application/common/TaskManager.php +++ b/airtime_mvc/application/common/TaskManager.php @@ -2,15 +2,19 @@ /** * Class TaskManager + * + * When adding a new task, the new AirtimeTask class will need to be added to the internal task list, + * as an ENUM value to the AirtimeTask interface, and as a case in the TaskFactory. */ final class TaskManager { /** - * @var array tasks to be run + * @var array tasks to be run. Maps task names to a boolean value denoting + * whether the task has been checked/run */ protected $_taskList = [ - AirtimeTask::UPGRADE, // Always run the upgrade first - AirtimeTask::CELERY + AirtimeTask::UPGRADE => false, + AirtimeTask::CELERY => false, ]; /** @@ -19,8 +23,7 @@ final class TaskManager { protected static $_instance; /** - * @var int TASK_INTERVAL_SECONDS how often, in seconds, to run the TaskManager tasks, - * if they need to be run + * @var int TASK_INTERVAL_SECONDS how often, in seconds, to run the TaskManager tasks */ const TASK_INTERVAL_SECONDS = 30; @@ -47,6 +50,22 @@ final class TaskManager { return self::$_instance; } + /** + * Run a single task. + * + * @param string $taskName the ENUM name of the task to be run + */ + public function runTask($taskName) { + $task = TaskFactory::getTask($taskName); + if ($task && $task->shouldBeRun()) { + $task->run(); + } + $this->_taskList[$taskName] = true; // Mark that the task has been checked/run. + // This is important for prioritized tasks that + // we need to run on every request (such as the + // schema check/upgrade) + } + /** * Run all tasks that need to be run. * @@ -81,10 +100,9 @@ final class TaskManager { // better to be silent here to avoid log bloat return; } - foreach ($this->_taskList as $task) { - $task = TaskFactory::getTask($task); - if ($task && $task->shouldBeRun()) { - $task->run(); + foreach ($this->_taskList as $task => $hasTaskRun) { + if (!$hasTaskRun) { + $this->runTask($task); } } }