dframe / cron
Cron component Dframe
v4.2.0
2022-10-28 12:21 UTC
Requires
- php: >=7.3
- ext-json: *
- dframe/loader: ^v4.1
Requires (Dev)
- mikey179/vfsstream: ^1.6
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-12-05 13:40:24 UTC
README
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'], []);