Skip to content

Instantly share code, notes, and snippets.

@atomjoy
Created September 5, 2025 08:20
Show Gist options
  • Save atomjoy/6b2ad5585273bf655ef5e542f2ec2959 to your computer and use it in GitHub Desktop.
Save atomjoy/6b2ad5585273bf655ef5e542f2ec2959 to your computer and use it in GitHub Desktop.
Schedule Laravel.

Schedule Laravel

Polecenia

# Lista
php artisan schedule:list
# Lokalnie
php artisan schedule:work
# Cron
php artisan schedule:run

Uruchom z cron

crontab -e

# Schedule runs every minute for 1 min.
* * * * * /usr/bin/php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

Utwórz zdarzenia w ServiceProvider

<?php

namespace App\Providers;

use App\Console\Commands\SendEmailsCommand;
use App\Jobs\Heartbeat;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schedule;
use Illuminate\Support\Stringable;

class ScheduleProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        Schedule::exec('php artisan queue:work --queue=default,emails --max-time=60 --memory=128')
            ->cron('* * * * *');
            //->timezone('America/New_York')
            //->runInBackground();
            //->onOneServer();

        Schedule::call(function () {
            Log::info("Schedule cron 5 min");
        })->cron('*/5 * * * *');

        Schedule::call(function () {
            Log::info("Schedule cron 5 sec");
            DB::table('recent_users')->delete();
        })->everyFiveSeconds();

        Schedule::job(new Heartbeat, 'heartbeats', 'sqs')->everyTenMinutes();

        Schedule::command(SendEmailsCommand::class, ['Alexia', '--force'])->daily();
            //->evenInMaintenanceMode();
            //->runInBackground();

        Schedule::command('report:generate')
            ->daily()
            ->sendOutputTo($filePath)
            ->emailOutputTo('[email protected]')
            ->emailOutputOnFailure('[email protected]')
            ->onSuccess(function (Stringable $output) {
                // The task succeeded...
            })
            ->onFailure(function (Stringable $output) {
                // The task failed...
            })
            ->after(function () {
                // The task has executed...
            })
            ->thenPing($url);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment