Skip to content
This repository was archived by the owner on Nov 5, 2025. It is now read-only.

Commit 20c92d5

Browse files
authored
Use Laravel Concurrency with SyncDriver instead of DatabaseDriver (#103)
* Update package requirements for Laravel Concurrency * Use Laravel Concurrency driver
1 parent 5858349 commit 20c92d5

File tree

6 files changed

+20
-377
lines changed

6 files changed

+20
-377
lines changed

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
],
1717
"require": {
1818
"php": ">=7",
19-
"illuminate/support": "^6|^7|^8|^9|^10.0|^11.0",
20-
"illuminate/console": "^6|^7|^8|^9|^10.0|^11.0",
21-
"symfony/process": "^4|^5|^6|^7.0"
19+
"illuminate/support": "^11.0|^12.0",
20+
"illuminate/queue": "^11.0|^12.0"
2221
},
2322
"autoload": {
2423
"psr-4": {
@@ -27,7 +26,7 @@
2726
},
2827
"extra": {
2928
"branch-alias": {
30-
"dev-master": "0.7-dev"
29+
"dev-master": "0.8-dev"
3130
},
3231
"laravel": {
3332
"providers": [

readme.md

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,29 @@
1-
# Laravel 5 Async Queue Driver
1+
# Laravel Async Queue Driver
22

33
## Push a function/closure to the background.
44

5-
6-
### For Laravel 5.4, check the [0.6 branch](https://github.com/barryvdh/laravel-async-queue/tree/v0.6.0)
7-
8-
### For Laravel 5.3, check the [0.5 branch](https://github.com/barryvdh/laravel-async-queue/tree/v0.5.0)
9-
10-
Just like the 'sync' driver, this is not a real queue driver. It is always fired immediatly.
5+
Just like the 'sync' or 'deferred' connection, this is not a real queue. It is always fired immediately.
116
The only difference is that the closure is sent to the background without waiting for the response.
127
This package is more usable as an alternative for running incidental tasks in the background, without setting up a 'real' queue driver.
13-
14-
> **Note:** This is using the DatabaseQueue, so make sure you set that up first, including migrations.
8+
It is similar to the 'deferred' connection, but it runs in a background process, so might be more suitable for long running tasks.
159

1610
### Install
1711

1812
Require the latest version of this package with Composer
1913

2014
composer require barryvdh/laravel-async-queue
2115

22-
Add the Service Provider to the providers array in config/app.php
23-
24-
Barryvdh\Queue\AsyncServiceProvider::class,
25-
26-
You need to create the migration table for queues and run it.
27-
28-
$ php artisan queue:table
29-
$ php artisan migrate
30-
3116
You should now be able to use the async driver in config/queue.php. Use the same config as for the database, but use async as driver.
3217

3318
'connections' => array(
3419
...
3520
'async' => array(
3621
'driver' => 'async',
37-
'table' => 'jobs',
38-
'queue' => 'default',
39-
'expire' => 60,
4022
),
4123
...
4224
}
4325

4426
Set the default to `async`, either by changing to config or setting `QUEUE_DRIVER` in your `.env` file to `async`.
4527

46-
> Note: By default, `php` is used as the binary path to PHP. You can change this by adding the `binary` option to the queue config. You can also add extra arguments (for HHVM for example)
47-
48-
'connections' => array(
49-
...
50-
'async' => array(
51-
'driver' => 'async',
52-
'table' => 'jobs',
53-
'queue' => 'default',
54-
'expire' => 60,
55-
'binary' => 'php',
56-
'binary_args' => '',
57-
),
58-
...
59-
}
60-
6128
It should work the same as the sync driver, so no need to run a queue listener. Downside is that you cannot actually queue or plan things. Queue::later() is also fired directly. For more info see http://laravel.com/docs/queues
6229

src/AsyncQueue.php

Lines changed: 7 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -1,187 +1,18 @@
11
<?php
22
namespace Barryvdh\Queue;
33

4-
use Illuminate\Database\Connection;
5-
use Illuminate\Queue\DatabaseQueue;
6-
use Illuminate\Queue\Jobs\DatabaseJob;
7-
use Symfony\Component\Process\Process;
8-
use Illuminate\Queue\Jobs\DatabaseJobRecord;
4+
use Illuminate\Queue\SyncQueue;
5+
use Illuminate\Support\Facades\Concurrency;
6+
use Illuminate\Support\Facades\Queue;
97

10-
class AsyncQueue extends DatabaseQueue
8+
class AsyncQueue extends SyncQueue
119
{
12-
/** @var string */
13-
protected $binary;
14-
15-
/** @var string */
16-
protected $binaryArgs;
17-
18-
/** @var string */
19-
protected $connectionName;
20-
2110
/**
22-
* @param \Illuminate\Database\Connection $database
23-
* @param string $table
24-
* @param string $default
25-
* @param int $expire
26-
* @param string $binary
27-
* @param string|array $binaryArgs
28-
*/
29-
public function __construct(Connection $database, $table, $default = 'default', $expire = 60, $binary = 'php', $binaryArgs = '', $connectionName = '')
30-
{
31-
parent::__construct($database, $table, $default, $expire);
32-
$this->binary = $binary;
33-
$this->binaryArgs = $binaryArgs;
34-
$this->connectionName = $connectionName;
35-
}
36-
37-
/**
38-
* Push a new job onto the queue.
39-
*
40-
* @param string $job
41-
* @param mixed $data
42-
* @param string|null $queue
43-
*
44-
* @return int
11+
* {@inheritdoc}
4512
*/
4613
public function push($job, $data = '', $queue = null)
4714
{
48-
$id = parent::push($job, $data, $queue);
49-
$this->startProcess($id);
50-
51-
return $id;
52-
}
53-
54-
/**
55-
* Push a raw payload onto the queue.
56-
*
57-
* @param string $payload
58-
* @param string $queue
59-
* @param array $options
60-
* @return mixed
61-
*/
62-
public function pushRaw($payload, $queue = null, array $options = [])
63-
{
64-
$id = parent::pushRaw($payload, $queue, $options);
65-
$this->startProcess($id);
66-
67-
return $id;
68-
}
69-
70-
/**
71-
* Push a new job onto the queue after a delay.
72-
*
73-
* @param \DateTime|int $delay
74-
* @param string $job
75-
* @param mixed $data
76-
* @param string|null $queue
77-
*
78-
* @return int
79-
*/
80-
public function later($delay, $job, $data = '', $queue = null)
81-
{
82-
$id = parent::later($delay, $job, $data, $queue);
83-
$this->startProcess($id);
84-
85-
return $id;
86-
}
87-
88-
/**
89-
* Create an array to insert for the given job.
90-
*
91-
* @param string|null $queue
92-
* @param string $payload
93-
* @param int $availableAt
94-
* @param int $attempts
95-
* @return array
96-
*/
97-
protected function buildDatabaseRecord($queue, $payload, $availableAt, $attempts = 0)
98-
{
99-
$record = parent::buildDatabaseRecord($queue, $payload, $availableAt, $attempts);
100-
$record['reserved_at'] = $this->currentTime();
101-
102-
return $record;
103-
}
104-
105-
/**
106-
* Get the next available job for the queue.
107-
*
108-
* @param int $id
109-
* @return DatabaseJob
110-
*/
111-
public function getJobFromId($id)
112-
{
113-
$job = $this->database->table($this->table)
114-
->where('id', $id)
115-
->first();
116-
117-
if ($job) {
118-
$job = $this->markJobAsReserved(new DatabaseJobRecord((object) $job));
119-
return new DatabaseJob(
120-
$this->container, $this, $job, $this->connectionName, $job->queue
121-
);
122-
}
123-
}
124-
125-
/**
126-
* Make a Process for the Artisan command for the job id.
127-
*
128-
* @param int $id
129-
*
130-
* @return void
131-
*/
132-
public function startProcess($id)
133-
{
134-
$command = $this->getCommand($id);
135-
$cwd = base_path();
136-
137-
$process = new Process([$command], $cwd);
138-
$process->run();
139-
}
140-
141-
/**
142-
* Get the Artisan command as a string for the job id.
143-
*
144-
* @param int $id
145-
*
146-
* @return string
147-
*/
148-
protected function getCommand($id)
149-
{
150-
$connection = $this->connectionName;
151-
$cmd = '%s artisan queue:async %d %s';
152-
$cmd = $this->getBackgroundCommand($cmd);
153-
154-
$binary = $this->getPhpBinary();
155-
156-
return sprintf($cmd, $binary, $id, $connection);
157-
}
158-
159-
/**
160-
* Get the escaped PHP Binary from the configuration
161-
*
162-
* @return string
163-
*/
164-
protected function getPhpBinary()
165-
{
166-
$path = $this->binary;
167-
if ( ! defined('PHP_WINDOWS_VERSION_BUILD')) {
168-
$path = escapeshellarg($path);
169-
}
170-
171-
$args = $this->binaryArgs;
172-
if (is_array($args)) {
173-
$args = implode(' ', $args);
174-
}
175-
176-
return trim($path . ' ' . $args);
177-
}
178-
179-
protected function getBackgroundCommand($cmd)
180-
{
181-
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
182-
return 'start /B ' . $cmd . ' > NUL';
183-
}
184-
185-
return $cmd . ' > /dev/null 2>&1 &';
15+
Concurrency::driver('process')
16+
->defer(fn () => Queue::connection('sync')->push($job, $data, $queue));
18617
}
18718
}

src/AsyncServiceProvider.php

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@
44

55
use Barryvdh\Queue\Connectors\AsyncConnector;
66
use Barryvdh\Queue\Console\AsyncCommand;
7+
use Illuminate\Contracts\Support\DeferrableProvider;
78
use Illuminate\Support\ServiceProvider;
89

9-
class AsyncServiceProvider extends ServiceProvider
10+
class AsyncServiceProvider extends ServiceProvider implements DeferrableProvider
1011
{
11-
/**
12-
* Indicates if loading of the provider is deferred.
13-
*
14-
* @var bool
15-
*/
16-
protected $defer = false;
17-
1812
/**
1913
* Add the connector to the queue drivers.
2014
*
@@ -23,31 +17,6 @@ class AsyncServiceProvider extends ServiceProvider
2317
public function boot()
2418
{
2519
$this->registerAsyncConnector($this->app['queue']);
26-
27-
$this->commands('command.queue.async');
28-
}
29-
30-
/**
31-
* Register the service provider.
32-
*
33-
* @return void
34-
*/
35-
public function register()
36-
{
37-
$this->registerAsyncCommand();
38-
}
39-
40-
/**
41-
* Register the queue listener console command.
42-
*
43-
*
44-
* @return void
45-
*/
46-
protected function registerAsyncCommand()
47-
{
48-
$this->app->singleton('command.queue.async', function () {
49-
return new AsyncCommand($this->app['queue.worker']);
50-
});
5120
}
5221

5322
/**
@@ -60,17 +29,7 @@ protected function registerAsyncCommand()
6029
protected function registerAsyncConnector($manager)
6130
{
6231
$manager->addConnector('async', function () {
63-
return new AsyncConnector($this->app['db']);
32+
return new AsyncConnector;
6433
});
6534
}
66-
67-
/**
68-
* Get the services provided by the provider.
69-
*
70-
* @return array
71-
*/
72-
public function provides()
73-
{
74-
return ['command.queue.async'];
75-
}
7635
}

src/Connectors/AsyncConnector.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,15 @@
33
namespace Barryvdh\Queue\Connectors;
44

55
use Barryvdh\Queue\AsyncQueue;
6-
use Illuminate\Queue\Connectors\DatabaseConnector;
7-
use Illuminate\Support\Arr;
6+
use Illuminate\Queue\Connectors\SyncConnector;
87

9-
class AsyncConnector extends DatabaseConnector
8+
class AsyncConnector extends SyncConnector
109
{
11-
1210
/**
13-
* Establish a queue connection.
14-
*
15-
* @param array $config
16-
*
17-
* @return \Illuminate\Contracts\Queue\Queue
11+
* {@inheritdoc}
1812
*/
1913
public function connect(array $config)
2014
{
21-
return new AsyncQueue(
22-
$this->connections->connection(Arr::get($config, 'connection')),
23-
$config['table'],
24-
$config['queue'],
25-
Arr::get($config, 'expire', 60),
26-
Arr::get($config, 'binary', 'php'),
27-
Arr::get($config, 'binary_args', ''),
28-
Arr::get($config, 'connection_name', '')
29-
);
15+
return new AsyncQueue($config['after_commit'] ?? null);
3016
}
3117
}

0 commit comments

Comments
 (0)