Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Cron
- {
- private $fileName;
- protected static $instance;
- public static function init($fileName = 'cache_cron')
- {
- if(!self::$instance) {
- self::$instance = new self( $fileName );
- }
- return self::$instance;
- }
- private function __construct($fileName)
- {
- $this->fileName = $fileName;
- }
- private function __clone() {}
- public function needExecute($time)
- {
- $fileName = __DIR__ . DIRECTORY_SEPARATOR . $this->fileName;
- return !(file_exists($fileName) && ((filemtime($this->fileName) + $time) >= time()));
- }
- public function isExecuted()
- {
- $fileName = __DIR__ . DIRECTORY_SEPARATOR . $this->fileName;
- touch($fileName);
- }
- }
- // 5 - кол секунд для повторного выполнения
- if( Cron::init()->needExecute(5) ) {
- var_dump('Run baby run');
- // сообщаем крону что он выполнился и должен обновить время последнего срабатывания
- Cron::init()->isExecuted();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement