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:

BindingClassTypeDescription
Schedule::classScheduleSingletonPrimary schedule instance
'scheduler'ScheduleSingletonAlias 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

PropertyDefaultDescription
intervalSeconds3600 (hourly)Default interval when no frequency is set
atTimenullNo specific time constraint
dayOfWeeknullNo day-of-week constraint
dayOfMonthnullNo day-of-month constraint
atMinutenullNo minute constraint

Lock Configuration

PropertyDefaultDescription
preventOverlapfalseOverlap prevention disabled by default
lockExpiresAt1440 (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:

ActionParametersDescription
wpzylos_scheduler_task_failedScheduledTask $task, Throwable $eFired 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

FilterParametersDescription
wpzylos_dispatch_jobbool $dispatched, object $jobUsed by job tasks to dispatch through the queue system