PHPackages                             dominicwatts/consolelock - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. dominicwatts/consolelock

ActiveMagento2-module[CLI &amp; Console](/categories/cli)

dominicwatts/consolelock
========================

Lock console command

1.0.0(5y ago)00proprietaryPHPPHP ~5.6.0||~7.0.0||~7.1.0||~7.2.0||~7.3.0

Since May 31Pushed 5y agoCompare

[ Source](https://github.com/DominicWatts/ConsoleLock)[ Packagist](https://packagist.org/packages/dominicwatts/consolelock)[ RSS](/packages/dominicwatts-consolelock/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (2)Used By (0)

Magento 2 and Symfony lock composent
====================================

[](#magento-2-and-symfony-lock-composent)

Experiment using Symfony lock compontent and magento console command. For locking console commands and cron proceses to prevent overlap, duplicate proceses or overloaded resource.

Install
-------

[](#install)

```
composer require symfony/lock

```

Usage
-----

[](#usage)

### FlockStore

[](#flockstore)

```
https://symfony.com/doc/current/components/lock.html#lock-store-flock

```

### Example console command

[](#example-console-command)

```
[...]
use Magento\Framework\Filesystem\DirectoryList;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\Store\FlockStore;
[...]
```

```
/**
 * Console Test script
 * @param \Magento\Framework\Filesystem\DirectoryList $dir
 */
public function __construct(
    DirectoryList $dir
) {
    $this->dir = $dir;
    parent::__construct();
}
```

```
$store = new FlockStore($this->dir->getPath('var'));
$factory = new LockFactory($store);
```

#### Example 1

[](#example-1)

```
$lock = $factory->createLock('lock-name');
if ($lock->acquire()) {
    // do stuff
    $lock->release();
} else {
    // cannot get lock
}
```

#### Example 2

[](#example-2)

```
$lock = $factory->createLock('lock-name', 30);
$lock->acquire();
try {
    // perform a job during less than 30 seconds
} catch (\Exception $e) {
    // whoops - error
} finally {
    $lock->release();
}
```

### Sample Console script

[](#sample-console-script)

```
