Skip to content

Commit 84ce746

Browse files
Nyholmjaviereguiluz
authored andcommitted
[Lock] Added paragraph about auto release
1 parent 8379ea8 commit 84ce746

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
@@ -199,6 +199,38 @@ This component also provides two useful methods related to expiring locks:
199199
``getRemainingLifetime()`` (which returns ``null`` or a ``float``
200200
as seconds) and ``isExpired()`` (which returns a boolean).
201201

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

0 commit comments

Comments
 (0)