承接 muhammedalkhudiry/laravel-long-term-tasks 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

muhammedalkhudiry/laravel-long-term-tasks

最新稳定版本:0.0.2

Composer 安装命令:

composer require muhammedalkhudiry/laravel-long-term-tasks

包简介

This is my package laravel-long-term-tasks

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package handles common cases where you must run a long-term task.

  • Example: Delete a user account after 30 days of inactivity

Installation

You can install the package via composer:

composer require muhammedalkhudiry/laravel-long-term-tasks

You can publish and run the migrations with:

php artisan vendor:publish --tag="long-term-tasks-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="long-term-tasks-config"

This is the contents of the published config file:

return [
    'model' => \MuhammedAlkhudiry\LaravelLongTermTasks\Models\LongTermTask::class,
];

Overview

Let's say you have a client who should have multiple payments, and we have to submit his/her first payment, You want to remind him/her to submit the second payment within 30 days.

Typically, you would create a command that checks the database for users who have not submitted the second payment and send them a reminder email and run this command in the schedule.

(The logic here can be more complex, like checking if the user has a valid subscription, if the user has a valid payment method, etc.)

// 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]);
            }
          }
        );
}

using this package, you can create a task that will be executed after 30 days, and you can handle the logic in the task itself.

schedule(new \App\Jobs\SecondPaymentReminder())
    ->on(now()->addDays(30))
    ->name("second-payment-{$payment->id}")
    ->save();

And that's it! ✨

Let's say the user refunded the first payment, you can delete the task using the task name.

\MuhammedAlkhudiry\LaravelLongTermTasks\TaskScheduler::delete("second-payment-{$payment->id}");

Usage

Add the command to your schedule

$schedule->command('long-term-tasks:process')->everyMinute(); // You can change the frequency depending on your needs

Create a Task

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

Note

then, catch, and finally will be serialized.

Delete a Task

    \MuhammedAlkhudiry\LaravelLongTermTasks\TaskScheduler::delete("second-payment-{$payment->id}");

Update a Task

\MuhammedAlkhudiry\LaravelLongTermTasks\TaskScheduler::get("second-payment-{$payment->id}")
    ->on(now()->addDays(1))
    ->update();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-04