Skip to content

Commit

Permalink
beforeHandle and afterHandle events
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Terentev committed Feb 2, 2016
1 parent e31ea65 commit 826bb55
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/console/QueueBusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
class QueueBusController extends Controller
{
const EVENT_BEFORE_HANDLE = 'beforeHandle';
const EVENT_AFTER_HANDLE = 'afterHandle';
/**
* @var mixed|QueueInterface
*/
Expand Down Expand Up @@ -83,6 +85,10 @@ protected function loop($queueName)
$id = ArrayHelper::getValue($job, 'id');
Console::output("New job ID#{$id}");

if(!$this->beforeHandle($job)) {
continue;
};

try {
if ($this->forceDelete) {
$this->delete($job);
Expand All @@ -97,6 +103,7 @@ protected function loop($queueName)
} catch (\Exception $e) {
$this->onError($job, $e);
}
$this->afterHandle($job);
unset($id, $job);
}

Expand All @@ -112,6 +119,19 @@ protected function loop($queueName)
}
}

/**
* @param $job
* @return mixed
*/
protected function beforeHandle($job)
{
$event = new QueueBusEvent([
'job' => $job
]);
$this->trigger(self::EVENT_BEFORE_HANDLE, $event);
return $event->isValid;
}

/**
* @param $job
* @return mixed|string
Expand All @@ -130,6 +150,16 @@ protected function handle($job)
$this->delete($job);
}

/**
* @param $job
*/
protected function afterHandle($job)
{
$this->trigger(self::EVENT_AFTER_HANDLE, new QueueBusEvent([
'job' => $job
]));
}

/**
* @param $job
*/
Expand Down
22 changes: 22 additions & 0 deletions src/console/QueueBusEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace trntv\bus\console;
use yii\base\Event;


/**
* Class QueueBusEvent
* @package trntv\bus\console
* @author Eugene Terentev <eugene@terentev.net>
*/
class QueueBusEvent extends Event
{
/**
* @var array
*/
public $job;
/**
* @var bool
*/
public $isValid = true;
}

0 comments on commit 826bb55

Please sign in to comment.