muhammedalkhudiry / laravel-long-term-tasks
这是我的包 laravel-long-term-tasks
0.0.2
2024-08-04 15:09 UTC
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0
- laravel/serializable-closure: ^1.3
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- spatie/laravel-ray: ^1.35
README
此包处理必须运行长期任务时的常见情况。
- 示例:在30天无活动后删除用户账户
安装
您可以通过composer安装此包
composer require muhammedalkhudiry/laravel-long-term-tasks
您可以使用以下命令发布并运行迁移
php artisan vendor:publish --tag="long-term-tasks-migrations"
php artisan migrate
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="long-term-tasks-config"
这是已发布配置文件的内容
return [ 'model' => \MuhammedAlkhudiry\LaravelLongTermTasks\Models\LongTermTask::class, ];
概述
假设您有一个客户需要多次付款,我们必须提交他的第一笔付款,您想提醒他在30天内提交第二笔付款。
通常,您会创建一个命令来检查数据库中未提交第二笔付款的用户,并发送提醒邮件,然后在计划中运行此命令。
(这里的逻辑可能更复杂,例如检查用户是否有有效的订阅,用户是否有有效的付款方式等。)
// App\Console\Kernel.php $schedule->command('second-payment-reminder:send')->everyMinute(); // App\Console\Commands\SecondPaymentReminder.php public function handle() { Payment::query() ->where('type', PaymentType::FIRST->value) ->where('is_customer_notified', false) ->each( function (Payment $payment) { if ($payment->next_payment_at?->isToday()) { $payment->customer->notify(new SecondPaymentReminderNotification($payment)); $payment->update(['is_customer_notified' => true]); } } ); }
使用此包,您可以在30天后执行任务,您可以在任务本身中处理逻辑。
schedule(new \App\Jobs\SecondPaymentReminder()) ->on(now()->addDays(30)) ->name("second-payment-{$payment->id}") ->save();
就是这样!✨
假设用户退款了第一笔付款,您可以使用任务名称删除任务。
\MuhammedAlkhudiry\LaravelLongTermTasks\TaskScheduler::delete("second-payment-{$payment->id}");
用法
将命令添加到您的计划中
$schedule->command('long-term-tasks:process')->everyMinute(); // You can change the frequency depending on your needs
创建任务
schedule(new \App\Jobs\SecondPaymentReminder()) ->on(now()->addDays(1)) // Required, the date when the task should be executed ->name("second-payment-{$payment->id}") // Optional, you can use it later to delete/update the task ->then(function ($task) { // When the task is executed }) ->catch(function ($task, $exception) { // When the task failed }) ->finally(function ($task) { // When the task is executed or failed }) ->shouldQueue() // Optional, by default it will run synchronously ->save(); // Required, to save the task
注意
then
、catch
和finally
将被序列化。
删除任务
\MuhammedAlkhudiry\LaravelLongTermTasks\TaskScheduler::delete("second-payment-{$payment->id}");
更新任务
\MuhammedAlkhudiry\LaravelLongTermTasks\TaskScheduler::get("second-payment-{$payment->id}") ->on(now()->addDays(1)) ->update();
测试
composer test
变更日志
请参阅变更日志以获取有关最近更改的更多信息。
贡献
请参阅贡献指南以获取详细信息。
安全漏洞
请审查我们的安全策略,了解如何报告安全漏洞。
鸣谢
许可
MIT许可证(MIT)。有关更多信息,请参阅许可文件。