PHP code example of jalendport / craft-metronome

1. Go to this page and download the library: Download jalendport/craft-metronome 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/ */

    

jalendport / craft-metronome example snippets




use jalendport\metronome\services\Schedule;

return [
    'schedule' => static function(Schedule $schedule): void {
        // A Craft console command. Pass extra arguments as the second array.
        $schedule->command('utils/update-search-indexes')
            ->daily();

        $schedule->command('resave/entries', ['--section=news'])
            ->hourly();

        // A raw shell command.
        $schedule->exec('/usr/bin/backup.sh')
            ->dailyAt('02:30');

        // A PHP callable. Anything it echoes is captured as the task's output.
        $schedule->call(function() {
            // ...
        })->everyFiveMinutes();

        // A queue job — pass a job instance or a job class name.
        $schedule->job(\my\plugin\jobs\SyncJob::class)
            ->hourly();
    },
];

use Craft;
use jalendport\metronome\events\DefineScheduleEvent;
use jalendport\metronome\services\Schedule;
use yii\base\Event;

Event::on(
    Schedule::class,
    Schedule::EVENT_DEFINE_SCHEDULE,
    function(DefineScheduleEvent $event) {
        $event->schedule
            ->job(\my\plugin\jobs\SyncJob::class)
            ->hourly()
            ->name('my-plugin-sync');
    },
);

use Craft;
use jalendport\metronome\events\TaskEvent;
use jalendport\metronome\services\Schedule;
use yii\base\Event;

Event::on(
    Schedule::class,
    Schedule::EVENT_BEFORE_TASK,
    function(TaskEvent $event) {
        $checkInId = \Sentry\captureCheckIn(
            slug: $event->task->getId(),
            status: \Sentry\CheckInStatus::inProgress(),
        );

        // Stash the check-in ID so the after-event can close it out.
        Craft::$app->getCache()->set('sentry:checkin:' . $event->task->getId(), $checkInId, 3600);
    },
);

Event::on(
    Schedule::class,
    Schedule::EVENT_AFTER_TASK,
    function(TaskEvent $event) {
        \Sentry\captureCheckIn(
            slug: $event->task->getId(),
            status: $event->success ? \Sentry\CheckInStatus::ok() : \Sentry\CheckInStatus::error(),
            checkInId: Craft::$app->getCache()->get('sentry:checkin:' . $event->task->getId()) ?: null,
            duration: $event->duration,
        );
    },
);
bash
composer 

$ php craft metronome/clear-locks
Cleared locks for 2 tasks.
bash
cp vendor/jalendport/craft-metronome/src/config.php config/metronome.php