dframe/cron

Cron component Dframe

v4.1.8 2022-01-30 11:34 UTC

This package is auto-updated.

Last update: 2022-11-28 12:14:30 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

php framework dframe logo

Documentation - Cron PHP

Composer

$ composer require dframe/cron

Cron

Cron is a service to perform tasks periodically. It allows you to execute a command at a specified time. Sending emails is such an example.

use Dframe\Cron\Task;
use Dframe\Router\Response;

set_time_limit(0);
ini_set('max_execution_time', 0);
date_default_timezone_set('Europe/Warsaw');

require_once dirname(__DIR__) . '/../../../vendor/autoload.php';
require_once dirname(__DIR__) . '/../../../web/config.php';

/**
 * An anonymous Cron class that calls itself
 */
return (new class() extends Task
{

    /**
     * Init function
     *
     * @return array
     */
    public function init()
    {
        $cron = $this->inLock('mail', [$this->loadModel('Mail/Mail'), 'sendMails'], []);
        if ($cron['return'] == true) {
            $mail = $cron['response'];
            return Response::renderJSON(['code' => 200, 'message' => 'Cron Complete', 'data' => ['mail' => ['data' => $mail['response']]]]);
        }

        return Response::renderJSON(['code' => 403, 'message' => 'Cron in Lock'])->status(403);

    }

})->init()->display();

Methods

lockTime(string $key, $ttl = 3600)

Lock time

if ($this->lockTime('mail')) {
    return Response::renderJSON(['code' => 403, 'message' => 'Time Lock'])->status(403);
}

inLock(string $key, object $loadModel, string $method, $args = [], $ttl = 3600)

This method has a built-in method that blocks it until complete.

$this->inLock('mail', [$this->loadModel('Mail/Mail'), 'sendMails'], []);