PHP code example of hellocron / php

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

    

hellocron / php example snippets


use HelloCron\HelloCron;

$cx = new HelloCron(ingestKey: 'ck_your_key');

$cx->job('db-backup', function () {
    // ... do the work ...
});

use HelloCron\HelloCron;
use HelloCron\Enum\PingStatus;

$cx = new HelloCron(ingestKey: 'ck_your_key');

$series = uniqid();
$cx->ping('db-backup', PingStatus::Run, series: $series);

// ... do the work ...

$cx->ping('db-backup', PingStatus::Complete, series: $series, duration: 47.3, exitCode: 0);

$cx->event('deploys', state: 'started', host: gethostname());

use HelloCron\Enum\MonitorKind;

$cx = new HelloCron(managementKey: 'mk_your_key');

$account = $cx->account();                       // plan, monitors used/limit
$list = $cx->monitors()->list(MonitorKind::Ping);

$cx->monitors()->create([
    'manifest_version' => 1,
    'kind' => 'ping',
    'name' => 'db-backup',
    'enabled' => true,
    'expected_interval_seconds' => 86400,
    'grace_seconds' => 3600,
    'tags' => ['production'],
]);

$bundle = $cx->monitors()->export();             // whole account as a bundle
$report = $cx->monitors()->apply($bundle, dryRun: true);

$cx->monitors()->delete($uuid, confirm: true);   // without confirm: refused locally

// derives expected_interval_seconds from the schedule and a grace of
// interval/2 clamped to [2 min, 1 h]; needs dragonmantank/cron-expression
$cx->monitors()->ensure('db-backup', cronExpression: '0 3 * * *');

// or state the interval yourself - no extra package needed
$cx->monitors()->ensure('db-backup', intervalSeconds: 86400);

// computed fields can be overridden
$cx->monitors()->ensure('db-backup', intervalSeconds: 86400, extra: ['tags' => ['production']]);

// validate the file in CI (no changes are made)
$report = $cx->monitors()->applyFromFile('monitors.yaml', dryRun: true);

// apply it on deploy
$report = $cx->monitors()->applyFromFile('monitors.yaml');

// snapshot the current account state back into a file
$cx->monitors()->exportToFile('monitors.yaml');

use HelloCron\Enum\ErrorCode;
use HelloCron\Exception\{ValidationException, AuthException, RateLimitException,
    ConfirmRequiredException, ConfigAsCodeDisabledException, TransportException};

try {
    $cx->monitors()->create($manifest);
} catch (ValidationException $e) {
    // $e->errorCodes() => ['schedule' => [ErrorCode::ScheduleInvalid]]
    // $e->errors()     => human-readable messages per field
} catch (AuthException $e) {
    // bad key, or a scoped key missing the needed scope
} catch (RateLimitException $e) {
    // 60 req/min per management key
}

$cx = new HelloCron(
    ingestKey: 'ck_your_key',
    httpClient: new \GuzzleHttp\Client(['connect_timeout' => 1, 'timeout' => 2]),
);
bash
composer