Traits

HasCron

v1.0.0

Adds scheduleCron, scheduleOnce, unscheduleCron, and isCronScheduled helpers on top of SchedulerServiceInterface.

Use Trait

use Meteorack\Sdk\RuntimeWp\Traits\HasCron;

How To Use It

  • Use the trait helpers when you are already inside an AbstractModule and do not need to interact with SchedulerServiceInterface directly.
  • Guard recurring jobs with isCronScheduled() and clean them up with unscheduleCron() during deactivation or uninstall flows.

Helpers

public function scheduleCron(string $hook, string $recurrence, array $args = []): void

Schedules a recurring cron job for the module.

public function unscheduleCron(string $hook): void

Unschedules a cron hook.

public function scheduleOnce(string $hook, int $timestamp, array $args = []): void

Schedules a one-off cron job.

public function isCronScheduled(string $hook): bool

Checks whether a cron hook is already scheduled.

Example

bootstrap.php
public function onSdkReady(SdkContext $ctx): void
{
    parent::onSdkReady($ctx);

    if (! $this->isCronScheduled('reports_sync')) {
        $this->scheduleCron('reports_sync', 'hourly');
    }

    $this->scheduleOnce('reports_cleanup', time() + 3600);
}