Tempest is still a work in progress. Visit our GitHub or Discord

Scheduling

tempest/console comes with a built-in scheduler to run commands repeatedly in the background. You can schedule console commands, as well as plain functions that aren't directly accessible via the console.

In order for the scheduler to run, you'll have to configure a single cron job on your server:

0 * * * * user /path/to/{*tempest schedule:run*}

You can manually trigger a schedule run as well:

./tempest schedule:run

Scheduling

Any method using the #[Schedule] attribute will be run by the scheduler. As with everything Tempest, these methods are discovered automatically.

// app/Jobs.php

use Tempest\Console\Schedule;
use Tempest\Console\Scheduler\Every;

final readonly class Jobs
{
    #[Schedule(Every::HOUR)]
    public function syncRss(): void
    {
        // …   
    }
}

For most common scheduling use-cases, the Every enum can be used:

#[Schedule(Every::MINUTE)]
#[Schedule(Every::QUARTER)]
#[Schedule(Every::HALF_HOUR)]
#[Schedule(Every::HOUR)]
#[Schedule(Every::TWELVE_HOURS)]
#[Schedule(Every::DAY)]
#[Schedule(Every::WEEK)]
#[Schedule(Every::MONTH)]

In case you need more fine-grained control, you can pass in an Interval object instead:

use Tempest\Console\Schedule;
use Tempest\Console\Scheduler\Interval;

#[Schedule(new Interval(hours: 2, minutes: 30))]
public function syncRss(): void
{
    // …   
}

Keep in mind that scheduled task don't have to be console commands, but they can be both if you need to run a task both manually, and a scheduled background task.

use Tempest\Console\ConsoleCommand;
use Tempest\Console\Schedule;
use Tempest\Console\Scheduler\Interval;

#[Schedule(Every::HOUR)]
#[ConsoleCommand('rss:sync')]
public function syncRss(): void
{
    // …   
}

Output and logging

Any scheduled task can inject Console and write to it as if it's running as a normal console command:

// app/Jobs.php

use Tempest\Console\HasConsole;
use Tempest\Console\Schedule;
use Tempest\Console\Scheduler\Every;

final readonly class Jobs
{
    use HasConsole;
    
    #[Schedule(Every::HOUR)]
    public function syncRss(): void
    {
        $this->console->writeln('Starting RSS sync…');
        
        // …
        
        $this->console->success('Done');
    }
}

The output of scheduled tasks is written to a log file, by default schedule.log in your project's root.