Esempio n. 1
0
 public static function lock()
 {
     self::$lock_file = self::$lock_dir . self::$lid . '.lock';
     self::$lock_data_file = self::$lock_dir . self::$lid . '.dat';
     self::$log_file = self::$lock_dir . self::$lid . '.log';
     self::$pid = getmypid();
     self::$start = microtime(true);
     if (file_exists(self::$lock_data_file)) {
         self::$data = unserialize(file_get_contents(self::$lock_data_file));
     }
     if (file_exists(self::$lock_file)) {
         $contents = file_get_contents(self::$lock_file);
         list(self::$last_pid, $old_timestamp) = explode(PHP_EOL, $contents);
         if ($old_timestamp + self::$timeout * 60 < time()) {
             self::$lock_aquired = true;
             self::log('a previous process timed out:' . self::$last_pid);
             self::log('lock aquired. starting');
         } else {
             self::$lock_aquired = false;
             self::log('exiting. overlapped with ' . self::$last_pid);
         }
     } else {
         self::$lock_aquired = true;
         self::log('lock aquired. starting');
     }
     if (self::$lock_aquired) {
         $h = fopen(self::$lock_file, 'w');
         fwrite($h, self::$pid . PHP_EOL . time());
     }
     return self::$lock_aquired;
 }