Crontab Job

A crontab job allows you to automatically execute code at pre-defined time intervals. You may create a crontab job by using the create crontab CLI command, such as:

apex create crontab <PACKAGE> <ALIAS>

apex create crontab my-shop process-recurring

The above example will create a new file at /src/MyShop/Opus/Crontabs/ProcessRecurring.php, with one method ofprocess()`, and three properties which are explained below.

Property Description
$auto_run Boolean that defines whether or not this crontab job should be automatically executed.
$interval How often to execute the crontab job (eg. D1 = 1 day, W2 = 2 weeks, M3 = 3 months, Q2 = semi-anually).
$description Description of the crontab job.

Below shows an example crontab class.

<?php
declare(strict_types = 1);

namespace App\MyShop\Opus\Crontabs;

use Apex\App\Interfaces\Opus\CrontabInterface;
use Apex\App\Cli\Cli;
use App\MyShop\Opus\Cli\RecurringPayments;

/**
 * Crontab job - ProcessRecurring
 */
class ProcessRecurring implements CrontabInterface
{

    #[Inject(RecurringPayments::class)]
    private RecurringPayments $rec_payments;

    #[Inject(Cli::class)]
    private Cli $cli;

    /**
     * Whether or not to automatically execute this crontab job.
     */
    public bool $auto_run = true;

    /**
     * How often to run this crontab job.  
     * Must be formatted as the period letter followed by the number.
     *
     * For the period, I = Minute, H = Hour, D = Day, W = Week, M = Month, Q = Quarter, Y = Year
     *
     * For example, I30 = every 30 minutes, H3 = every 3 hours, W2 = every 2 weeks, et al.
     */
    public string $interval = 'D1';

    /**
     * Description of the crontab job
     */
    public string $description = 'Process Recurring Payments';


    /**
     * Process the crontab job
     */
    public function process():void
    {

        // Process payments
        $this->rec_payments->process($this->cli, []);

    }

}

The above process() method will be automatically executed every day as defined by the $interval property.

Execute Crontab Jobs Manually

You may manually process all pending crontab jobs with the crontab CLI command without any arguments, such as:

apex crontab

This will go through all crontab jobs installed on the system, check whether or not they're due to be executed, and if yes will execute them. You may also force execution of a single crontab job with the command:

apex crontab <PACKAGE> <ALIAS>

apex crontab my-shop process-recurring

This will execute the specified crontab job regardless whether or not it's due.