public function testIsLocked()
 {
     // isLocked() should report lock status accurately.
     $file = new TempFile();
     $lock = PhutilFileLock::newForPath($file);
     $this->assertFalse($lock->isLocked());
     $lock->lock();
     $this->assertTrue($lock->isLocked());
     $lock->unlock();
     $this->assertFalse($lock->isLocked());
 }
 /**
  * @task storage
  */
 private function lockCache($wait = 0)
 {
     if ($this->lock) {
         throw new Exception('Trying to lockCache() with a lock!');
     }
     if (!Filesystem::pathExists($this->getCacheDirectory())) {
         Filesystem::createDirectory($this->getCacheDirectory(), 0755, true);
     }
     $lock = PhutilFileLock::newForPath($this->getCacheDirectory() . '.lock');
     $lock->lock($wait);
     $this->lock = $lock;
 }
 /**
  * @task storage
  */
 private function loadCache($hold_lock)
 {
     if ($this->lock) {
         throw new Exception('Trying to loadCache() with a lock!');
     }
     $lock = PhutilFileLock::newForPath($this->getCacheFile() . '.lock');
     $lock->lock();
     try {
         $this->cache = array();
         if (Filesystem::pathExists($this->getCacheFile())) {
             $cache = unserialize(Filesystem::readFile($this->getCacheFile()));
             if ($cache) {
                 $this->cache = $cache;
             }
         }
     } catch (Exception $ex) {
         $lock->unlock();
         throw $ex;
     }
     if ($hold_lock) {
         $this->lock = $lock;
     } else {
         $lock->unlock();
     }
 }
Пример #4
0
$args->setSynopsis(<<<EOHELP
**lock.php** __file__ [__options__]
    Acquire a lockfile and hold it until told to unlock it.

EOHELP
);
$args->parseStandardArguments();
$args->parse(array(array('name' => 'test', 'help' => 'Instead of holding the lock, release it and exit.'), array('name' => 'hold', 'help' => 'Hold indefinitely without prompting.'), array('name' => 'wait', 'param' => 'n', 'help' => 'Block for up to __n__ seconds waiting for the lock.', 'default' => 0), array('name' => 'file', 'wildcard' => true)));
$file = $args->getArg('file');
if (count($file) != 1) {
    $args->printHelpAndExit();
}
$file = head($file);
$console = PhutilConsole::getConsole();
$console->writeOut("This process has PID %d. Acquiring lock...\n", getmypid());
$lock = PhutilFileLock::newForPath($file);
try {
    $lock->lock($args->getArg('wait'));
} catch (PhutilFileLockException $ex) {
    $console->writeOut("**UNABLE TO ACQUIRE LOCK:** Lock is already held.\n");
    exit(1);
}
// NOTE: This string is magic, the unit tests look for it.
$console->writeOut("LOCK ACQUIRED\n");
if ($args->getArg('test')) {
    $lock->unlock();
    exit(0);
}
if ($args->getArg('hold')) {
    while (true) {
        sleep(1);
 /**
  * @task storage
  */
 private function loadCache($hold_lock)
 {
     if ($this->lock) {
         throw new Exception(pht('Trying to %s with a lock!', __FUNCTION__ . '()'));
     }
     $lock = PhutilFileLock::newForPath($this->getCacheFile() . '.lock');
     try {
         $lock->lock($this->wait);
     } catch (PhutilLockException $ex) {
         if ($hold_lock) {
             throw $ex;
         } else {
             $this->cache = array();
             return;
         }
     }
     try {
         $this->cache = array();
         if (Filesystem::pathExists($this->getCacheFile())) {
             $cache = unserialize(Filesystem::readFile($this->getCacheFile()));
             if ($cache) {
                 $this->cache = $cache;
             }
         }
     } catch (Exception $ex) {
         $lock->unlock();
         throw $ex;
     }
     if ($hold_lock) {
         $this->lock = $lock;
     } else {
         $lock->unlock();
     }
 }