Skip to content

Commit 70170b8

Browse files
committed
register worker callback only once, move to constructor
1 parent ed090e4 commit 70170b8

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

GearmanConsumer.php

+13-6
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,23 @@ class GearmanConsumer implements Consumer
2525
*/
2626
private $context;
2727

28+
/**
29+
* Message content.
30+
*
31+
* @var mixed
32+
*/
33+
private $message;
34+
2835
public function __construct(GearmanContext $context, GearmanDestination $destination)
2936
{
3037
$this->context = $context;
3138
$this->destination = $destination;
3239

3340
$this->worker = $context->createWorker();
41+
42+
$this->worker->addFunction($this->destination->getName(), function (\GearmanJob $job) {
43+
$this->message = GearmanMessage::jsonUnserialize($job->workload());
44+
});
3445
}
3546

3647
/**
@@ -53,18 +64,14 @@ public function receive(int $timeout = 0): ?Message
5364
$this->worker->setTimeout($timeout);
5465

5566
try {
56-
$message = null;
57-
58-
$this->worker->addFunction($this->destination->getName(), function (\GearmanJob $job) use (&$message) {
59-
$message = GearmanMessage::jsonUnserialize($job->workload());
60-
});
67+
$this->message = null;
6168

6269
$this->worker->work();
6370
} finally {
6471
restore_error_handler();
6572
}
6673

67-
return $message;
74+
return $this->message;
6875
}
6976

7077
/**

0 commit comments

Comments
 (0)