Task manager update to run individual tasks

This commit is contained in:
Duncan Sommerville 2015-06-30 11:46:58 -04:00
parent 7dd69da614
commit 04fe265538
2 changed files with 30 additions and 10 deletions

View File

@ -130,9 +130,11 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
protected function _initTasks() { protected function _initTasks() {
/* We need to wrap this here so that we aren't checking when we're running the unit test suite /* 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) { if (getenv("AIRTIME_UNIT_TEST") != 1) {
//This will do the upgrade too if it's needed... //This will do the upgrade too if it's needed...
TaskManager::getInstance()->runTasks(); $taskManager->runTasks();
} }
} }

View File

@ -2,15 +2,19 @@
/** /**
* Class TaskManager * 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 { 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 = [ protected $_taskList = [
AirtimeTask::UPGRADE, // Always run the upgrade first AirtimeTask::UPGRADE => false,
AirtimeTask::CELERY AirtimeTask::CELERY => false,
]; ];
/** /**
@ -19,8 +23,7 @@ final class TaskManager {
protected static $_instance; protected static $_instance;
/** /**
* @var int TASK_INTERVAL_SECONDS how often, in seconds, to run the TaskManager tasks, * @var int TASK_INTERVAL_SECONDS how often, in seconds, to run the TaskManager tasks
* if they need to be run
*/ */
const TASK_INTERVAL_SECONDS = 30; const TASK_INTERVAL_SECONDS = 30;
@ -47,6 +50,22 @@ final class TaskManager {
return self::$_instance; 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. * Run all tasks that need to be run.
* *
@ -81,10 +100,9 @@ final class TaskManager {
// better to be silent here to avoid log bloat // better to be silent here to avoid log bloat
return; return;
} }
foreach ($this->_taskList as $task) { foreach ($this->_taskList as $task => $hasTaskRun) {
$task = TaskFactory::getTask($task); if (!$hasTaskRun) {
if ($task && $task->shouldBeRun()) { $this->runTask($task);
$task->run();
} }
} }
} }