Skip to content

Commit

Permalink
Merge pull request #1157 from cturbelin/gearman-worker-register
Browse files Browse the repository at this point in the history
register worker callback only once, move to constructor
  • Loading branch information
makasim authored Apr 3, 2021
2 parents b32370d + 6d2e586 commit 856839e
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions pkg/gearman/GearmanConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
}

/**
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 856839e

Please sign in to comment.