Skip to content

Commit ffc5e0a

Browse files
Merge branch '6.4' into 7.0
* 6.4: fix typo Add types to private and internal properties [Workflow] Cleaning code [Scheduler] Fix NPE in debug:scheduler command
2 parents 26c5022 + 836e1d1 commit ffc5e0a

File tree

4 files changed

+34
-45
lines changed

4 files changed

+34
-45
lines changed

Lock.php

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Psr\Log\LoggerAwareInterface;
1515
use Psr\Log\LoggerAwareTrait;
16-
use Psr\Log\NullLogger;
1716
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1817
use Symfony\Component\Lock\Exception\LockAcquiringException;
1918
use Symfony\Component\Lock\Exception\LockConflictedException;
@@ -29,24 +28,18 @@ final class Lock implements SharedLockInterface, LoggerAwareInterface
2928
{
3029
use LoggerAwareTrait;
3130

32-
private PersistingStoreInterface $store;
33-
private Key $key;
34-
private ?float $ttl;
35-
private bool $autoRelease;
3631
private bool $dirty = false;
3732

3833
/**
3934
* @param float|null $ttl Maximum expected lock duration in seconds
4035
* @param bool $autoRelease Whether to automatically release the lock or not when the lock instance is destroyed
4136
*/
42-
public function __construct(Key $key, PersistingStoreInterface $store, float $ttl = null, bool $autoRelease = true)
43-
{
44-
$this->store = $store;
45-
$this->key = $key;
46-
$this->ttl = $ttl;
47-
$this->autoRelease = $autoRelease;
48-
49-
$this->logger = new NullLogger();
37+
public function __construct(
38+
private Key $key,
39+
private PersistingStoreInterface $store,
40+
private ?float $ttl = null,
41+
private bool $autoRelease = true,
42+
) {
5043
}
5144

5245
public function __sleep(): array
@@ -93,7 +86,7 @@ public function acquire(bool $blocking = false): bool
9386
}
9487

9588
$this->dirty = true;
96-
$this->logger->debug('Successfully acquired the "{resource}" lock.', ['resource' => $this->key]);
89+
$this->logger?->debug('Successfully acquired the "{resource}" lock.', ['resource' => $this->key]);
9790

9891
if ($this->ttl) {
9992
$this->refresh();
@@ -111,15 +104,15 @@ public function acquire(bool $blocking = false): bool
111104
return true;
112105
} catch (LockConflictedException $e) {
113106
$this->dirty = false;
114-
$this->logger->info('Failed to acquire the "{resource}" lock. Someone else already acquired the lock.', ['resource' => $this->key]);
107+
$this->logger?->info('Failed to acquire the "{resource}" lock. Someone else already acquired the lock.', ['resource' => $this->key]);
115108

116109
if ($blocking) {
117110
throw $e;
118111
}
119112

120113
return false;
121114
} catch (\Exception $e) {
122-
$this->logger->notice('Failed to acquire the "{resource}" lock.', ['resource' => $this->key, 'exception' => $e]);
115+
$this->logger?->notice('Failed to acquire the "{resource}" lock.', ['resource' => $this->key, 'exception' => $e]);
123116
throw new LockAcquiringException(sprintf('Failed to acquire the "%s" lock.', $this->key), 0, $e);
124117
}
125118
}
@@ -129,7 +122,7 @@ public function acquireRead(bool $blocking = false): bool
129122
$this->key->resetLifetime();
130123
try {
131124
if (!$this->store instanceof SharedLockStoreInterface) {
132-
$this->logger->debug('Store does not support ReadLocks, fallback to WriteLock.', ['resource' => $this->key]);
125+
$this->logger?->debug('Store does not support ReadLocks, fallback to WriteLock.', ['resource' => $this->key]);
133126

134127
return $this->acquire($blocking);
135128
}
@@ -151,7 +144,7 @@ public function acquireRead(bool $blocking = false): bool
151144
}
152145

153146
$this->dirty = true;
154-
$this->logger->debug('Successfully acquired the "{resource}" lock for reading.', ['resource' => $this->key]);
147+
$this->logger?->debug('Successfully acquired the "{resource}" lock for reading.', ['resource' => $this->key]);
155148

156149
if ($this->ttl) {
157150
$this->refresh();
@@ -169,15 +162,15 @@ public function acquireRead(bool $blocking = false): bool
169162
return true;
170163
} catch (LockConflictedException $e) {
171164
$this->dirty = false;
172-
$this->logger->info('Failed to acquire the "{resource}" lock. Someone else already acquired the lock.', ['resource' => $this->key]);
165+
$this->logger?->info('Failed to acquire the "{resource}" lock. Someone else already acquired the lock.', ['resource' => $this->key]);
173166

174167
if ($blocking) {
175168
throw $e;
176169
}
177170

178171
return false;
179172
} catch (\Exception $e) {
180-
$this->logger->notice('Failed to acquire the "{resource}" lock.', ['resource' => $this->key, 'exception' => $e]);
173+
$this->logger?->notice('Failed to acquire the "{resource}" lock.', ['resource' => $this->key, 'exception' => $e]);
181174
throw new LockAcquiringException(sprintf('Failed to acquire the "%s" lock.', $this->key), 0, $e);
182175
}
183176
}
@@ -202,13 +195,13 @@ public function refresh(float $ttl = null): void
202195
throw new LockExpiredException(sprintf('Failed to put off the expiration of the "%s" lock within the specified time.', $this->key));
203196
}
204197

205-
$this->logger->debug('Expiration defined for "{resource}" lock for "{ttl}" seconds.', ['resource' => $this->key, 'ttl' => $ttl]);
198+
$this->logger?->debug('Expiration defined for "{resource}" lock for "{ttl}" seconds.', ['resource' => $this->key, 'ttl' => $ttl]);
206199
} catch (LockConflictedException $e) {
207200
$this->dirty = false;
208-
$this->logger->notice('Failed to define an expiration for the "{resource}" lock, someone else acquired the lock.', ['resource' => $this->key]);
201+
$this->logger?->notice('Failed to define an expiration for the "{resource}" lock, someone else acquired the lock.', ['resource' => $this->key]);
209202
throw $e;
210203
} catch (\Exception $e) {
211-
$this->logger->notice('Failed to define an expiration for the "{resource}" lock.', ['resource' => $this->key, 'exception' => $e]);
204+
$this->logger?->notice('Failed to define an expiration for the "{resource}" lock.', ['resource' => $this->key, 'exception' => $e]);
212205
throw new LockAcquiringException(sprintf('Failed to define an expiration for the "%s" lock.', $this->key), 0, $e);
213206
}
214207
}
@@ -234,9 +227,9 @@ public function release(): void
234227
throw new LockReleasingException(sprintf('Failed to release the "%s" lock, the resource is still locked.', $this->key));
235228
}
236229

237-
$this->logger->debug('Successfully released the "{resource}" lock.', ['resource' => $this->key]);
230+
$this->logger?->debug('Successfully released the "{resource}" lock.', ['resource' => $this->key]);
238231
} catch (LockReleasingException $e) {
239-
$this->logger->notice('Failed to release the "{resource}" lock.', ['resource' => $this->key]);
232+
$this->logger?->notice('Failed to release the "{resource}" lock.', ['resource' => $this->key]);
240233
throw $e;
241234
}
242235
}

LockFactory.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,9 @@ class LockFactory implements LoggerAwareInterface
2525
{
2626
use LoggerAwareTrait;
2727

28-
private PersistingStoreInterface $store;
29-
30-
public function __construct(PersistingStoreInterface $store)
31-
{
32-
$this->store = $store;
33-
34-
$this->logger = new NullLogger();
28+
public function __construct(
29+
private PersistingStoreInterface $store,
30+
) {
3531
}
3632

3733
/**
@@ -56,7 +52,9 @@ public function createLock(string $resource, ?float $ttl = 300.0, bool $autoRele
5652
public function createLockFromKey(Key $key, ?float $ttl = 300.0, bool $autoRelease = true): SharedLockInterface
5753
{
5854
$lock = new Lock($key, $this->store, $ttl, $autoRelease);
59-
$lock->setLogger($this->logger);
55+
if ($this->logger) {
56+
$lock->setLogger($this->logger);
57+
}
6058

6159
return $lock;
6260
}

Store/CombinedStore.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Psr\Log\LoggerAwareInterface;
1515
use Psr\Log\LoggerAwareTrait;
16-
use Psr\Log\NullLogger;
1716
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1817
use Symfony\Component\Lock\Exception\LockConflictedException;
1918
use Symfony\Component\Lock\Key;
@@ -50,7 +49,6 @@ public function __construct(array $stores, StrategyInterface $strategy)
5049

5150
$this->stores = $stores;
5251
$this->strategy = $strategy;
53-
$this->logger = new NullLogger();
5452
}
5553

5654
public function save(Key $key): void
@@ -64,7 +62,7 @@ public function save(Key $key): void
6462
$store->save($key);
6563
++$successCount;
6664
} catch (\Exception $e) {
67-
$this->logger->debug('One store failed to save the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
65+
$this->logger?->debug('One store failed to save the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
6866
++$failureCount;
6967
}
7068

@@ -79,7 +77,7 @@ public function save(Key $key): void
7977
return;
8078
}
8179

82-
$this->logger->info('Failed to store the "{resource}" lock. Quorum has not been met.', ['resource' => $key, 'success' => $successCount, 'failure' => $failureCount]);
80+
$this->logger?->info('Failed to store the "{resource}" lock. Quorum has not been met.', ['resource' => $key, 'success' => $successCount, 'failure' => $failureCount]);
8381

8482
// clean up potential locks
8583
$this->delete($key);
@@ -102,7 +100,7 @@ public function saveRead(Key $key): void
102100
}
103101
++$successCount;
104102
} catch (\Exception $e) {
105-
$this->logger->debug('One store failed to save the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
103+
$this->logger?->debug('One store failed to save the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
106104
++$failureCount;
107105
}
108106

@@ -117,7 +115,7 @@ public function saveRead(Key $key): void
117115
return;
118116
}
119117

120-
$this->logger->info('Failed to store the "{resource}" lock. Quorum has not been met.', ['resource' => $key, 'success' => $successCount, 'failure' => $failureCount]);
118+
$this->logger?->info('Failed to store the "{resource}" lock. Quorum has not been met.', ['resource' => $key, 'success' => $successCount, 'failure' => $failureCount]);
121119

122120
// clean up potential locks
123121
$this->delete($key);
@@ -135,15 +133,15 @@ public function putOffExpiration(Key $key, float $ttl): void
135133
foreach ($this->stores as $store) {
136134
try {
137135
if (0.0 >= $adjustedTtl = $expireAt - microtime(true)) {
138-
$this->logger->debug('Stores took to long to put off the expiration of the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'ttl' => $ttl]);
136+
$this->logger?->debug('Stores took to long to put off the expiration of the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'ttl' => $ttl]);
139137
$key->reduceLifetime(0);
140138
break;
141139
}
142140

143141
$store->putOffExpiration($key, $adjustedTtl);
144142
++$successCount;
145143
} catch (\Exception $e) {
146-
$this->logger->debug('One store failed to put off the expiration of the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
144+
$this->logger?->debug('One store failed to put off the expiration of the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
147145
++$failureCount;
148146
}
149147

@@ -158,7 +156,7 @@ public function putOffExpiration(Key $key, float $ttl): void
158156
return;
159157
}
160158

161-
$this->logger->notice('Failed to define the expiration for the "{resource}" lock. Quorum has not been met.', ['resource' => $key, 'success' => $successCount, 'failure' => $failureCount]);
159+
$this->logger?->notice('Failed to define the expiration for the "{resource}" lock. Quorum has not been met.', ['resource' => $key, 'success' => $successCount, 'failure' => $failureCount]);
162160

163161
// clean up potential locks
164162
$this->delete($key);
@@ -172,7 +170,7 @@ public function delete(Key $key): void
172170
try {
173171
$store->delete($key);
174172
} catch (\Exception $e) {
175-
$this->logger->notice('One store failed to delete the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
173+
$this->logger?->notice('One store failed to delete the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
176174
}
177175
}
178176
}
@@ -191,7 +189,7 @@ public function exists(Key $key): bool
191189
++$failureCount;
192190
}
193191
} catch (\Exception $e) {
194-
$this->logger->debug('One store failed to check the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
192+
$this->logger?->debug('One store failed to check the "{resource}" lock.', ['resource' => $key, 'store' => $store, 'exception' => $e]);
195193
++$failureCount;
196194
}
197195

Store/DoctrineDbalPostgreSqlStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
class DoctrineDbalPostgreSqlStore implements BlockingSharedLockStoreInterface, BlockingStoreInterface
3434
{
3535
private Connection $conn;
36-
private static $storeRegistry = [];
36+
private static array $storeRegistry = [];
3737

3838
/**
3939
* You can either pass an existing database connection a Doctrine DBAL Connection

0 commit comments

Comments
 (0)