Advertisement
lemb

Untitled

Nov 28th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.01 KB | None | 0 0
  1. <?php
  2.  
  3. class Cron
  4. {
  5.     private $fileName;
  6.  
  7.     protected static $instance;
  8.  
  9.     public static function init($fileName = 'cache_cron')
  10.     {
  11.         if(!self::$instance) {
  12.             self::$instance = new self( $fileName );
  13.         }
  14.  
  15.         return self::$instance;
  16.     }
  17.  
  18.     private function __construct($fileName)
  19.     {
  20.         $this->fileName = $fileName;
  21.     }
  22.  
  23.     private function __clone() {}
  24.  
  25.     public function needExecute($time)
  26.     {
  27.         $fileName = __DIR__ . DIRECTORY_SEPARATOR . $this->fileName;
  28.         return !(file_exists($fileName) && ((filemtime($this->fileName) + $time) >= time()));
  29.     }
  30.  
  31.     public function isExecuted()
  32.     {
  33.         $fileName = __DIR__ . DIRECTORY_SEPARATOR . $this->fileName;
  34.         touch($fileName);
  35.     }
  36. }
  37.  
  38. // 5 - кол секунд для повторного выполнения
  39. if( Cron::init()->needExecute(5) ) {
  40.     var_dump('Run baby run');
  41.  
  42.  
  43.     // сообщаем крону что он выполнился и должен обновить время последнего срабатывания
  44.     Cron::init()->isExecuted();
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement