.github
2 years ago
CannotObtainLockException.php
4 years ago
FsConnectionFactory.php
4 years ago
FsConsumer.php
4 years ago
FsContext.php
4 years ago
FsDestination.php
4 years ago
FsMessage.php
4 years ago
FsProducer.php
4 years ago
LICENSE
4 years ago
LegacyFilesystemLock.php
4 years ago
Lock.php
4 years ago
README.md
2 years ago
composer.json
2 years ago
LegacyFilesystemLock.php
186 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Enqueue\Fs; |
| 4 | |
| 5 | use Symfony\Component\Filesystem\Exception\IOException; |
| 6 | use Symfony\Component\Filesystem\Filesystem; |
| 7 | |
| 8 | class LegacyFilesystemLock implements Lock |
| 9 | { |
| 10 | /** |
| 11 | * Array key is a destination name. String. |
| 12 | * |
| 13 | * @var LockHandler[] |
| 14 | */ |
| 15 | private $lockHandlers; |
| 16 | |
| 17 | public function __construct() |
| 18 | { |
| 19 | $this->lockHandlers = []; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * {@inheritdoc} |
| 24 | */ |
| 25 | public function lock(FsDestination $destination) |
| 26 | { |
| 27 | $lockHandler = $this->getLockHandler($destination); |
| 28 | |
| 29 | if (false == $lockHandler->lock(true)) { |
| 30 | throw new CannotObtainLockException(sprintf('Cannot obtain the lock for destination %s', $destination->getName())); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * {@inheritdoc} |
| 36 | */ |
| 37 | public function release(FsDestination $destination) |
| 38 | { |
| 39 | $lockHandler = $this->getLockHandler($destination); |
| 40 | |
| 41 | $lockHandler->release(); |
| 42 | } |
| 43 | |
| 44 | public function releaseAll() |
| 45 | { |
| 46 | foreach ($this->lockHandlers as $lockHandler) { |
| 47 | $lockHandler->release(); |
| 48 | } |
| 49 | |
| 50 | $this->lockHandlers = []; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param FsDestination $destination |
| 55 | * |
| 56 | * @return LockHandler |
| 57 | */ |
| 58 | private function getLockHandler(FsDestination $destination) |
| 59 | { |
| 60 | if (false == isset($this->lockHandlers[$destination->getName()])) { |
| 61 | $this->lockHandlers[$destination->getName()] = new LockHandler( |
| 62 | $destination->getName(), |
| 63 | $destination->getFileInfo()->getPath() |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | return $this->lockHandlers[$destination->getName()]; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // symfony/lock component works only with 3.x and 4.x Symfony |
| 72 | // For symfony 2.x we should use LockHandler from symfony/component which was removed from 4.x |
| 73 | // because we cannot use both at the same time. I copied and pasted the lock handler here |
| 74 | |
| 75 | /* |
| 76 | * This file is part of the Symfony package. |
| 77 | * |
| 78 | * (c) Fabien Potencier <fabien@symfony.com> |
| 79 | * |
| 80 | * For the full copyright and license information, please view the LICENSE |
| 81 | * file that was distributed with this source code. |
| 82 | */ |
| 83 | |
| 84 | /** |
| 85 | * LockHandler class provides a simple abstraction to lock anything by means of |
| 86 | * a file lock. |
| 87 | * |
| 88 | * A locked file is created based on the lock name when calling lock(). Other |
| 89 | * lock handlers will not be able to lock the same name until it is released |
| 90 | * (explicitly by calling release() or implicitly when the instance holding the |
| 91 | * lock is destroyed). |
| 92 | * |
| 93 | * @author Grégoire Pineau <lyrixx@lyrixx.info> |
| 94 | * @author Romain Neutron <imprec@gmail.com> |
| 95 | * @author Nicolas Grekas <p@tchwork.com> |
| 96 | * |
| 97 | * @deprecated since version 3.4, to be removed in 4.0. Use Symfony\Component\Lock\Store\SemaphoreStore or Symfony\Component\Lock\Store\FlockStore instead. |
| 98 | */ |
| 99 | class LockHandler |
| 100 | { |
| 101 | private $file; |
| 102 | private $handle; |
| 103 | |
| 104 | /** |
| 105 | * @param string $name The lock name |
| 106 | * @param string|null $lockPath The directory to store the lock. Default values will use temporary directory |
| 107 | * |
| 108 | * @throws IOException If the lock directory could not be created or is not writable |
| 109 | */ |
| 110 | public function __construct($name, $lockPath = null) |
| 111 | { |
| 112 | $lockPath = $lockPath ?: sys_get_temp_dir(); |
| 113 | |
| 114 | if (!is_dir($lockPath)) { |
| 115 | $fs = new Filesystem(); |
| 116 | $fs->mkdir($lockPath); |
| 117 | } |
| 118 | |
| 119 | if (!is_writable($lockPath)) { |
| 120 | throw new IOException(sprintf('The directory "%s" is not writable.', $lockPath), 0, null, $lockPath); |
| 121 | } |
| 122 | |
| 123 | $this->file = sprintf('%s/sf.%s.%s.lock', $lockPath, preg_replace('/[^a-z0-9\._-]+/i', '-', $name), hash('sha256', $name)); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Lock the resource. |
| 128 | * |
| 129 | * @param bool $blocking Wait until the lock is released |
| 130 | * |
| 131 | * @throws IOException If the lock file could not be created or opened |
| 132 | * |
| 133 | * @return bool Returns true if the lock was acquired, false otherwise |
| 134 | */ |
| 135 | public function lock($blocking = false) |
| 136 | { |
| 137 | if ($this->handle) { |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | $error = null; |
| 142 | |
| 143 | // Silence error reporting |
| 144 | set_error_handler(function ($errno, $msg) use (&$error) { |
| 145 | $error = $msg; |
| 146 | }); |
| 147 | |
| 148 | if (!$this->handle = fopen($this->file, 'r')) { |
| 149 | if ($this->handle = fopen($this->file, 'x')) { |
| 150 | chmod($this->file, 0444); |
| 151 | } elseif (!$this->handle = fopen($this->file, 'r')) { |
| 152 | usleep(100); // Give some time for chmod() to complete |
| 153 | $this->handle = fopen($this->file, 'r'); |
| 154 | } |
| 155 | } |
| 156 | restore_error_handler(); |
| 157 | |
| 158 | if (!$this->handle) { |
| 159 | throw new IOException($error, 0, null, $this->file); |
| 160 | } |
| 161 | |
| 162 | // On Windows, even if PHP doc says the contrary, LOCK_NB works, see |
| 163 | // https://bugs.php.net/54129 |
| 164 | if (!flock($this->handle, LOCK_EX | ($blocking ? 0 : LOCK_NB))) { |
| 165 | fclose($this->handle); |
| 166 | $this->handle = null; |
| 167 | |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Release the resource. |
| 176 | */ |
| 177 | public function release() |
| 178 | { |
| 179 | if ($this->handle) { |
| 180 | flock($this->handle, LOCK_UN | LOCK_NB); |
| 181 | fclose($this->handle); |
| 182 | $this->handle = null; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 |