From d6456f1a2aa977965d6dba032cab309b83387e4c Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 5 Sep 2016 15:31:41 +0200 Subject: [PATCH 1/2] Documented the Lockable Trait --- console/lockable_trait.rst | 64 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 console/lockable_trait.rst diff --git a/console/lockable_trait.rst b/console/lockable_trait.rst new file mode 100644 index 00000000000..5238a178011 --- /dev/null +++ b/console/lockable_trait.rst @@ -0,0 +1,64 @@ +Prevent Multiple Executions of a Console Command +================================================ + +A simple but effective way to prevent multiple executions of the same command in +a single server is to use **file locks**. The Filesystem component provides a +:doc:`LockHandler ` class that eases the +creation and release of these locks:: + + // ... + use Symfony\Component\Filesystem\LockHandler; + + class UpdateContentsCommand extends Command + { + // ... + + protected function execute(InputInterface $input, OutputInterface $output) + { + // create the lock (tip: use the command name as the lock name + // to ensure its uniqueness) + $lock = new LockHandler('update:contents'); + if (!$lock->lock()) { + $output->writeln('The command is already running in another process.'); + + return 0; + } + + // ... + + // if not released explicitly, Symfony releases the lock + // automatically when the execution of the command ends + $lock->release(); + } + } + +The Console component provides a PHP trait called ``LockableTrait`` that adds +two convenient methods to lock and release commands:: + + // ... + use Symfony\Component\Console\Command\LockableTrait; + + class UpdateContentsCommand extends Command + { + use LockableTrait; + + // ... + + protected function execute(InputInterface $input, OutputInterface $output) + { + if (!$this->lock()) { + $output->writeln('The command is already running in another process.'); + + return 0; + } + + // If you prefer to wait until the lock is released, use this: + // $this->lock(true); + + // ... + + // if not released explicitly, Symfony releases the lock + // automatically when the execution of the command ends + $this->release(); + } + } From a60af478f6f7a1d8f3bac6dce6b06a4ee41a66fd Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 28 Sep 2016 12:09:15 +0200 Subject: [PATCH 2/2] Simplified the article --- console/lockable_trait.rst | 32 +++----------------------------- 1 file changed, 3 insertions(+), 29 deletions(-) diff --git a/console/lockable_trait.rst b/console/lockable_trait.rst index 5238a178011..0053e4980bf 100644 --- a/console/lockable_trait.rst +++ b/console/lockable_trait.rst @@ -4,36 +4,10 @@ Prevent Multiple Executions of a Console Command A simple but effective way to prevent multiple executions of the same command in a single server is to use **file locks**. The Filesystem component provides a :doc:`LockHandler ` class that eases the -creation and release of these locks:: +creation and release of these locks. - // ... - use Symfony\Component\Filesystem\LockHandler; - - class UpdateContentsCommand extends Command - { - // ... - - protected function execute(InputInterface $input, OutputInterface $output) - { - // create the lock (tip: use the command name as the lock name - // to ensure its uniqueness) - $lock = new LockHandler('update:contents'); - if (!$lock->lock()) { - $output->writeln('The command is already running in another process.'); - - return 0; - } - - // ... - - // if not released explicitly, Symfony releases the lock - // automatically when the execution of the command ends - $lock->release(); - } - } - -The Console component provides a PHP trait called ``LockableTrait`` that adds -two convenient methods to lock and release commands:: +In addition, the Console component provides a PHP trait called ``LockableTrait`` +that adds two convenient methods to lock and release commands:: // ... use Symfony\Component\Console\Command\LockableTrait;