From 5233c2a4f283f9c863e0b6fd963ff89fa9f19b75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Deruss=C3=A9?= Date: Fri, 4 Sep 2020 23:45:41 +0200 Subject: [PATCH] Fix bad example of lock --- components/lock.rst | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/components/lock.rst b/components/lock.rst index 19f1d2dbf8b..fdaf3b7686a 100644 --- a/components/lock.rst +++ b/components/lock.rst @@ -117,7 +117,9 @@ method, the resource will stay locked until the timeout:: // create an expiring lock that lasts 30 seconds $lock = $factory->createLock('charts-generation', 30); - $lock->acquire(); + if (!$lock->acquire()) { + return; + } try { // perform a job during less than 30 seconds } finally { @@ -136,7 +138,9 @@ to reset the TTL to its original value:: // ... $lock = $factory->createLock('charts-generation', 30); - $lock->acquire(); + if (!$lock->acquire()) { + return; + } try { while (!$finished) { // perform a small part of the job. @@ -366,7 +370,9 @@ Using the above methods, a more robust code would be:: // ... $lock = $factory->createLock('invoice-publication', 30); - $lock->acquire(); + if (!$lock->acquire()) { + return; + } while (!$finished) { if ($lock->getRemainingLifetime() <= 5) { if ($lock->isExpired()) {