Configuration
ServiceProvider Registration
The SchedulerServiceProvider is the primary configuration point. When booted, it automatically configures WP-Cron integration.
use WPZylos\Framework\Scheduler\SchedulerServiceProvider;
$app->register(new SchedulerServiceProvider());
Container Bindings
The ServiceProvider registers two bindings:
| Binding | Class | Type | Description |
|---|---|---|---|
Schedule::class | Schedule | Singleton | Primary schedule instance |
'scheduler' | Schedule | Singleton | Alias for convenience |
Both resolve to the same Schedule instance:
$schedule = $app->make(Schedule::class);
// or
$schedule = $app->make('scheduler');
Cron Schedule Configuration
The ServiceProvider registers a custom every_minute cron interval via the cron_schedules filter. The interval name is prefixed using your plugin's context prefix:
// Resulting schedule name: {prefix}every_minute
// Example: myplugin_every_minute
Cron Hook Name
The recurring cron event hook name is also context-prefixed:
// Hook name: {prefix}run_scheduler
// Example: myplugin_run_scheduler
Task Frequency Defaults
| Property | Default | Description |
|---|---|---|
intervalSeconds | 3600 (hourly) | Default interval when no frequency is set |
atTime | null | No specific time constraint |
dayOfWeek | null | No day-of-week constraint |
dayOfMonth | null | No day-of-month constraint |
atMinute | null | No minute constraint |
Lock Configuration
| Property | Default | Description |
|---|---|---|
preventOverlap | false | Overlap prevention disabled by default |
lockExpiresAt | 1440 (minutes) | Lock auto-expires after 24 hours |
Customize lock duration per-task:
$schedule->call($fn)
->withoutOverlapping(60) // 1 hour lock
->name('short-lock-task');
$schedule->call($fn)
->withoutOverlapping(4320) // 3 day lock
->name('long-lock-task');
Lock Key Format
Lock keys are stored as WordPress transients with the format:
scheduler_lock_{md5($taskName)}
If a task has no name, the key is derived from:
- Callbacks:
task_+spl_object_hash - Commands/Jobs:
task_+md5(serialize($callback))
WP-Cron Configuration
Disable WordPress Default Cron
For production, disable WordPress's traffic-based cron trigger:
// wp-config.php
define('DISABLE_WP_CRON', true);
System Cron Setup
Replace with a system-level cron job:
# Hit wp-cron.php every minute via HTTP
* * * * * curl -s https://yoursite.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1
# Or using wget
* * * * * wget -qO- https://yoursite.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1
# Or via WP-CLI (recommended)
* * * * * cd /path/to/wordpress && wp cron event run --due-now > /dev/null 2>&1
WordPress Actions
The scheduler fires the following WordPress actions:
| Action | Parameters | Description |
|---|---|---|
wpzylos_scheduler_task_failed | ScheduledTask $task, Throwable $e | Fired when a task throws an exception |
Listening for Failures
add_action('wpzylos_scheduler_task_failed', function ($task, $e) {
// Log to file
error_log(sprintf(
'[Scheduler] Task "%s" failed: %s in %s:%d',
$task->getName(),
$e->getMessage(),
$e->getFile(),
$e->getLine()
));
// Send admin notification for critical tasks
if (str_starts_with($task->getName(), 'critical-')) {
wp_mail(
get_option('admin_email'),
"Scheduler Task Failed: {$task->getName()}",
$e->getMessage()
);
}
}, 10, 2);
WordPress Filters
| Filter | Parameters | Description |
|---|---|---|
wpzylos_dispatch_job | bool $dispatched, object $job | Used by job tasks to dispatch through the queue system |