Skip to content

[Lock] Complete Lock example for less ambiguity #17835

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 1 commit into from
Feb 3, 2023
Merged
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
36 changes: 35 additions & 1 deletion components/lock.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,42 @@ Serializing Locks
The :class:`Symfony\\Component\\Lock\\Key` contains the state of the
:class:`Symfony\\Component\\Lock\\Lock` and can be serialized. This
allows the user to begin a long job in a process by acquiring the lock, and
continue the job in another process using the same lock::
continue the job in another process using the same lock.

First, you may create a serializable class containing the resource and the
key of the lock::

// src/Lock/RefreshTaxonomy.php
namespace App\Lock;

use Symfony\Component\Lock\Key;

class RefreshTaxonomy
{
private object $article;
private Key $key;

public function __construct(object $article, Key $key)
{
$this->article = $article;
$this->key = $key;
}

public function getArticle(): object
{
return $this->article;
}

public function getKey(): Key
{
return $this->key;
}
}

Then, you can use this class to dispatch all that's needed for another process
to handle the rest of the job::

use App\Lock\RefreshTaxonomy;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\Lock;

Expand Down