Exemple #1
0
 /**
  * @expectedException \RuntimeException
  * @expectedExceptionMessage Cannot obtain the lock for id "5669dd8f56c02c4628031635". Timeout after 2 seconds
  */
 public function testWaitForLockIsNotReleased()
 {
     $lockCollection = $this->database->selectCollection('storage_lock_test');
     $pessimisticLock = new PessimisticLock($lockCollection);
     $pessimisticLock->createIndexes();
     $pessimisticLock->lock('5669dd8f56c02c4628031635');
     $pessimisticLock->lock('5669dd8f56c02c4628031635', 2);
 }
Exemple #2
0
 /**
  * @param $id
  * @param callable $lockCallback
  */
 public function lock($id, callable $lockCallback)
 {
     if (false == $this->pessimisticLock) {
         throw new \LogicException('Cannot lock. The PessimisticLock instance is not injected');
     }
     $this->pessimisticLock->lock($id);
     try {
         if ($model = $this->findOne(['_id' => new ObjectID((string) $id)])) {
             call_user_func($lockCallback, $model, $this);
         }
     } finally {
         $this->pessimisticLock->unlock($id);
     }
 }