PHP Classes


Recommend this page to a friend!
  Classes of Slawomir Kaleta   PHP Cron Job Lock   Download  
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: 1 year ago
Size: 2,328 bytes


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 require dframe/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;

ini_set('max_execution_time', 0);

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);




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