PHP code example of faerber / laravel-kernel-cadence

1. Go to this page and download the library: Download faerber/laravel-kernel-cadence library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

faerber / laravel-kernel-cadence example snippets


// Before: every one of these fires at :00, :15, :30, :45
$schedule->command('tt:dailyOrders')->everyFifteenMinutes();
$schedule->command('tt:etsyNumbers')->everyFifteenMinutes();
$schedule->command('tt:tlmOrders')->everyFifteenMinutes();

// After: the same cadence, spread across the quarter hour
$schedule->command('tt:dailyOrders')->everyMinutes(15, offsetBy: 8);
$schedule->command('tt:etsyNumbers')->everyMinutes(15, offsetBy: 6);
$schedule->command('tt:tlmOrders')->everyMinutes(15, offsetBy: 10);

// Offset 2 minutes to spread the per-tenant fan-out load;
// everyFiveMinutes() has no offset modifier.
$schedule->command('ail:dispatchDueOrderReminders')->cron('2-59/5 * * * *');

$schedule->command('cron:shipRatings')->everyMinutes(10, offsetBy: 2)->withoutOverlapping(10);

$schedule->command('cron:shipRatings')->everyTenMinutes()->offsetBy(2);
$schedule->command('cron:dailyReport')->dailyAt('03:00')->offsetBy(7);   //=> 03:07
$schedule->command('cron:warmCache')->hourly()->offsetBy(7);             //=> hourlyAt(7)

use Faerber\KernelCadence\Cadence;

Cadence::everyMinutes(15, offsetBy: 8)->expression();  //=> '8-59/15 * * * *'  (:08 :23 :38 :53)
Cadence::everyFifteenMinutes(8)->expression();         //=> the same thing, named
Cadence::everyFifteenMinutes()->offsetBy(8)->minutes(); //=> [8, 23, 38, 53]

$schedule->command('tt:dailyOrders')->cron(Cadence::everyFifteenMinutes(8)->expression());

use Faerber\KernelCadence\Cadence;

$lanes = Cadence::everyFifteenMinutes()->spreadAcross(3);  // phases :00, :05, :10

$schedule->command('cron:aquatreeOrderRawImport')->cadence($lanes->lane(0));
$schedule->command('cron:aquatreeOrderImport')->cadence($lanes->lane(1));
$schedule->command('cron:aquatreeUpdateShipping')->cadence($lanes->lane(2));

use Faerber\KernelCadence\Scheduling\ApplicationSchedule;
use Faerber\KernelCadence\Testing\AssertsScheduleLoad;
use Illuminate\Console\Scheduling\Schedule;

class ScheduleLoadDistributionTest extends TestCase {
    use AssertsScheduleLoad;

    private const MAX_TASKS_PER_MINUTE = 12;

    public function test_no_single_minute_carries_a_disproportionate_share(): void {
        $this->assertNoMinuteCarriesMoreThan(
            self::MAX_TASKS_PER_MINUTE,
            $this->hourlyRecurringLoad($this->schedule()),
        );
    }

    public function test_the_top_of_the_hour_is_not_an_outlier(): void {
        $this->assertTopOfTheHourIsNotAnOutlier($this->hourlyRecurringLoad($this->schedule()));
    }

    public function test_the_import_pipeline_stays_ordered(): void {
        $this->assertRunsBefore('cron:orderRawImport', 'cron:orderImport', $this->schedule());
    }

    private function schedule(): Schedule {
        return ApplicationSchedule::resolve($this->app);
    }
}

use Faerber\KernelCadence\Analysis\ScheduleLoad;

$load = ScheduleLoad::ofHourlyRecurring($schedule);

$load->peak();            //=> MinuteLoad, ':00 (11 tasks)'
$load->at(15)->tasks;     //=> 4
$load->exceeding(12);     //=> list<MinuteLoad>, busiest first
$load->mean();            //=> 4.783333
$load->histogram();       //=> the bar chart above

// Before
$schedule->command('tt:dailyOrders')->cron(Cadence::everyMinutes(15, offsetBy: 8));
// After
$schedule->command('tt:dailyOrders')->everyMinutes(15, offsetBy: 8);

php artisan schedule:load --max=12