Skip to content

Commit

Permalink
Scheduler now supports the removing/renaming of tasks better
Browse files Browse the repository at this point in the history
  • Loading branch information
badams committed Jun 14, 2016
1 parent e506429 commit 9a05232
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/Module.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
namespace webtoolsnz\scheduler;

use webtoolsnz\scheduler\models\SchedulerLog;
use Yii;
use yii\base\BootstrapInterface;
use webtoolsnz\scheduler\models\SchedulerTask;
use yii\helpers\ArrayHelper;

/**
* Class Module
Expand All @@ -29,7 +31,7 @@ class Module extends \yii\base\Module implements BootstrapInterface
*/
public function bootstrap($app)
{
Yii::setAlias('@scheduler', dirname(__DIR__).'/src');
Yii::setAlias('@scheduler', dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src');

if ($app instanceof \yii\console\Application && !isset($app->controllerMap[$this->id])) {
$app->controllerMap[$this->id] = [
Expand Down Expand Up @@ -64,12 +66,32 @@ public function getTasks()
if (preg_match('/^[a-zA-Z0-9_]*Task$/', $className)) {
$tasks[] = $this->loadTask($className);
}

}

$this->cleanTasks($tasks);

return $tasks;
}

/**
* Removes any records of tasks that no longer exist.
*
* @param Task[] $tasks
*/
public function cleanTasks($tasks)
{
$currentTasks = ArrayHelper::map($tasks, function ($task) {
return $task->getName();
}, 'description');

foreach (SchedulerTask::find()->indexBy('name')->all() as $name => $task) { /* @var SchedulerTask $task */
if (!array_key_exists($name, $currentTasks)) {
SchedulerLog::deleteAll(['scheduler_task_id' => $task->id]);
$task->delete();
}
}
}

/**
* Given the className of a task, it will return a new instance of that task.
* If the task doesn't exist, null will be returned.
Expand Down

0 comments on commit 9a05232

Please sign in to comment.