Skip to content

Commit 6e12d6d

Browse files
committed
minor #15049 [Lock] Added paragraph about auto release (Nyholm)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [Lock] Added paragraph about auto release This will fix symfony/symfony#40325 @twisted1919 is this paragraph helpful? Should I add something more? Commits ------- 84ce746 [Lock] Added paragraph about auto release
2 parents ef72b4b + 84ce746 commit 6e12d6d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

components/lock.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,38 @@ This component also provides two useful methods related to expiring locks:
200200
``getRemainingLifetime()`` (which returns ``null`` or a ``float``
201201
as seconds) and ``isExpired()`` (which returns a boolean).
202202

203+
Automatically Releasing The Lock
204+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
205+
206+
A Lock will be automatically released when the Lock object is destructed. This is
207+
an implementation detail that will be important when Locks are shared between processes.
208+
In the example below, the ``pcntl_fork()`` will create two processes and the Lock
209+
will be released automatically as soon as one process finishes::
210+
211+
// ...
212+
$lock = $factory->createLock('report-generation', 3600);
213+
if (!$lock->acquire()) {
214+
return;
215+
}
216+
217+
$pid = pcntl_fork();
218+
if ($pid == -1) {
219+
// Could not fork
220+
exit(1);
221+
} elseif ($pid) {
222+
// Parent process
223+
sleep(30);
224+
} else {
225+
// Child process
226+
echo 'The lock will be released now.'
227+
exit(0);
228+
}
229+
// ...
230+
231+
To disable this behavior, one needs to set the 3rd argument to the ``LockFactory::createLock()``
232+
to false. That will make the Lock acquired for 3600 seconds or until ``Lock::release()``
233+
is called.
234+
203235
The Owner of The Lock
204236
---------------------
205237

0 commit comments

Comments
 (0)