Skip to content

[Command] Added LockHelper #3956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions components/filesystem/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Filesystem
==========

.. toctree::
:maxdepth: 2

introduction
lock_handler
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ The Filesystem Component
The Filesystem component was introduced in Symfony 2.1. Previously, the
``Filesystem`` class was located in the HttpKernel component.


.. tip::

A lock handler feature was introduce in symfony 2.6.
:doc:`See the documentation for more information </components/filesystem/lock_handler>`.

Installation
------------

Expand Down
69 changes: 69 additions & 0 deletions components/filesystem/lock_handler.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
LockHandler
===========

.. versionadded:: 2.6
The lock handler feature was introduced in Symfony 2.6

What is a Lock?
---------------

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 a few decades ago for mainframes, but continues being useful for
modern applications.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of makes it sound like this feature helps only in avoiding multiple people accessing
the same "computer file" - not something more generic. I'm ok with it really, but maybe you can
find some better wording.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. I'm not very fluent in English and in documentation. We already change this part lof of time, it's my best here...


Symfony provides a LockHelper to help you use locks in your project.

Usage
-----

.. caution::

The lock handler only works if you're using just one server. If you have
several hosts, you must not use this helper.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're saying "hosts", which makes me think of host names, not actual machines. What about saying:

The lock handler only works if you're using just one server. If you have several servers...

... I may also be misinterpreting what you mean :).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you totally understand the point ;) Anyway, I change the doc by using your sentence ;) and I also fixed all others issues.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too bad, it would be cool if you could pass any writeable stream, including something shared. Sorry, feature creep - maybe someday when/if the Filesystem component does more than just the local filesystem ;).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to pass a writable stream. Locking in a distributed environment is totally different and need much more powerful tools (like http://www.consul.io/, very good tool).
So here, we use a file with flock system primitive.


A lock can be used, for example, to allow only one instance of a command to run.

.. code-block:: php

use Symfony\Component\Filesystem\LockHandler;

$lockHandler = new LockHandler('hello.lock');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing with this example is that some people could think that you have to pass the full file name, including the .lock extension.

if (!$lockHandler->lock()) {
// the resource "hello" is already locked by another process

return 0;
}

The first argument of the constructor is a string that it will use as part of
the name of the file used to create the lock on the local filesystem. A best
practice for Symfony commands is to use the command name, such as ``acme:my-command``.
``LockHandler`` sanitizes the contents of the string before creating
the file, so you can pass any value for this argument.

.. tip::

The ``.lock`` extension is optional, but it's a common practice to include
it. This will make it easier to find lock files on the filesystem. Moreover,
to avoid name collisions, ``LockHandler`` also appends a hash to the name of
the lock file.

By default, the lock will be created in the temporary directory, but you can
optionally select the directory where locks are created by passing it as the
second argument of the constructor.

The :method:`Symfony\\Component\\Filesystem\\LockHandler::lock` method tries to
acquire the lock. If the lock is acquired, the method returns ``true``,
``false`` otherwise. If the ``lock`` method is called several times on the same
instance it will always return ``true`` if the lock was acquired on the first
call.

You can pass an optional blocking argument as the first argument to the
``lock()`` method, which defaults to ``false``. If this is set to ``true``, your
PHP code will wait indefinitely until the lock is released by another process.

The resource is automatically released by PHP at the end of the script. In
addition, you can invoke the
:method:`Symfony\\Component\\Filesystem\\LockHandler::release` method to release
the lock explicitly. Once it's released, any other process can lock the
resource.
2 changes: 1 addition & 1 deletion components/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The Components
dom_crawler
event_dispatcher/index
expression_language/index
filesystem
filesystem/index
finder
form/index
http_foundation/index
Expand Down
5 changes: 3 additions & 2 deletions components/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@
* :doc:`/components/expression_language/extending`
* :doc:`/components/expression_language/caching`

* **Filesystem**
* :doc:`/components/filesystem/index`

* :doc:`/components/filesystem`
* :doc:`/components/filesystem/introduction`
* :doc:`/components/filesystem/lock_handler`

* **Finder**

Expand Down
1 change: 1 addition & 0 deletions redirection_map
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
/cookbook/console/generating_urls /cookbook/console/sending_emails
/components/yaml /components/yaml/introduction
/components/templating /components/templating/introduction
/components/filesystem /components/filesystem/introduction
/cmf/reference/configuration/block /cmf/bundles/block/configuration
/cmf/reference/configuration/content /cmf/bundles/content/configuration
/cmf/reference/configuration/core /cmf/bundles/core/configuration
Expand Down