From 5fc683932a0a80606eda827f81deb664ee5e55ac Mon Sep 17 00:00:00 2001 From: Bruce Aldridge Date: Tue, 23 Aug 2016 12:44:15 +1200 Subject: [PATCH] adding events and exceptions --- README.md | 29 +++++++++++++++++++++++++++++ src/Task.php | 10 ++++++++++ src/TaskRunner.php | 11 +++++++++++ src/console/SchedulerController.php | 24 ++++++++++++++++++++++-- src/events/SchedulerEvent.php | 17 +++++++++++++++++ src/events/TaskEvent.php | 14 ++++++++++++++ 6 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 src/events/SchedulerEvent.php create mode 100644 src/events/TaskEvent.php diff --git a/README.md b/README.md index bf6a7a5..d3be239 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,35 @@ In order to have your tasks run automatically simply setup a crontab like so */5 * * * * admin php /path/to/my/app/yii scheduler/run-all > /dev/null & ``` +### Events & Errors + +Events are thrown before and running individual tasks as well as at a global level for multiple tasks + +Task Level + +```php +Event::on(AlphabetTask::className(), AlphabetTask::EVENT_BEFORE_RUN, function ($event) { + Yii::trace($event->task->className . ' is about to run'); +}); +Event::on(AlphabetTask::className(), AlphabetTask::EVENT_AFTER_RUN, function ($event) { + Yii::trace($event->task->className . ' just ran '.($event->success ? 'successfully' : 'and failed')); +}); +``` + +or at the global level, to throw errors in `/yii` + +```php +$application->on(\webtoolsnz\scheduler\events\SchedulerEvent::EVENT_AFTER_RUN, function ($event) { + if (!$event->success) { + foreach($event->exceptions as $exception) { + throw $exception; + } + } +}); +``` + +You could throw the exceptions at the task level, however this will prevent further tasks from running. + ## License The MIT License (MIT). Please see [LICENSE](LICENSE) for more information. \ No newline at end of file diff --git a/src/Task.php b/src/Task.php index c8a6f58..da5d1b2 100644 --- a/src/Task.php +++ b/src/Task.php @@ -13,6 +13,16 @@ */ abstract class Task extends \yii\base\Component { + const EVENT_BEFORE_RUN = 'TaskBeforeRun'; + const EVENT_AFTER_RUN = 'TaskAfterRun'; + + /** + * Exception raised during run (if any) + * + * @var \Exception|null + */ + public $exception; + /** * Brief description of the task. * diff --git a/src/TaskRunner.php b/src/TaskRunner.php index 5c86256..769cc18 100644 --- a/src/TaskRunner.php +++ b/src/TaskRunner.php @@ -1,6 +1,7 @@ getTask(); if ($task->shouldRun($forceRun)) { + $event = new TaskEvent([ + 'task' => $task, + 'success' => true, + ]); + $this->trigger(Task::EVENT_BEFORE_RUN, $event); $task->start(); ob_start(); try { @@ -88,8 +95,12 @@ public function runTask($forceRun = false) $task->stop(); } catch (\Exception $e) { $this->running = false; + $task->exception = $e; + $event->exception = $e; + $event->success = false; $this->handleError($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine()); } + $this->trigger(Task::EVENT_AFTER_RUN, $event); } $task->getModel()->save(); $this->errorTearDown(); diff --git a/src/console/SchedulerController.php b/src/console/SchedulerController.php index 77c6f7c..702f5a0 100644 --- a/src/console/SchedulerController.php +++ b/src/console/SchedulerController.php @@ -2,6 +2,7 @@ namespace webtoolsnz\scheduler\console; +use webtoolsnz\scheduler\events\SchedulerEvent; use webtoolsnz\scheduler\models\base\SchedulerLog; use webtoolsnz\scheduler\models\SchedulerTask; use webtoolsnz\scheduler\Task; @@ -111,10 +112,19 @@ public function actionRunAll() $tasks = $this->getScheduler()->getTasks(); echo 'Running Tasks:'.PHP_EOL; - + $event = new SchedulerEvent([ + 'tasks' => $tasks, + 'success' => true, + ]); + $this->trigger(SchedulerEvent::EVENT_BEFORE_RUN, $event); foreach ($tasks as $task) { $this->runTask($task); + if ($task->exception) { + $event->success = false; + $event->exceptions[] = $task->exception; + } } + $this->trigger(SchedulerEvent::EVENT_AFTER_RUN, $event); echo PHP_EOL; } @@ -127,13 +137,23 @@ public function actionRun() throw new InvalidParamException('taskName must be specified'); } + /* @var Task $task */ $task = $this->getScheduler()->loadTask($this->taskName); if (!$task) { throw new InvalidParamException('Invalid taskName'); } - + $event = new SchedulerEvent([ + 'tasks' => [$task], + 'success' => true, + ]); + $this->trigger(SchedulerEvent::EVENT_BEFORE_RUN, $event); $this->runTask($task); + if ($task->exception) { + $event->success = false; + $event->exceptions = [$task->exception]; + } + $this->trigger(SchedulerEvent::EVENT_AFTER_RUN, $event); } /** diff --git a/src/events/SchedulerEvent.php b/src/events/SchedulerEvent.php new file mode 100644 index 0000000..1577a30 --- /dev/null +++ b/src/events/SchedulerEvent.php @@ -0,0 +1,17 @@ +