Skip to content

Commit d8e30fb

Browse files
authored
Merge pull request #26 from ramasofficial/add-extensions-support
Add extensions support
2 parents f037278 + 67f22aa commit d8e30fb

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/EnqueueServiceProvider.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313

1414
class EnqueueServiceProvider extends ServiceProvider
1515
{
16+
/**
17+
* @var array
18+
*/
19+
protected $extensions = [];
20+
1621
public function boot()
1722
{
1823
$this->bootInteropQueueDriver();
@@ -36,7 +41,7 @@ private function registerClient()
3641
$this->app->singleton(SimpleClient::class, function() {
3742
/** @var \Illuminate\Config\Repository $config */
3843
$config = $this->app['config'];
39-
44+
4045
return new SimpleClient($config->get('enqueue.client'));
4146
});
4247

@@ -64,14 +69,14 @@ private function bootInteropQueueDriver()
6469
});
6570

6671
$this->app->extend('queue.worker', function ($worker, $app) {
67-
return new Worker(
72+
return (new Worker(
6873
$app['queue'],
6974
$app['events'],
7075
$app[ExceptionHandler::class],
7176
function () use ($app) {
7277
return $app->isDownForMaintenance();
7378
}
74-
);
79+
))->setExtensions($this->extensions);
7580
});
7681
}
7782
}

src/Worker.php

+23-4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class Worker extends \Illuminate\Queue\Worker implements
4040

4141
protected $job;
4242

43+
protected $extensions = [];
44+
4345
public function daemon($connectionName, $queueNames, WorkerOptions $options)
4446
{
4547
$this->connectionName = $connectionName;
@@ -56,7 +58,9 @@ public function daemon($connectionName, $queueNames, WorkerOptions $options)
5658
}
5759

5860
$context = $this->queue->getQueueInteropContext();
59-
$queueConsumer = new QueueConsumer($context, new ChainExtension([$this]));
61+
$queueConsumer = new QueueConsumer($context, new ChainExtension(
62+
$this->getAllExtensions([$this])
63+
));
6064
foreach (explode(',', $queueNames) as $queueName) {
6165
$queueConsumer->bindCallback($queueName, function() {
6266
$this->runJob($this->job, $this->connectionName, $this->options);
@@ -85,10 +89,10 @@ public function runNextJob($connectionName, $queueNames, WorkerOptions $options)
8589

8690
$context = $this->queue->getQueueInteropContext();
8791

88-
$queueConsumer = new QueueConsumer($context, new ChainExtension([
92+
$queueConsumer = new QueueConsumer($context, new ChainExtension($this->getAllExtensions([
8993
$this,
9094
new LimitConsumedMessagesExtension(1),
91-
]));
95+
])));
9296

9397
foreach (explode(',', $queueNames) as $queueName) {
9498
$queueConsumer->bindCallback($queueName, function() {
@@ -163,5 +167,20 @@ public function stop($status = 0)
163167

164168
parent::stop($status);
165169
}
166-
}
167170

171+
public function setExtensions(array $extensions): self
172+
{
173+
$this->extensions = $extensions;
174+
175+
return $this;
176+
}
177+
178+
protected function getAllExtensions(array $array): array
179+
{
180+
foreach ($this->extensions as $extension) {
181+
$array[] = $extension;
182+
}
183+
184+
return $array;
185+
}
186+
}

0 commit comments

Comments
 (0)