lastdragon-ru / lara-asp-queue
Requires
- php: ^8.1|^8.2|^8.3
- laravel/framework: ^9.21.0|^10.0.0
- lastdragon-ru/lara-asp-core: 5.6.0
- symfony/polyfill-php83: ^1.28
Requires (Dev)
- lastdragon-ru/lara-asp-testing: 5.6.0
- orchestra/testbench: ^7.0.0|^8.0.0
- phpunit/phpunit: ^9.5.0|^10.1.0
Suggests
- ext-fileinfo: Required for league/flysystem
- 5.x-dev
- 5.6.0
- 5.5.0
- 5.4.0
- 5.3.1
- 5.3.0
- 5.2.0
- 5.1.0
- 5.0.0
- 5.0.0-beta.1
- 5.0.0-beta.0
- 4.x-dev
- 4.6.0
- 4.5.2
- 4.5.1
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.1
- 4.2.0
- 4.1.0
- 4.0.0
- 3.0.0
- 2.x-dev
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.x-dev
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.15.0
- 0.14.1
- 0.14.0
- 0.13.0
- 0.12.0
- 0.11.0
- 0.10.0
- 0.9.0
- 0.8.1
- 0.8.0
- 0.7.0
- 0.6.1
- 0.6.0
- 0.5.0
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.0
This package is auto-updated.
Last update: 2024-02-16 07:59:18 UTC
README
此包为队列任务和队列监听器提供额外功能,如多级配置支持、任务覆盖(对于包开发非常有用,可以提供基础实现并允许应用程序扩展它)、易于定义定时任务,以及构造函数中的依赖注入支持。
警告
由于复杂性和支持困难,该包被标记为过时。请使用 core 包代替,该包提供类似但简化的功能。
要求
要求 | 约束 | 支持 |
---|---|---|
PHP | ^8.3 |
HEAD ⋯ 5.0.0 |
^8.2 |
HEAD ⋯ 2.0.0 |
|
^8.1 |
HEAD ⋯ 2.0.0 |
|
^8.0 |
4.6.0 ⋯ 2.0.0 |
|
^8.0.0 |
1.1.2 ⋯ 0.12.0 |
|
>=8.0.0 |
0.11.0 ⋯ 0.4.0 |
|
>=7.4.0 |
0.3.0 ⋯ 0.1.0 |
|
Laravel | ^10.0.0 |
HEAD ⋯ 2.1.0 |
^9.21.0 |
HEAD ⋯ 5.0.0-beta.1 |
|
^9.0.0 |
5.0.0-beta.0 ⋯ 0.12.0 |
|
^8.22.1 |
3.0.0 ⋯ 0.2.0 |
|
^8.0 |
0.1.0 |
安装
-
运行
composer require lastdragon-ru/lara-asp-queue
-
将以下代码添加到
bootstrap/app.php
中以覆盖默认事件调度器(在所有其他单例之前)$app->singleton('events', \LastDragon_ru\LaraASP\Queue\EventsDispatcher::class);
如果您想使用配置/DI进行队列监听器,则需要这样做。请参阅 laravel/framework#25272 了解原因。
配置
要添加任务/监听器/可邮寄配置,您只需扩展以下 基类 之一
<?php declare(strict_types = 1); namespace App\Jobs; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Date; use LastDragon_ru\LaraASP\Core\Utils\Cast; use LastDragon_ru\LaraASP\Queue\QueueableConfigurator; use LastDragon_ru\LaraASP\Queue\Queueables\Job; use Override; class MyJobWithConfig extends Job { /** * Default config. * * @inheritDoc */ #[Override] public function getQueueConfig(): array { return [ 'queue' => 'queue', 'settings' => [ 'expire' => '18 hours', ], ] + parent::getQueueConfig(); } public function __invoke(QueueableConfigurator $configurator): void { // This is how we can get access to the actual config inside `handle` $config = $configurator->config($this); $expire = Cast::toString($config->setting('expire')); $expire = Date::now()->sub($expire); Model::query() ->where('updated_at', '<', $expire) ->delete(); } }
配置的优先级如下(最后者获胜)
- 自己的属性(例如
$this->connection
、$this->queue
等) - 从
getQueueConfig()
获取的自己的配置 - 应用程序的配置(如果存在,则为
queue.queueables.<class>
从config/queue.php
) onConnection()
、onQueue()
等调用
因此,您可以在应用程序配置中轻松设置任务设置,例如,我们可以将 expire
设置为 8 hours
。
<?php declare(strict_types = 1); use App\Jobs\MyJobWithConfig; // config/queue.php return [ // ..... /* |-------------------------------------------------------------------------- | Queueables Configuration |-------------------------------------------------------------------------- | | These options configure the behavior of custom queue jobs. | */ 'queueables' => [ MyJobWithConfig::class => [ 'settings' => [ 'expire' => '8 hours', ], ], ], ];
定时任务
创建定时任务的方式类似。它们只是有两个额外的设置
<?php declare(strict_types = 1); namespace App\Jobs; use LastDragon_ru\LaraASP\Queue\Queueables\CronJob; use Override; class MyCronJob extends CronJob { /** * @inheritDoc */ #[Override] public function getQueueConfig(): array { return [ 'cron' => '0 * * * *', // Cron expression 'enabled' => true, // Status (`false` will disable the job) ] + parent::getQueueConfig(); } public function __invoke(): void { // .... } }
但是,任务的注册略有不同。对于 Kernel
,您应该使用以下方式
<?php declare(strict_types = 1); namespace App\Console; use App\Jobs\MyCronJob; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use LastDragon_ru\LaraASP\Queue\Concerns\ConsoleKernelWithSchedule; use LastDragon_ru\LaraASP\Queue\Contracts\Cronable; use Override; use function base_path; class Kernel extends ConsoleKernel { // !!! Add this trait use ConsoleKernelWithSchedule; // !!! Add this property and put all cron jobs inside /** * The application's command schedule. * * @var list<class-string<Cronable>> */ protected array $schedule = [ MyCronJob::class, ]; /** * Register the commands for the application. */ #[Override] protected function commands(): void { $this->load(__DIR__.'/Commands'); require base_path('routes/console.php'); } }
并且对于包提供者
<?php declare(strict_types = 1); namespace LastDragon_ru\LaraASP\Migrator; use App\Jobs\MyCronJob; use Illuminate\Support\ServiceProvider; use LastDragon_ru\LaraASP\Queue\Concerns\ProviderWithSchedule; class Provider extends ServiceProvider { use ProviderWithSchedule; public function boot(): void { $this->bootSchedule( // Put all cron jobs provided in the package here MyCronJob::class, ); } }
最后,该包还在任务描述中公开了所有设置
$ php artisan schedule:list
+---------+-------------+------------------------------------------------------------------------+---------------------+
| Command | Interval | Description | Next Due |
+---------+-------------+------------------------------------------------------------------------+---------------------+
| | 0 0 * * * | App\Jobs\JobsCleanupCronJob | 2021-03-14 00:00:00 |
| | | {"queue":"default","enabled":true,"settings":{"expire":"18 hours"}} | |
| | */5 * * * * | App\Jobs\SiteLogsCleanupCronJob | 2021-03-13 06:40:00 |
| | | {"queue":"default","enabled":true,"settings":{"expire":"30 days"}} | |
+---------+-------------+------------------------------------------------------------------------+---------------------+
覆盖包任务
对于包开发者来说,最有意思且最有用的是能够扩展应用程序中所有包任务的能力。例如,我们的包提供了 DoSomethingPackageJob
,其设置可以通过配置轻松更改,但我们能否在应用程序中扩展它?是的!
首先,我们不需要对 CronJob
进行额外操作,但对于 Job
和 Mails
应该使用 Container::make()
<?php declare(strict_types = 1); use Illuminate\Container\Container; use Package\Jobs\DoSomethingPackageJob; // Use Container::getInstance()->make(DoSomethingPackageJob::class)->dispatch(); // Instead of // @phpstan-ignore-next-line DoSomethingPackageJob::dispatch();
然后在应用程序内部
<?php declare(strict_types = 1); namespace App\Jobs; use Override; use Package\Jobs\DoSomethingPackageJob; class DoSomethingAppJob extends DoSomethingPackageJob { #[Override] public function __invoke(): void { // our implementation } }
最后,注册它
<?php declare(strict_types = 1); namespace App\Providers; use App\Jobs\DoSomethingAppJob; use Illuminate\Support\ServiceProvider; use Override; use Package\Jobs\DoSomethingPackageJob; class AppServiceProvider extends ServiceProvider { /** * Register any application services. */ #[Override] public function register(): void { $this->app->bind(DoSomethingAppJob::class, DoSomethingPackageJob::class); } }
🥳
CustomUpdateSomethingJob
将在 config/queue.php
中使用与 UpdateSomethingJob
相同的设置名称。有时您可能想创建一个具有自己配置的新作业,在这种情况下,您应该断开配置链。
<?php declare(strict_types = 1); namespace App\Jobs; use LastDragon_ru\LaraASP\Queue\Concerns\WithConfig; use Override; use Package\Jobs\DoSomethingPackageJob; class DoSomethingAppJob extends DoSomethingPackageJob { use WithConfig; // Indicates that the job has its own config #[Override] public function __invoke(): void { // our implementation } }