Skip to content

#[AsCron] ​

Mark a class as a recurring job. Action Scheduler runs it on a schedule.

Signature ​

php
#[Attribute(Attribute::TARGET_CLASS)]
final readonly class AsCron
{
    public ?int $intervalSeconds;

    public ?string $expression;

    public function __construct(
        public CronInterval|int|string $schedule,
        public string $group = 'foehn',
        public ?string $hook = null,
    ) {}
}

Parameters ​

ParameterTypeDefaultDescription
scheduleCronInterval|int|string—An interval (CronInterval case or seconds) or a cron expression (required)
groupstring'foehn'The Action Scheduler group, to filter in the admin
hook?stringnullThe action hook. null makes it from the class name: App\Jobs\SyncStock gives foehn/app/jobs/sync_stock

Properties ​

The constructor sets two read-only properties from schedule:

PropertyTypeValue
intervalSeconds?intThe interval in seconds, or null when schedule is a cron expression
expression?stringThe cron expression with single spaces, or null when it is an interval

CronInterval ​

Studiometa\Foehn\Jobs\CronInterval is an integer enum:

CaseSeconds
Hourly3600
TwiceDaily43200
Daily86400
Weekly604800

Requirements ​

  • The class has a public __invoke() method with no required arguments. The constructor can receive services from the container.
  • A cron expression has five fields: minute, hour, day of month, month and day of week. It runs in the site timezone.
  • Action Scheduler is active. Without it, Føhn schedules nothing.

When the class has no public __invoke(), or the expression is not valid, Føhn does not schedule it and wp foehn inspect problems reports it.

Usage ​

php
<?php

namespace App\Jobs;

use Studiometa\Foehn\Attributes\AsCron;
use Studiometa\Foehn\Jobs\CronInterval;

#[AsCron(CronInterval::Daily)]
final class PurgeExpiredExports
{
    public function __invoke(): void
    {
        // …
    }
}

// Every 300 seconds, in its own group
#[AsCron(300, group: 'inventory')]
final class SyncStock
{
    public function __invoke(): void {}
}

// At 06:30 from Monday to Friday, in the site timezone
#[AsCron('30 6 * * 1-5')]
final class SendDigest
{
    public function __invoke(): void {}
}

Føhn is released under the GPL-3.0-or-later license. The starter, the installer and the Vite plugin are MIT.