Skip to content

Commit

Permalink
adding events and exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
brucealdridge committed Aug 23, 2016
1 parent 88da5de commit 5fc6839
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 2 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
10 changes: 10 additions & 0 deletions src/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
11 changes: 11 additions & 0 deletions src/TaskRunner.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace webtoolsnz\scheduler;

use webtoolsnz\scheduler\events\TaskEvent;
use webtoolsnz\scheduler\models\SchedulerLog;
use webtoolsnz\scheduler\models\SchedulerTask;

Expand All @@ -12,6 +13,7 @@
*/
class TaskRunner extends \yii\base\Component
{

/**
* Indicates whether an error occured during the executing of the task.
* @var bool
Expand Down Expand Up @@ -76,6 +78,11 @@ public function runTask($forceRun = false)
$task = $this->getTask();

if ($task->shouldRun($forceRun)) {
$event = new TaskEvent([
'task' => $task,
'success' => true,
]);
$this->trigger(Task::EVENT_BEFORE_RUN, $event);
$task->start();
ob_start();
try {
Expand All @@ -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();
Expand Down
24 changes: 22 additions & 2 deletions src/console/SchedulerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/events/SchedulerEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php


namespace webtoolsnz\scheduler\events;

use yii\base\Event;


class SchedulerEvent extends Event
{
const EVENT_BEFORE_RUN = 'SchedulerBeforeRun';
const EVENT_AFTER_RUN = 'SchedulerAfterRun';

public $tasks;
public $exceptions;
public $success;
}
14 changes: 14 additions & 0 deletions src/events/TaskEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php


namespace webtoolsnz\scheduler\events;

use yii\base\Event;


class TaskEvent extends Event
{
public $task;
public $exception;
public $success;
}

0 comments on commit 5fc6839

Please sign in to comment.