rushing/laravel-schedule-runs

There is no license information available for the latest version (1.0) of this package.

Track when the last time a task/command/whatever was executed.

1.0 2023-03-31 13:11 UTC

This package is auto-updated.

Last update: 2024-04-30 00:46:01 UTC


README

Inspired by (Tofandel's Stack Overflow response)[https://stackoverflow.com/a/73554077]. Packagaed for reusability.

Install

composer require rushing/laravel-schedule-runs
php artisan schedule-runs:install

Usage

Use the trait on one of your commands.

class MyCommand {
    use \Rushing\ScheduleRuns\Traits\HasScheduleRuns;

    protected $signature = 'app:my-command {--since=}';

    public function getScheduleRunHandle()
    {
        // The class name is used by default in the trait, but you can override here.
        return static::class;
    }

    public function handle()
    {
        $query = MyModel::query();

        // Get the last schedule run
        $this->getLastScheduleRun();

        // Modify a query to get items created or updated after the last schedule run. Optionally, pass an override parameter.
        $this->scopeWhereLastScheduleRun($query, ['created_at', 'updated_at'], '>=', $this->option('since'));

        // Clear the history
        $this->scheduleRuns()->delete(); // or each->delete() if you want to fire the model events.

        // Set the last run time, optionally with extra data attached.
        $this->setLastScheduleRun(now(), [
            'whatever' => 'you want'
        ]);
    }
}