2017-05-01 16:32:30 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Lock;
|
|
|
|
|
|
|
|
use Psr\Log\LoggerAwareInterface;
|
|
|
|
use Psr\Log\LoggerAwareTrait;
|
|
|
|
use Psr\Log\NullLogger;
|
|
|
|
use Symfony\Component\Lock\Exception\InvalidArgumentException;
|
|
|
|
use Symfony\Component\Lock\Exception\LockAcquiringException;
|
|
|
|
use Symfony\Component\Lock\Exception\LockConflictedException;
|
2017-04-26 21:57:15 +01:00
|
|
|
use Symfony\Component\Lock\Exception\LockExpiredException;
|
2017-05-01 16:32:30 +01:00
|
|
|
use Symfony\Component\Lock\Exception\LockReleasingException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lock is the default implementation of the LockInterface.
|
|
|
|
*
|
|
|
|
* @author Jérémy Derussé <jeremy@derusse.com>
|
|
|
|
*/
|
|
|
|
final class Lock implements LockInterface, LoggerAwareInterface
|
|
|
|
{
|
|
|
|
use LoggerAwareTrait;
|
|
|
|
|
|
|
|
private $store;
|
|
|
|
private $key;
|
|
|
|
private $ttl;
|
2017-03-23 20:40:19 +00:00
|
|
|
private $autoRelease;
|
|
|
|
private $dirty = false;
|
2017-05-01 16:32:30 +01:00
|
|
|
|
|
|
|
/**
|
2017-03-23 20:40:19 +00:00
|
|
|
* @param Key $key Resource to lock
|
|
|
|
* @param StoreInterface $store Store used to handle lock persistence
|
|
|
|
* @param float|null $ttl Maximum expected lock duration in seconds
|
|
|
|
* @param bool $autoRelease Whether to automatically release the lock or not when the lock instance is destroyed
|
2017-05-01 16:32:30 +01:00
|
|
|
*/
|
2017-10-28 19:15:32 +01:00
|
|
|
public function __construct(Key $key, StoreInterface $store, float $ttl = null, bool $autoRelease = true)
|
2017-05-01 16:32:30 +01:00
|
|
|
{
|
|
|
|
$this->store = $store;
|
|
|
|
$this->key = $key;
|
|
|
|
$this->ttl = $ttl;
|
2017-10-28 19:15:32 +01:00
|
|
|
$this->autoRelease = $autoRelease;
|
2017-05-01 16:32:30 +01:00
|
|
|
|
|
|
|
$this->logger = new NullLogger();
|
|
|
|
}
|
|
|
|
|
2017-03-23 20:40:19 +00:00
|
|
|
/**
|
|
|
|
* Automatically releases the underlying lock when the object is destructed.
|
|
|
|
*/
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
if (!$this->autoRelease || !$this->dirty || !$this->isAcquired()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->release();
|
|
|
|
}
|
|
|
|
|
2017-05-01 16:32:30 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function acquire($blocking = false)
|
|
|
|
{
|
|
|
|
try {
|
2018-09-13 19:01:11 +01:00
|
|
|
if ($blocking) {
|
2017-05-01 16:32:30 +01:00
|
|
|
$this->store->waitAndSave($this->key);
|
2018-09-13 19:01:11 +01:00
|
|
|
} else {
|
|
|
|
$this->store->save($this->key);
|
2017-05-01 16:32:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-23 20:40:19 +00:00
|
|
|
$this->dirty = true;
|
2017-05-01 16:32:30 +01:00
|
|
|
$this->logger->info('Successfully acquired the "{resource}" lock.', array('resource' => $this->key));
|
|
|
|
|
|
|
|
if ($this->ttl) {
|
|
|
|
$this->refresh();
|
|
|
|
}
|
|
|
|
|
2017-04-26 21:57:15 +01:00
|
|
|
if ($this->key->isExpired()) {
|
|
|
|
throw new LockExpiredException(sprintf('Failed to store the "%s" lock.', $this->key));
|
|
|
|
}
|
|
|
|
|
2017-05-01 16:32:30 +01:00
|
|
|
return true;
|
|
|
|
} catch (LockConflictedException $e) {
|
2017-03-23 20:40:19 +00:00
|
|
|
$this->dirty = false;
|
2018-02-02 12:35:25 +00:00
|
|
|
$this->logger->notice('Failed to acquire the "{resource}" lock. Someone else already acquired the lock.', array('resource' => $this->key));
|
2017-05-01 16:32:30 +01:00
|
|
|
|
|
|
|
if ($blocking) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} catch (\Exception $e) {
|
2018-02-02 12:35:25 +00:00
|
|
|
$this->logger->notice('Failed to acquire the "{resource}" lock.', array('resource' => $this->key, 'exception' => $e));
|
2017-05-01 16:32:30 +01:00
|
|
|
throw new LockAcquiringException(sprintf('Failed to acquire the "%s" lock.', $this->key), 0, $e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-02-19 12:56:49 +00:00
|
|
|
public function refresh($ttl = null)
|
2017-05-01 16:32:30 +01:00
|
|
|
{
|
2018-02-19 12:56:49 +00:00
|
|
|
if (null === $ttl) {
|
|
|
|
$ttl = $this->ttl;
|
|
|
|
}
|
|
|
|
if (!$ttl) {
|
2017-05-01 16:32:30 +01:00
|
|
|
throw new InvalidArgumentException('You have to define an expiration duration.');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-04-26 21:57:15 +01:00
|
|
|
$this->key->resetLifetime();
|
2018-02-19 12:56:49 +00:00
|
|
|
$this->store->putOffExpiration($this->key, $ttl);
|
2017-03-23 20:40:19 +00:00
|
|
|
$this->dirty = true;
|
2017-04-26 21:57:15 +01:00
|
|
|
|
|
|
|
if ($this->key->isExpired()) {
|
|
|
|
throw new LockExpiredException(sprintf('Failed to put off the expiration of the "%s" lock within the specified time.', $this->key));
|
|
|
|
}
|
|
|
|
|
2018-02-19 12:56:49 +00:00
|
|
|
$this->logger->info('Expiration defined for "{resource}" lock for "{ttl}" seconds.', array('resource' => $this->key, 'ttl' => $ttl));
|
2017-05-01 16:32:30 +01:00
|
|
|
} catch (LockConflictedException $e) {
|
2017-03-23 20:40:19 +00:00
|
|
|
$this->dirty = false;
|
2018-02-02 12:35:25 +00:00
|
|
|
$this->logger->notice('Failed to define an expiration for the "{resource}" lock, someone else acquired the lock.', array('resource' => $this->key));
|
2017-05-01 16:32:30 +01:00
|
|
|
throw $e;
|
|
|
|
} catch (\Exception $e) {
|
2018-02-02 12:35:25 +00:00
|
|
|
$this->logger->notice('Failed to define an expiration for the "{resource}" lock.', array('resource' => $this->key, 'exception' => $e));
|
2017-05-01 16:32:30 +01:00
|
|
|
throw new LockAcquiringException(sprintf('Failed to define an expiration for the "%s" lock.', $this->key), 0, $e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function isAcquired()
|
|
|
|
{
|
2017-03-23 20:40:19 +00:00
|
|
|
return $this->dirty = $this->store->exists($this->key);
|
2017-05-01 16:32:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function release()
|
|
|
|
{
|
|
|
|
$this->store->delete($this->key);
|
2017-03-23 20:40:19 +00:00
|
|
|
$this->dirty = false;
|
2017-05-01 16:32:30 +01:00
|
|
|
|
|
|
|
if ($this->store->exists($this->key)) {
|
2018-02-02 12:35:25 +00:00
|
|
|
$this->logger->notice('Failed to release the "{resource}" lock.', array('resource' => $this->key));
|
2017-05-01 16:32:30 +01:00
|
|
|
throw new LockReleasingException(sprintf('Failed to release the "%s" lock.', $this->key));
|
|
|
|
}
|
|
|
|
}
|
2017-04-26 23:02:52 +01:00
|
|
|
|
|
|
|
/**
|
2017-09-24 16:01:32 +01:00
|
|
|
* {@inheritdoc}
|
2017-04-26 23:02:52 +01:00
|
|
|
*/
|
|
|
|
public function isExpired()
|
|
|
|
{
|
2017-04-26 21:57:15 +01:00
|
|
|
return $this->key->isExpired();
|
2017-04-26 23:02:52 +01:00
|
|
|
}
|
|
|
|
|
2017-04-26 21:57:15 +01:00
|
|
|
/**
|
2017-09-24 16:01:32 +01:00
|
|
|
* {@inheritdoc}
|
2017-04-26 21:57:15 +01:00
|
|
|
*/
|
|
|
|
public function getRemainingLifetime()
|
2017-04-26 23:02:52 +01:00
|
|
|
{
|
2017-04-26 21:57:15 +01:00
|
|
|
return $this->key->getRemainingLifetime();
|
2017-04-26 23:02:52 +01:00
|
|
|
}
|
2017-05-01 16:32:30 +01:00
|
|
|
}
|