From e8d696295d2ced1df31f7f797d1a6f1ddea71bb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Iva=C5=9Fcu?= Date: Mon, 9 Sep 2019 13:03:36 +0300 Subject: [PATCH 01/14] Laravel 6.0 Compatibility (#86) * Laravel 6.0 Compatibility * Update composer.json --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 21bf978..1763253 100644 --- a/composer.json +++ b/composer.json @@ -11,8 +11,8 @@ ], "require": { "php": ">=7", - "illuminate/support": "5.5.x|5.6.x|5.7.x|5.8.x", - "illuminate/console": "5.5.x|5.6.x|5.7.x|5.8.x", + "illuminate/support": "^5.5|^6", + "illuminate/console": "^5.5|^6", "symfony/process": "^3.2|^4" }, "autoload": { From 30868e3bf87c769010d8d4d06f1569b518d773b0 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Tue, 25 Feb 2020 21:53:44 +0100 Subject: [PATCH 02/14] Update composer.json --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 1763253..263f500 100644 --- a/composer.json +++ b/composer.json @@ -11,9 +11,9 @@ ], "require": { "php": ">=7", - "illuminate/support": "^5.5|^6", - "illuminate/console": "^5.5|^6", - "symfony/process": "^3.2|^4" + "illuminate/support": "^5.5|^6|^7", + "illuminate/console": "^5.5|^6|^7", + "symfony/process": "^3.2|^4|^5" }, "autoload": { "psr-4": { From 2ec069792a1005e150647fcaaed271968ca5ea66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Iva=C5=9Fcu?= Date: Sat, 12 Sep 2020 19:37:00 +0300 Subject: [PATCH 03/14] Laravel 8 support (#94) * Laravel 8 support * Update composer.json Co-authored-by: Barry vd. Heuvel --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 263f500..25cc511 100644 --- a/composer.json +++ b/composer.json @@ -11,8 +11,8 @@ ], "require": { "php": ">=7", - "illuminate/support": "^5.5|^6|^7", - "illuminate/console": "^5.5|^6|^7", + "illuminate/support": "^5.5|^6|^7|^8", + "illuminate/console": "^5.5|^6|^7|^8", "symfony/process": "^3.2|^4|^5" }, "autoload": { From 39f044e11ed9af0afed4df8ed1fbd8706dabe596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Iva=C5=9Fcu?= Date: Mon, 17 May 2021 11:23:42 +0300 Subject: [PATCH 04/14] Process needs command to be array (#95) --- src/AsyncQueue.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AsyncQueue.php b/src/AsyncQueue.php index a59bfed..3db92f8 100644 --- a/src/AsyncQueue.php +++ b/src/AsyncQueue.php @@ -134,7 +134,7 @@ public function startProcess($id) $command = $this->getCommand($id); $cwd = base_path(); - $process = new Process($command, $cwd); + $process = new Process([$command], $cwd); $process->run(); } From f2ac186f7afbfe5ef22445bc4dd9d036907c35e4 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Mon, 17 May 2021 10:26:03 +0200 Subject: [PATCH 05/14] Update composer.json --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 25cc511..98717fb 100644 --- a/composer.json +++ b/composer.json @@ -11,9 +11,9 @@ ], "require": { "php": ">=7", - "illuminate/support": "^5.5|^6|^7|^8", - "illuminate/console": "^5.5|^6|^7|^8", - "symfony/process": "^3.2|^4|^5" + "illuminate/support": "^6|^7|^8", + "illuminate/console": "^6|^7|^8", + "symfony/process": "^4|^5" }, "autoload": { "psr-4": { From 1b40f1177dbf322dcf0ccae0f93e555d610971c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Iva=C5=9Fcu?= Date: Wed, 9 Feb 2022 15:35:20 +0200 Subject: [PATCH 06/14] Laravel 9 support (#99) --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 98717fb..b6822ce 100644 --- a/composer.json +++ b/composer.json @@ -11,9 +11,9 @@ ], "require": { "php": ">=7", - "illuminate/support": "^6|^7|^8", - "illuminate/console": "^6|^7|^8", - "symfony/process": "^4|^5" + "illuminate/support": "^6|^7|^8|^9", + "illuminate/console": "^6|^7|^8|^9", + "symfony/process": "^4|^5|^6" }, "autoload": { "psr-4": { From 2a6a11a0104184374129189d93c6305482e27b89 Mon Sep 17 00:00:00 2001 From: Laravel Shift Date: Sun, 5 Feb 2023 10:01:47 -0500 Subject: [PATCH 07/14] Bump dependencies for Laravel 10 (#100) --- composer.json | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index b6822ce..568329d 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,12 @@ { "name": "barryvdh/laravel-async-queue", "description": "Async Queue Driver for Laravel (Push to background)", - "keywords": ["laravel", "queue", "async", "background"], + "keywords": [ + "laravel", + "queue", + "async", + "background" + ], "license": "MIT", "authors": [ { @@ -11,8 +16,8 @@ ], "require": { "php": ">=7", - "illuminate/support": "^6|^7|^8|^9", - "illuminate/console": "^6|^7|^8|^9", + "illuminate/support": "^6|^7|^8|^9|^10.0", + "illuminate/console": "^6|^7|^8|^9|^10.0", "symfony/process": "^4|^5|^6" }, "autoload": { From 58583499e99c643ad7aa824b9e4afcf2104dcd41 Mon Sep 17 00:00:00 2001 From: Laravel Shift Date: Sat, 2 Mar 2024 05:11:09 -0500 Subject: [PATCH 08/14] Bump dependencies for Laravel 11 (#102) --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 568329d..55d78a0 100644 --- a/composer.json +++ b/composer.json @@ -16,9 +16,9 @@ ], "require": { "php": ">=7", - "illuminate/support": "^6|^7|^8|^9|^10.0", - "illuminate/console": "^6|^7|^8|^9|^10.0", - "symfony/process": "^4|^5|^6" + "illuminate/support": "^6|^7|^8|^9|^10.0|^11.0", + "illuminate/console": "^6|^7|^8|^9|^10.0|^11.0", + "symfony/process": "^4|^5|^6|^7.0" }, "autoload": { "psr-4": { From 20c92d5299546002c5e34587f244d78f92c9cc62 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Mon, 3 Nov 2025 12:05:43 +0100 Subject: [PATCH 09/14] Use Laravel Concurrency with SyncDriver instead of DatabaseDriver (#103) * Update package requirements for Laravel Concurrency * Use Laravel Concurrency driver --- composer.json | 7 +- readme.md | 39 +------ src/AsyncQueue.php | 183 ++---------------------------- src/AsyncServiceProvider.php | 47 +------- src/Connectors/AsyncConnector.php | 22 +--- src/Console/AsyncCommand.php | 99 ---------------- 6 files changed, 20 insertions(+), 377 deletions(-) delete mode 100644 src/Console/AsyncCommand.php diff --git a/composer.json b/composer.json index 55d78a0..2c07a27 100644 --- a/composer.json +++ b/composer.json @@ -16,9 +16,8 @@ ], "require": { "php": ">=7", - "illuminate/support": "^6|^7|^8|^9|^10.0|^11.0", - "illuminate/console": "^6|^7|^8|^9|^10.0|^11.0", - "symfony/process": "^4|^5|^6|^7.0" + "illuminate/support": "^11.0|^12.0", + "illuminate/queue": "^11.0|^12.0" }, "autoload": { "psr-4": { @@ -27,7 +26,7 @@ }, "extra": { "branch-alias": { - "dev-master": "0.7-dev" + "dev-master": "0.8-dev" }, "laravel": { "providers": [ diff --git a/readme.md b/readme.md index 4aba467..e059221 100644 --- a/readme.md +++ b/readme.md @@ -1,17 +1,11 @@ -# Laravel 5 Async Queue Driver +# Laravel Async Queue Driver ## Push a function/closure to the background. - -### For Laravel 5.4, check the [0.6 branch](https://github.com/barryvdh/laravel-async-queue/tree/v0.6.0) - -### For Laravel 5.3, check the [0.5 branch](https://github.com/barryvdh/laravel-async-queue/tree/v0.5.0) - -Just like the 'sync' driver, this is not a real queue driver. It is always fired immediatly. +Just like the 'sync' or 'deferred' connection, this is not a real queue. It is always fired immediately. The only difference is that the closure is sent to the background without waiting for the response. This package is more usable as an alternative for running incidental tasks in the background, without setting up a 'real' queue driver. - -> **Note:** This is using the DatabaseQueue, so make sure you set that up first, including migrations. +It is similar to the 'deferred' connection, but it runs in a background process, so might be more suitable for long running tasks. ### Install @@ -19,44 +13,17 @@ Require the latest version of this package with Composer composer require barryvdh/laravel-async-queue -Add the Service Provider to the providers array in config/app.php - - Barryvdh\Queue\AsyncServiceProvider::class, - -You need to create the migration table for queues and run it. - - $ php artisan queue:table - $ php artisan migrate - 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. 'connections' => array( ... 'async' => array( 'driver' => 'async', - 'table' => 'jobs', - 'queue' => 'default', - 'expire' => 60, ), ... } Set the default to `async`, either by changing to config or setting `QUEUE_DRIVER` in your `.env` file to `async`. -> 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) - - 'connections' => array( - ... - 'async' => array( - 'driver' => 'async', - 'table' => 'jobs', - 'queue' => 'default', - 'expire' => 60, - 'binary' => 'php', - 'binary_args' => '', - ), - ... - } - 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 diff --git a/src/AsyncQueue.php b/src/AsyncQueue.php index 3db92f8..a30db73 100644 --- a/src/AsyncQueue.php +++ b/src/AsyncQueue.php @@ -1,187 +1,18 @@ binary = $binary; - $this->binaryArgs = $binaryArgs; - $this->connectionName = $connectionName; - } - - /** - * Push a new job onto the queue. - * - * @param string $job - * @param mixed $data - * @param string|null $queue - * - * @return int + * {@inheritdoc} */ public function push($job, $data = '', $queue = null) { - $id = parent::push($job, $data, $queue); - $this->startProcess($id); - - return $id; - } - - /** - * Push a raw payload onto the queue. - * - * @param string $payload - * @param string $queue - * @param array $options - * @return mixed - */ - public function pushRaw($payload, $queue = null, array $options = []) - { - $id = parent::pushRaw($payload, $queue, $options); - $this->startProcess($id); - - return $id; - } - - /** - * Push a new job onto the queue after a delay. - * - * @param \DateTime|int $delay - * @param string $job - * @param mixed $data - * @param string|null $queue - * - * @return int - */ - public function later($delay, $job, $data = '', $queue = null) - { - $id = parent::later($delay, $job, $data, $queue); - $this->startProcess($id); - - return $id; - } - - /** - * Create an array to insert for the given job. - * - * @param string|null $queue - * @param string $payload - * @param int $availableAt - * @param int $attempts - * @return array - */ - protected function buildDatabaseRecord($queue, $payload, $availableAt, $attempts = 0) - { - $record = parent::buildDatabaseRecord($queue, $payload, $availableAt, $attempts); - $record['reserved_at'] = $this->currentTime(); - - return $record; - } - - /** - * Get the next available job for the queue. - * - * @param int $id - * @return DatabaseJob - */ - public function getJobFromId($id) - { - $job = $this->database->table($this->table) - ->where('id', $id) - ->first(); - - if ($job) { - $job = $this->markJobAsReserved(new DatabaseJobRecord((object) $job)); - return new DatabaseJob( - $this->container, $this, $job, $this->connectionName, $job->queue - ); - } - } - - /** - * Make a Process for the Artisan command for the job id. - * - * @param int $id - * - * @return void - */ - public function startProcess($id) - { - $command = $this->getCommand($id); - $cwd = base_path(); - - $process = new Process([$command], $cwd); - $process->run(); - } - - /** - * Get the Artisan command as a string for the job id. - * - * @param int $id - * - * @return string - */ - protected function getCommand($id) - { - $connection = $this->connectionName; - $cmd = '%s artisan queue:async %d %s'; - $cmd = $this->getBackgroundCommand($cmd); - - $binary = $this->getPhpBinary(); - - return sprintf($cmd, $binary, $id, $connection); - } - - /** - * Get the escaped PHP Binary from the configuration - * - * @return string - */ - protected function getPhpBinary() - { - $path = $this->binary; - if ( ! defined('PHP_WINDOWS_VERSION_BUILD')) { - $path = escapeshellarg($path); - } - - $args = $this->binaryArgs; - if (is_array($args)) { - $args = implode(' ', $args); - } - - return trim($path . ' ' . $args); - } - - protected function getBackgroundCommand($cmd) - { - if (defined('PHP_WINDOWS_VERSION_BUILD')) { - return 'start /B ' . $cmd . ' > NUL'; - } - - return $cmd . ' > /dev/null 2>&1 &'; + Concurrency::driver('process') + ->defer(fn () => Queue::connection('sync')->push($job, $data, $queue)); } } diff --git a/src/AsyncServiceProvider.php b/src/AsyncServiceProvider.php index fab4b14..5fb86a8 100644 --- a/src/AsyncServiceProvider.php +++ b/src/AsyncServiceProvider.php @@ -4,17 +4,11 @@ use Barryvdh\Queue\Connectors\AsyncConnector; use Barryvdh\Queue\Console\AsyncCommand; +use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider; -class AsyncServiceProvider extends ServiceProvider +class AsyncServiceProvider extends ServiceProvider implements DeferrableProvider { - /** - * Indicates if loading of the provider is deferred. - * - * @var bool - */ - protected $defer = false; - /** * Add the connector to the queue drivers. * @@ -23,31 +17,6 @@ class AsyncServiceProvider extends ServiceProvider public function boot() { $this->registerAsyncConnector($this->app['queue']); - - $this->commands('command.queue.async'); - } - - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - $this->registerAsyncCommand(); - } - - /** - * Register the queue listener console command. - * - * - * @return void - */ - protected function registerAsyncCommand() - { - $this->app->singleton('command.queue.async', function () { - return new AsyncCommand($this->app['queue.worker']); - }); } /** @@ -60,17 +29,7 @@ protected function registerAsyncCommand() protected function registerAsyncConnector($manager) { $manager->addConnector('async', function () { - return new AsyncConnector($this->app['db']); + return new AsyncConnector; }); } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return ['command.queue.async']; - } } diff --git a/src/Connectors/AsyncConnector.php b/src/Connectors/AsyncConnector.php index e725797..9963ed5 100644 --- a/src/Connectors/AsyncConnector.php +++ b/src/Connectors/AsyncConnector.php @@ -3,29 +3,15 @@ namespace Barryvdh\Queue\Connectors; use Barryvdh\Queue\AsyncQueue; -use Illuminate\Queue\Connectors\DatabaseConnector; -use Illuminate\Support\Arr; +use Illuminate\Queue\Connectors\SyncConnector; -class AsyncConnector extends DatabaseConnector +class AsyncConnector extends SyncConnector { - /** - * Establish a queue connection. - * - * @param array $config - * - * @return \Illuminate\Contracts\Queue\Queue + * {@inheritdoc} */ public function connect(array $config) { - return new AsyncQueue( - $this->connections->connection(Arr::get($config, 'connection')), - $config['table'], - $config['queue'], - Arr::get($config, 'expire', 60), - Arr::get($config, 'binary', 'php'), - Arr::get($config, 'binary_args', ''), - Arr::get($config, 'connection_name', '') - ); + return new AsyncQueue($config['after_commit'] ?? null); } } diff --git a/src/Console/AsyncCommand.php b/src/Console/AsyncCommand.php deleted file mode 100644 index 1950e98..0000000 --- a/src/Console/AsyncCommand.php +++ /dev/null @@ -1,99 +0,0 @@ -worker = $worker; - } - - /** - * Execute the console command. - * - * @param WorkerOptions $options - * @return void - */ - public function handle(WorkerOptions $options) - { - $id = $this->argument('id'); - $connection = $this->argument('connection'); - - $this->processJob( - $connection, $id, $options - ); - } - - /** - * Process the job - * - * @param string $connectionName - * @param integer $id - * @param WorkerOptions $options - * - * @throws \Throwable - */ - protected function processJob($connectionName, $id, $options) - { - $manager = $this->worker->getManager(); - - /** @var AsyncQueue $connection */ - $connection = $manager->connection($connectionName); - - $job = $connection->getJobFromId($id); - - if ( ! is_null($job)) { - $this->worker->process( - $manager->getName($connectionName), $job, $options - ); - } - } - - /** - * Get the console command arguments. - * - * @return array - */ - protected function getArguments() - { - return [ - ['id', InputArgument::REQUIRED, 'The Job ID'], - ['connection', InputArgument::OPTIONAL, 'The name of connection'], - ]; - } -} From fd1220fecd73e2dabf9b8090ed05fc1e0d8cadfc Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Mon, 3 Nov 2025 12:44:58 +0100 Subject: [PATCH 10/14] Fix php version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2c07a27..1905395 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } ], "require": { - "php": ">=7", + "php": "^8.2", "illuminate/support": "^11.0|^12.0", "illuminate/queue": "^11.0|^12.0" }, From ae194f3b24d5109a3575c24c495da974f5e11711 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Mon, 3 Nov 2025 12:51:11 +0100 Subject: [PATCH 11/14] Update AsyncServiceProvider.php --- src/AsyncServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AsyncServiceProvider.php b/src/AsyncServiceProvider.php index 5fb86a8..9613964 100644 --- a/src/AsyncServiceProvider.php +++ b/src/AsyncServiceProvider.php @@ -7,7 +7,7 @@ use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider; -class AsyncServiceProvider extends ServiceProvider implements DeferrableProvider +class AsyncServiceProvider extends ServiceProvider { /** * Add the connector to the queue drivers. From 5ba956db766030b3dd99c79b9baa0120a518c1b0 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Mon, 3 Nov 2025 13:14:55 +0100 Subject: [PATCH 12/14] Use afterResolving --- src/AsyncServiceProvider.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/AsyncServiceProvider.php b/src/AsyncServiceProvider.php index 9613964..2989964 100644 --- a/src/AsyncServiceProvider.php +++ b/src/AsyncServiceProvider.php @@ -3,8 +3,7 @@ namespace Barryvdh\Queue; use Barryvdh\Queue\Connectors\AsyncConnector; -use Barryvdh\Queue\Console\AsyncCommand; -use Illuminate\Contracts\Support\DeferrableProvider; +use Illuminate\Queue\QueueManager; use Illuminate\Support\ServiceProvider; class AsyncServiceProvider extends ServiceProvider @@ -14,22 +13,22 @@ class AsyncServiceProvider extends ServiceProvider * * @return void */ - public function boot() + public function register() { - $this->registerAsyncConnector($this->app['queue']); + $this->registerAsyncConnector(); } /** * Register the Async queue connector. * - * @param \Illuminate\Queue\QueueManager $manager - * * @return void */ - protected function registerAsyncConnector($manager) + protected function registerAsyncConnector() { - $manager->addConnector('async', function () { - return new AsyncConnector; + $this->callAfterResolving(QueueManager::class, function ($manager) { + $manager->addConnector('async', function () { + return new AsyncConnector; + }); }); } } From 193875db72f70d08da29c55eb1d852f9a487a0b4 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Mon, 3 Nov 2025 13:55:23 +0100 Subject: [PATCH 13/14] Revise readme for installation and usage clarity Updated installation instructions and clarified usage notes. --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index e059221..19ada50 100644 --- a/readme.md +++ b/readme.md @@ -7,6 +7,8 @@ The only difference is that the closure is sent to the background without waitin This package is more usable as an alternative for running incidental tasks in the background, without setting up a 'real' queue driver. It is similar to the 'deferred' connection, but it runs in a background process, so might be more suitable for long running tasks. +> Note: Since v0.8 this uses the Concurrently::defer() method instead of the database queue. No database migrations tables are required now. The config can be simplified as below. +> ### Install Require the latest version of this package with Composer From 3fb29a19764ac2376c82b8356081d50437cf3637 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Wed, 5 Nov 2025 09:48:40 +0100 Subject: [PATCH 14/14] Update readme.md --- readme.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/readme.md b/readme.md index 19ada50..e2423e7 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,8 @@ # Laravel Async Queue Driver +## Available as `background` in Laravel [v12.37](https://github.com/laravel/framework/releases/tag/v12.37.0) +Update you config to use driver 'background' and you don't need this package anymore. + ## Push a function/closure to the background. Just like the 'sync' or 'deferred' connection, this is not a real queue. It is always fired immediately.