From 826bb5520175ad5c5cd19b41402f8eeb585c4def Mon Sep 17 00:00:00 2001 From: Eugene Terentev Date: Tue, 2 Feb 2016 13:14:41 +0200 Subject: [PATCH] beforeHandle and afterHandle events --- src/console/QueueBusController.php | 30 ++++++++++++++++++++++++++++++ src/console/QueueBusEvent.php | 22 ++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/console/QueueBusEvent.php diff --git a/src/console/QueueBusController.php b/src/console/QueueBusController.php index 78aed82..93d649c 100644 --- a/src/console/QueueBusController.php +++ b/src/console/QueueBusController.php @@ -20,6 +20,8 @@ */ class QueueBusController extends Controller { + const EVENT_BEFORE_HANDLE = 'beforeHandle'; + const EVENT_AFTER_HANDLE = 'afterHandle'; /** * @var mixed|QueueInterface */ @@ -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); @@ -97,6 +103,7 @@ protected function loop($queueName) } catch (\Exception $e) { $this->onError($job, $e); } + $this->afterHandle($job); unset($id, $job); } @@ -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 @@ -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 */ diff --git a/src/console/QueueBusEvent.php b/src/console/QueueBusEvent.php new file mode 100644 index 0000000..ca71232 --- /dev/null +++ b/src/console/QueueBusEvent.php @@ -0,0 +1,22 @@ + + */ +class QueueBusEvent extends Event +{ + /** + * @var array + */ + public $job; + /** + * @var bool + */ + public $isValid = true; +} \ No newline at end of file