This repository was archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Full Example
Miguel Muscat edited this page Feb 13, 2019
·
3 revisions
The below example demonstrates how one can set up Cronarchy with reusable constants for centralizing the instance ID, daemon URL and config path, as well as using a static var inside a function to store and cache the Cronarchy instance.
plugins/
├── example/
│ ├── includes/
. │ ├── cron-daemon.php
. │ └── cron-config
. ├── vendor/
│ ├── autoload.php
│ ...
├── boostrap.php
└── example.php
<?php
// Load autoloader, with puns
require __DIR__ . '/vendor/autoload.php';
// Plugin constants
define('EXAMPLE_FILE', __FILE__);
define('EXAMPLE_DIR', __DIR__);
define('EXAMPLE_URL', plugin_dir_url(EXAMPLE_FILE));
// Constants for Cronarchy
define('EXAMPLE_CRONARCHY_ID', 'example');
define('EXAMPLE_DAEMON_URL', EXAMPLE_URL . 'includes/cron-daemon.php');
define('EXAMPLE_DAEMON_CONFIG_PATH', EXAMPLE_DIR . 'includes/cron-config');<?php
use RebelCode\Cronarchy\Daemon;
require_once __DIR__ . '/bootstrap.php';
$daemon = new Daemon(EXAMPLE_CRONARCHY_ID, EXAMPLE_DAEMON_CONFIG_PATH);
$daemon->run();<?php
use RebelCode\Cronarchy\Cronarchy;
require_once __DIR__ . '/bootstrap.php';
function exampleCronarchyInstance() {
static $instance = null;
if ($instance === null) {
$instance = Cronarchy::setup(
EXAMPLE_CRONARCHY_ID,
EXAMPLE_DAEMON_URL,
EXAMPLE_DAEMON_CONFIG_PATH,
[
'run_interval' => 15,
'max_job_run_time' => 60,
'max_total_run_time' => 600,
]
);
}
return $instance;
}
add_action('init', function () {
$manager = exampleCronarchyInstance()->getManager();
try {
$manager->getJob(null, 'my_cron_hook');
} catch (OutOfRangeException $exception) {
// Run 1 minute from now, every 15 minutes
$job = new Job(null, time() + 60 , 'my_cron_hook', [], 15 * 60);
$manager->scheduleJob($job);
}
});
add_action('my_cron_hook', function() {
echo 'Running my cron!';
});