File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -199,6 +199,38 @@ This component also provides two useful methods related to expiring locks:
199
199
``getRemainingLifetime() `` (which returns ``null `` or a ``float ``
200
200
as seconds) and ``isExpired() `` (which returns a boolean).
201
201
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
+
202
234
The Owner of The Lock
203
235
---------------------
204
236
You can’t perform that action at this time.
0 commit comments