Skip to content

Commit 361f3fc

Browse files
authored
Process queue jobs in background (#57648)
1 parent a48282a commit 361f3fc

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Illuminate\Queue;
4+
5+
use Illuminate\Contracts\Queue\Job;
6+
use Illuminate\Support\Facades\Concurrency;
7+
8+
class BackgroundQueue extends SyncQueue
9+
{
10+
/**
11+
* Push a new job onto the queue.
12+
*
13+
* @param string $job
14+
* @param mixed $data
15+
* @param string|null $queue
16+
* @return mixed
17+
*
18+
* @throws \Throwable
19+
*/
20+
public function push($job, $data = '', $queue = null)
21+
{
22+
Concurrency::driver('process')->defer(
23+
fn () => \Illuminate\Support\Facades\Queue::connection('sync')->push($job, $data, $queue)
24+
);
25+
}
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Illuminate\Queue\Connectors;
4+
5+
use Illuminate\Queue\BackgroundQueue;
6+
7+
class BackgroundConnector implements ConnectorInterface
8+
{
9+
/**
10+
* Establish a queue connection.
11+
*
12+
* @return \Illuminate\Contracts\Queue\Queue
13+
*/
14+
public function connect(array $config)
15+
{
16+
return new BackgroundQueue($config['after_commit'] ?? null);
17+
}
18+
}

src/Illuminate/Queue/QueueServiceProvider.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Contracts\Debug\ExceptionHandler;
77
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
88
use Illuminate\Contracts\Support\DeferrableProvider;
9+
use Illuminate\Queue\Connectors\BackgroundConnector;
910
use Illuminate\Queue\Connectors\BeanstalkdConnector;
1011
use Illuminate\Queue\Connectors\DatabaseConnector;
1112
use Illuminate\Queue\Connectors\DeferredConnector;
@@ -105,7 +106,7 @@ protected function registerConnection()
105106
*/
106107
public function registerConnectors($manager)
107108
{
108-
foreach (['Null', 'Sync', 'Deferred', 'Failover', 'Database', 'Redis', 'Beanstalkd', 'Sqs'] as $connector) {
109+
foreach (['Null', 'Sync', 'Deferred', 'Background', 'Failover', 'Database', 'Redis', 'Beanstalkd', 'Sqs'] as $connector) {
109110
$this->{"register{$connector}Connector"}($manager);
110111
}
111112
}
@@ -149,6 +150,19 @@ protected function registerDeferredConnector($manager)
149150
});
150151
}
151152

153+
/**
154+
* Register the Background queue connector.
155+
*
156+
* @param \Illuminate\Queue\QueueManager $manager
157+
* @return void
158+
*/
159+
protected function registerBackgroundConnector($manager)
160+
{
161+
$manager->addConnector('background', function () {
162+
return new BackgroundConnector;
163+
});
164+
}
165+
152166
/**
153167
* Register the Failover queue connector.
154168
*

0 commit comments

Comments
 (0)