tobento / app-queue
Queue app support.
Requires
- php: >=8.0
- tobento/app: ^1.0.7
- tobento/app-console: ^1.0
- tobento/app-logging: ^1.0
- tobento/app-migration: ^1.0
- tobento/service-queue: ^1.0
- tobento/service-storage: ^1.2.5
Requires (Dev)
- phpunit/phpunit: ^9.5
- tobento/app-event: ^1.0.1
- tobento/service-filesystem: ^1.0.5
- vimeo/psalm: ^4.0
README
Queue support for the app using the Queue Service.
Table of Contents
Getting Started
Add the latest version of the app queue project running this command.
composer require tobento/app-queue
Requirements
- PHP 8.0 or greater
Documentation
App
Check out the App Skeleton if you are using the skeleton.
You may also check out the App to learn more about the app in general.
Queue Boot
The queue boot does the following:
- installs and loads queue config file
- implements queue interfaces
use Tobento\App\AppFactory; use Tobento\Service\Queue\QueueInterface; use Tobento\Service\Queue\QueuesInterface; use Tobento\Service\Queue\JobProcessorInterface; use Tobento\Service\Queue\FailedJobHandlerInterface; // Create the app $app = (new AppFactory())->createApp(); // Add directories: $app->dirs() ->dir(realpath(__DIR__.'/../'), 'root') ->dir(realpath(__DIR__.'/../app/'), 'app') ->dir($app->dir('app').'config', 'config', group: 'config') ->dir($app->dir('root').'public', 'public') ->dir($app->dir('root').'vendor', 'vendor'); // Adding boots $app->boot(\Tobento\App\Queue\Boot\Queue::class); $app->booting(); // Implemented interfaces: $queue = $app->get(QueueInterface::class); $queues = $app->get(QueuesInterface::class); $jobProcessor = $app->get(JobProcessorInterface::class); $failedJobHandler = $app->get(FailedJobHandlerInterface::class); // Run the app $app->run();
Console Note
The queue boot automatically boots the App Console Boot as to run the queue worker using commands.
If you are not using the App Skeleton you may adjust the app
file in the root directory with the path to your app:
// Get and run the application. // (require __DIR__.'/app/app.php')->run(); (require __DIR__.'/path/to/app.php')->run();
Queue Config
The configuration for the queue is located in the app/config/queue.php
file at the default App Skeleton config location where you can specify the queues for your application.
Creating Jobs
Check out the Queue Service - Creating Jobs section to learn more about creating jobs.
Dispatching Jobs
Check out the Queue Service - Dispatching Jobs section to learn more about creating jobs.
Running Queues
To run queues you may run the Queue Worker using the queue:work
console command.
php app queue:work
Check out the Queue Service - Work Command section to learn more about the command.
To keep the queue:work
process running permanently in the background, you should use a process monitor such as Supervisor to ensure that the queue worker does not stop running.
Alternatives
Alternatively, you may run the queue:work
command using the App Schedule by using the Command Task running every minute:
use Tobento\Service\Schedule\Task\CommandTask;
$task = (new CommandTask(
command: 'queue:work',
input: [
// you may stop the queue to work when it is empty:
'--stop-when-empty' => null,
// you may run only a specific queue instead of all:
'--queue' => 'secondary',
// it is advised to define the timeout in seconds
// before your server times out, otherwise
// failed jobs might not be handled.
'--timeout' => 60,
],
))->cron('* * * * *');
Failed Jobs
By default, failed jobs will be handled by the implemented \Tobento\App\Queue\LogFailedJobHandler::class
. Any failed job will be repushed to the queue until it reaches the maximum retries. Once it has reached the maximum retries the job will not be queued anymore and will be sent into the app log.
On the App Logging Config file you may define a specific logger:
/*
|--------------------------------------------------------------------------
| Aliases
|--------------------------------------------------------------------------
*/
'aliases' => [
\Tobento\App\Queue\LogFailedJobHandler::class => 'error',
],
Custom Handler
You can change the default behavior by creating a custom failed job handler and using the app on
method to replace the implemented handler:
use Tobento\Service\Queue\FailedJobHandlerInterface;
$app->on(FailedJobHandlerInterface::class, function (): FailedJobHandlerInterface {
return new CustomFailedJobHandler();
});