Quantcast
Channel: Symfony Blog
Viewing all articles
Browse latest Browse all 3091

New in Symfony 2.6: LockHandler

$
0
0
Grégoire Pineau

Contributed by
Grégoire Pineau
in #10475.

File locking is a mechanism that restricts access to a computer file by allowing only one user or process access at any specific time. This mechanism was introduced back in 1963 for the mainframes and it will make its debut in Symfony starting from version 2.6.

The new LockHandler class provides a simple abstraction to lock anything by means of a file lock. Its most common use case is to avoid race conditions by locking commands, so the same command cannot be executed concurrently by different processes.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
useSymfony\Component\Console\Command\Command;useSymfony\Component\Console\Input\InputInterface;useSymfony\Component\Console\Output\OutputInterface;useSymfony\Component\Filesystem\LockHandler;classUpdateContentsCommandextendsCommand{protectedfunctionconfigure(){// ...}protectedfunctionexecute(InputInterface$input,OutputInterface$output){// create the lock$lock=newLockHandler('update:contents');if(!$lock->lock()){$output->writeln('The command is already running in another process.');return0;}// ... do some task// (optional) release the lock (otherwise, PHP will do it// for you automatically)$lock->release();}}

The LockHandler constructor takes as its first argument the lock identifier, which will be used as part of the name of the file used to create the lock. By default, locks are created in the temporary directory of the system. If you want to use a specific directory, pass it as the second optional argument of the constructor.

The lock() method returns true if the lock was acquired and false otherwise. In addition, you can optionally pass a boolean argument to indicate if you want to wait until the requested lock is released. This is useful to execute a command just after the other locking command is finished:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
classUpdateContentsCommandextendsCommand{// ...protectedfunctionexecute(InputInterface$input,OutputInterface$output){// create the lock$lock=newLockHandler('update:contents');// wait for the lock release as long as necessaryif(!$lock->lock(true)){$output->writeln('The command is already running in another process.');return0;}// ...}}

The lock handler has been limited on purpose to work only with file-based locks, because it's extremely complex to make it work on network or databases. This means that it only works when using one and only one host. If you have several hosts, you must not use this helper.


Be trained by Symfony experts - 2014-10-06 Cologne - 2014-10-06 Cologne

Viewing all articles
Browse latest Browse all 3091

Trending Articles