PHP Classes

PHP Cron Job Lock: Manage lock files while a cron job is running

Recommend this page to a friend!
  Info   View files Documentation   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 176 This week: 1All time: 8,761 This week: 560Up
Version License PHP version Categories
cron-task 4.1.7The PHP License7PHP 5, Language
Description 

Author

This class can manage lock files while a cron job is running.

It extends the loader class to provide functions that use file locks to check if a given cron task runs so the job does not run multiple times.

The class can also unlock the file to allow the same cron job to run again when it is time.

Innovation Award
PHP Programming Innovation award winner
October 2021
Winner


Prize: One official elePHPant Plush Mascott
Cron a is a popular program that is available in UNIX-based operating systems like Linux. It is capable of running other programs at specific schedules.

This capability of the cron program is helpful to execute tasks that need to be done regularly, like, for instance, creating a backup copy of a database.

Some tasks cannot be executed more than once at the same time. One way to assure that a job runs only once at a time is to use file locks.

This package uses file locks to prevent that the same task executes more than once.

Manuel Lemos
Picture of Slawomir Kaleta
  Performance   Level  
Name: Slawomir Kaleta <contact>
Classes: 15 packages by
Country: Poland Poland
Age: ???
All time rank: 233054 in Poland Poland
Week rank: 33 Up2 in Poland Poland Up
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

Documentation

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

  Files folder image Files  
File Role Description
Files folder imageExceptions (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Plain text file Task.php Class Class source

  Files folder image Files  /  Exceptions  
File Role Description
  Plain text file TaskException.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:176
This week:1
All time:8,761
This week:560Up