PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Slawomir Kaleta   PHP Cron Job Lock   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP Cron Job Lock
Manage lock files while a cron job is running
Author: By
Last change:
Date: 2 years ago
Size: 2,328 bytes
 

Contents

Class file image Download

Dframe/Cron - Component

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'], []);