PHP code example of daycry / cronjob

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

    

daycry / cronjob example snippets


    /*
    |--------------------------------------------------------------------------
    | Dashboard login
    |--------------------------------------------------------------------------
    */
    public string $username = 'admin';
    public string $password = 'admin';

/*
    |--------------------------------------------------------------------------
    | Views
    |--------------------------------------------------------------------------
    |
    | Notification of each task
    |
    */
    public array $views = [
        'login'                       => '\Daycry\CronJob\Views\login',
        'dashboard'                   => '\Daycry\CronJob\Views\dashboard',
        'layout'                      => '\Daycry\CronJob\Views\layout',
        'logs'                        => '\Daycry\CronJob\Views\logs'
    ];

$schedule->command('slow-command')->runInBackground()->hourly();

$schedule->event('Foo')->setRunType('multiple')->hourly(); // Runs in all servers
$schedule->event('Foo')->setRunType('single')->hourly(); // Runs in one server

$schedule->command('generate:report')->everyDay()->named('generate-report');
$schedule->command('send:report')->everyDay()->dependsOn('generate-report');

$schedule->command('archive:report')->everyDay()->dependsOn(['generate-report', 'send-report']);

$schedule->command('unstable:task')->maxRetries(3)->timeout(60); // Retries up to 3 times, 60s timeout

class MyJobRunner extends \Daycry\CronJob\JobRunner {
    protected function beforeJob($job) { /* ... */ }
    protected function afterJob($job, $result, $error) { /* ... */ }
}

class MonitoringJobRunner extends \Daycry\CronJob\JobRunner {
    protected function afterJob($job, $result, $error) {
        $duration = $job->duration(); // Or use your own timing logic
        if ($error) {
            // Send alert to Slack, email, etc.
            log_message('alert', "Job '{$job->getName()}' failed: " . $error->getMessage());
        }
        if ($duration > 10) { // seconds
            // Alert if job took too long
            log_message('warning', "Job '{$job->getName()}' took {$duration}s to complete.");
        }
    }
}

$schedule->validateDependencies();

class SlackMonitoringJobRunner extends \Daycry\CronJob\JobRunner {
    protected function afterJob($job, $result, $error) {
        if ($error) {
            $payload = json_encode([
                'text' => "Job '{$job->getName()}' failed: " . $error->getMessage()
            ]);
            $ch = curl_init('https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK');
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_exec($ch);
            curl_close($ch);
        }
    }
}

class SentryMonitoringJobRunner extends \Daycry\CronJob\JobRunner {
    protected function afterJob($job, $result, $error) {
        if ($error) {
            if (class_exists('Sentry\captureException')) {
                \Sentry\captureException($error);
            }
        }
    }
}

use Prometheus\CollectorRegistry;
use Prometheus\Storage\InMemory;

class PrometheusMonitoringJobRunner extends \Daycry\CronJob\JobRunner {
    protected $registry;
    public function __construct() {
        parent::__construct();
        $this->registry = new CollectorRegistry(new InMemory());
    }
    protected function afterJob($job, $result, $error) {
        $duration = $job->duration();
        $gauge = $this->registry->getOrRegisterGauge('cronjob', 'job_duration_seconds', 'Job duration in seconds', ['job']);
        $gauge->set($duration, [$job->getName()]);
    }
}

 namespace Daycry\CronJob\Config;

use CodeIgniter\Config\BaseConfig;
use Daycry\CronJob\Scheduler;

class CronJob extends BaseConfig
{
    /*
    |--------------------------------------------------------------------------
	| Cronjobs
	|--------------------------------------------------------------------------
    |
	| Register any tasks within this method for the application.
	| Called by the TaskRunner.
	|
	| @param Scheduler $schedule
	*/
    public function init(Scheduler $schedule)
    {
        $schedule->call(function() { 
            DemoContent::refresh();
        })->everyMonday();
    }
}

$schedule->shell('cp foo bar')->daily( '11:00 pm' );
$schedule->shell('cp foo bar')->daily( '23:00' );