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)    
Last Updated Ratings Unique User Downloads Download Rankings
2022-01-30 (13 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 164 This week: 2All time: 8,803 This week: 75Up
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
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
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

Details

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:164
This week:2
All time:8,803
This week:75Up
For more information send a message to info at phpclasses dot org.