protected function execute(\JRegistry $args, Status $status) : void

Fires to execute the scheduled cron job.


Description

This is the action that the cron jobs should perform when the scheduled interval is satisfied.

It is possible to throw an exception to safely abort the cron execution. Whenever a cron jobs terminates with an exception (or a throwable error), the registered logs are always notified via mail to the administrator.

This method should not contain any exit or die function, otherwise the job won't be properly terminated.


Parameters

$args

(JRegistry)  The configured cron job settings.

$status

(Status)  The object used to log the execution status. This class is part of the E4J\VikRestaurants\Cron namespace.

Return Value

None.


Example

/**
 * @inheritDoc
 * 
 * IMPORTANT: you first need to load the E4J\VikRestaurants\Cron\Status object accordingly.
 */
protected function execute(\JRegistry $args, Status $status)
{
    // recover settings from cron configuration
    $paramValue = $args->get(
        'foo', // this is the parameter form name
        '' // this is the default value to use in case the setting is missing or blank
    );

    // use this method if you need to recover the unique ID of the cron job
    $id = $this->getId();

    // set some logs
    $status->setContent('First line of logs.');
    // append some logs at the end (it is possible to chain the methods and to specify a different separator)
    $status->appendContent('Second line')->appendContent('of logs.', ' ');
    // prepend some logs at the beginning
    $status->prependContent('Displayed before the first line.');

    // set the execution status (true: success; false: failure)
    $status->setStatus(true);

    // choose whether the logs should be notified via mail to the administrator (false by default)
    $status->setNotify(true);

    // It is also possible to register some metadata to improve the logs rendering.
    // In this example we show how to register a metadata for the latest table reservation.

    $db = JFactory::getDbo();

    // load primary key of the last reservation
    $db->setQuery(
        $db->getQuery(true)
            ->select('MAX(' . $db->qn('id') . ')')
            ->from($db->qn('#__vikrestaurants_reservation')),
    );

    if ($resId = $db->loadResult()) {
        // register log metadata
        $status->addMetadata(new \E4J\VikRestaurants\Metadata\Addon\RestaurantReservationDetails($resId));
    }
}
Last Update: 23 hours ago.
Helpful?