muhammedalkhudiry / laravel-long-term-tasks
This is my package laravel-long-term-tasks
Fund package maintenance!
muhammed alkhudiry
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
This package is auto-updated.
Last update: 2025-03-03 05:42:28 UTC
README
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.