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';
$schedule->event('Foo')->setRunType('multiple')->hourly(); // Runs in all servers
$schedule->event('Foo')->setRunType('single')->hourly(); // Runs in one server
$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.");
}
}
}
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();
}
}