From 6d2e58645977002136a0ff05df4c3a4f020f5821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Turbelin?= Date: Fri, 2 Apr 2021 19:56:31 +0200 Subject: [PATCH] register worker callback only once, move to constructor --- pkg/gearman/GearmanConsumer.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/gearman/GearmanConsumer.php b/pkg/gearman/GearmanConsumer.php index 68f15e045..7c1dea0a1 100644 --- a/pkg/gearman/GearmanConsumer.php +++ b/pkg/gearman/GearmanConsumer.php @@ -25,12 +25,23 @@ class GearmanConsumer implements Consumer */ private $context; + /** + * Message content. + * + * @var mixed + */ + private $message; + public function __construct(GearmanContext $context, GearmanDestination $destination) { $this->context = $context; $this->destination = $destination; $this->worker = $context->createWorker(); + + $this->worker->addFunction($this->destination->getName(), function (\GearmanJob $job) { + $this->message = GearmanMessage::jsonUnserialize($job->workload()); + }); } /** @@ -53,18 +64,14 @@ public function receive(int $timeout = 0): ?Message $this->worker->setTimeout($timeout); try { - $message = null; - - $this->worker->addFunction($this->destination->getName(), function (\GearmanJob $job) use (&$message) { - $message = GearmanMessage::jsonUnserialize($job->workload()); - }); + $this->message = null; $this->worker->work(); } finally { restore_error_handler(); } - return $message; + return $this->message; } /**