Skip to content

Commit

Permalink
Refactored Error handler slightly added TASK_FAILURE event (#12)
Browse files Browse the repository at this point in the history
* Refactored Error handler slightly added TASK_FAILURE event
  • Loading branch information
badams authored Oct 22, 2019
1 parent c7da545 commit cf7fbb2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
8 changes: 7 additions & 1 deletion src/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace webtoolsnz\scheduler;

use yii\base\ErrorException;

/**
* Class ErrorHandler
* @package webtoolsnz\scheduler
*/
class ErrorHandler extends \yii\console\ErrorHandler
{
public $memoryReserveSize = 2097152;

/**
* @var TaskRunner
*/
Expand All @@ -25,8 +29,10 @@ public function register()
public function schedulerShutdownHandler()
{
$error = error_get_last();

if ($this->taskRunner && E_ERROR == $error['type']) {
$this->taskRunner->handleError($error['type'], $error['message'], $error['file'], $error['line']);
$exception = new ErrorException($error['message'], $error['type'], $error['type'], $error['file'], $error['line']);
$this->taskRunner->handleError($exception);
}

// Allow yiis error handler to take over and handle logging
Expand Down
1 change: 1 addition & 0 deletions src/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ abstract class Task extends \yii\base\Component
{
const EVENT_BEFORE_RUN = 'TaskBeforeRun';
const EVENT_AFTER_RUN = 'TaskAfterRun';
const EVENT_FAILURE = ' TaskFailure';

/**
* @var bool create a database lock to ensure the task only runs once
Expand Down
28 changes: 18 additions & 10 deletions src/TaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use webtoolsnz\scheduler\events\TaskEvent;
use webtoolsnz\scheduler\models\SchedulerLog;
use webtoolsnz\scheduler\models\SchedulerTask;
use yii\base\ErrorException;
use yii\base\Exception;

/**
* Class TaskRunner
Expand Down Expand Up @@ -100,7 +102,7 @@ public function runTask($forceRun = false)
$task->exception = $e;
$event->exception = $e;
$event->success = false;
$this->handleError($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
$this->handleError($e);
}
$this->trigger(Task::EVENT_AFTER_RUN, $event);
}
Expand All @@ -122,17 +124,16 @@ public function shutdownHandler()
}

/**
* @param $code
* @param $message
* @param $file
* @param $lineNumber
* @param \Exception|ErrorException|Exception $exception
*/
public function handleError($code, $message, $file, $lineNumber)
public function handleError(\Exception $exception)
{
echo sprintf('ERROR: %s %s', $code, PHP_EOL);
echo sprintf('ERROR FILE: %s %s', $file, PHP_EOL);
echo sprintf('ERROR LINE: %s %s', $lineNumber, PHP_EOL);
echo sprintf('ERROR MESSAGE: %s %s', $message, PHP_EOL);
echo sprintf(
"%s: %s \n\n Stack Trace: \n %s",
$exception->getName(),
$exception->getMessage(),
$exception->getTraceAsString()
);

// if the failed task was mid transaction, rollback so we can save.
if (null !== ($tx = \Yii::$app->db->getTransaction())) {
Expand All @@ -146,6 +147,13 @@ public function handleError($code, $message, $file, $lineNumber)
$this->log($output);
$this->getTask()->getModel()->status_id = SchedulerTask::STATUS_ERROR;
$this->getTask()->stop();

$this->getTask()->trigger(Task::EVENT_FAILURE, new TaskEvent([
'task' => $this->getTask(),
'output' => $output,
'success' => false,
'exception' => $exception,
]));
}

/**
Expand Down
21 changes: 18 additions & 3 deletions src/events/TaskEvent.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
<?php


namespace webtoolsnz\scheduler\events;

use webtoolsnz\scheduler\Task;
use yii\base\Event;


class TaskEvent extends Event
{
/**
* @var Task
*/
public $task;

/**
* @var \Exception
*/
public $exception;

/**
* @var bool
*/
public $success;

/**
* @var string
*/
public $output;

public $cancel = false;
}
}

0 comments on commit cf7fbb2

Please sign in to comment.