PHPackages                             neurohotep/console-mutex - 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. neurohotep/console-mutex

ActiveLibrary[CLI &amp; Console](/categories/cli)

neurohotep/console-mutex
========================

Mutex for Laravel console commands.

6.0.0(6y ago)0339MITPHPPHP ^7.2

Since May 26Pushed 6y agoCompare

[ Source](https://github.com/neurohotep/laravel-console-mutex)[ Packagist](https://packagist.org/packages/neurohotep/console-mutex)[ RSS](/packages/neurohotep-console-mutex/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (8)Versions (79)Used By (0)

Laravel Console Mutex
=====================

[](#laravel-console-mutex)

[![Become a Patron](https://user-images.githubusercontent.com/1286821/43083932-4915853a-8ea0-11e8-8983-db9e0f04e772.png)](https://patreon.com/dmitryivanov)

[![StyleCI](https://camo.githubusercontent.com/06e96146227d794132156da3c51682093799a1efbfa7429e650fb2acfcf32684/68747470733a2f2f7374796c6563692e696f2f7265706f732f35393537303035322f736869656c643f6272616e63683d6d6173746572267374796c653d666c6174)](https://styleci.io/repos/59570052)[![Build Status](https://camo.githubusercontent.com/99e92cd956cfc33d55e0f2d22e5877f05c38d24b654c6bb0cc74be649bd9da92/68747470733a2f2f7472617669732d63692e6f72672f646d697472792d6976616e6f762f6c61726176656c2d636f6e736f6c652d6d757465782e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/dmitry-ivanov/laravel-console-mutex)[![Coverage Status](https://camo.githubusercontent.com/50ac8c4b310f843797d5b28bbaac671a34912064c705c4946315160c002d8c27/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f646d697472792d6976616e6f762f6c61726176656c2d636f6e736f6c652d6d757465782f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/dmitry-ivanov/laravel-console-mutex?branch=master)

[![Latest Stable Version](https://camo.githubusercontent.com/94e0957cc8b2957793fe4e90b1088546153d4332110bbabc5e36b92f22cf4f09/68747470733a2f2f706f7365722e707567782e6f72672f696c6c756d696e617465642f636f6e736f6c652d6d757465782f762f737461626c65)](https://packagist.org/packages/illuminated/console-mutex)[![Latest Unstable Version](https://camo.githubusercontent.com/c4387fd334f8c8b28987b6915c90de1433a57ebf5c6b56c6696c7dbdb1828666/68747470733a2f2f706f7365722e707567782e6f72672f696c6c756d696e617465642f636f6e736f6c652d6d757465782f762f756e737461626c65)](https://packagist.org/packages/illuminated/console-mutex)[![Total Downloads](https://camo.githubusercontent.com/f7f5b5582d1ba12f09f16fd1090ef114e014f4234310e1adea4849e4b552d277/68747470733a2f2f706f7365722e707567782e6f72672f696c6c756d696e617465642f636f6e736f6c652d6d757465782f646f776e6c6f616473)](https://packagist.org/packages/illuminated/console-mutex)[![License](https://camo.githubusercontent.com/f94546ed07c2a9ab39ecef5cddf60910d8a06610fd4aa38a4b35a6304233e4b4/68747470733a2f2f706f7365722e707567782e6f72672f696c6c756d696e617465642f636f6e736f6c652d6d757465782f6c6963656e7365)](https://packagist.org/packages/illuminated/console-mutex)

Mutex for Laravel console commands.

LaravelConsole Mutex6.x[6.x](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/6.x)5.8.\*[5.8.\*](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/5.8)5.7.\*[5.7.\*](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/5.7)5.6.\*[5.6.\*](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/5.6)5.5.\*[5.5.\*](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/5.5)5.4.\*[5.4.\*](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/5.4)5.3.\*[5.3.\*](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/5.3)5.2.\*[5.2.\*](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/5.2)5.1.\*[5.1.\*](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/5.1)[![Example](doc/img/example-new.gif)](doc/img/example-new.gif)

Table of contents
-----------------

[](#table-of-contents)

- [Usage](#usage)
- [Strategies](#strategies)
- [Advanced](#advanced)
    - [Set custom timeout](#set-custom-timeout)
    - [Handle several commands](#handle-several-commands)
    - [Custom mutex file storage](#custom-mutex-file-storage)
- [Troubleshooting](#troubleshooting)
    - [Trait included, but nothing happens?](#trait-included-but-nothing-happens)
    - [Several traits conflict?](#several-traits-conflict)
- [License](#license)

Usage
-----

[](#usage)

1. Install the package via Composer:

    ```
    composer require illuminated/console-mutex
    ```
2. Use `Illuminated\Console\WithoutOverlapping` trait:

    ```
    use Illuminated\Console\WithoutOverlapping;

    class ExampleCommand extends Command
    {
        use WithoutOverlapping;

        // ...
    }
    ```

Strategies
----------

[](#strategies)

Overlapping can be prevented by various strategies:

- `file` (default)
- `mysql`
- `redis`
- `memcached`

Default `file` strategy is fine for small applications, which are deployed on a single server. If your application is more complex and deployed on several nodes, then you probably would like to use another mutex strategy.

You can change the mutex strategy by specifying `$mutexStrategy` field:

```
class ExampleCommand extends Command
{
    use WithoutOverlapping;

    protected $mutexStrategy = 'mysql';

    // ...
}
```

Or by using `setMutexStrategy` method:

```
class ExampleCommand extends Command
{
    use WithoutOverlapping;

    public function __construct()
    {
        parent::__construct();

        $this->setMutexStrategy('mysql');
    }

    // ...
}
```

Advanced
--------

[](#advanced)

### Set custom timeout

[](#set-custom-timeout)

By default, the mutex is checking for a running command, and if it finds such, it just exits. However, you can manually set the timeout for a mutex, so it can wait for another command to finish its execution, instead of just quitting immediately.

You can change the mutex timeout by specifying `$mutexTimeout` field:

```
class ExampleCommand extends Command
{
    use WithoutOverlapping;

    protected $mutexTimeout = 3000; // milliseconds

    // ...
}
```

Or by using `setMutexTimeout` method:

```
class ExampleCommand extends Command
{
    use WithoutOverlapping;

    public function __construct()
    {
        parent::__construct();

        $this->setMutexTimeout(3000); // milliseconds
    }

    // ...
}
```

There are three possible options for `$mutexTimeout` field:

- `0` - check without waiting (default);
- `{milliseconds}` - check, and wait for a maximum of milliseconds specified;
- `null` - wait, till running command finish its execution;

### Handle several commands

[](#handle-several-commands)

Sometimes it is useful to set common mutex for several commands. You can easily achieve this by setting them the same mutex name. By default, mutex name is generated based on a command's name and arguments.

To change this, override `getMutexName` method in your command:

```
class ExampleCommand extends Command
{
    use WithoutOverlapping;

    public function getMutexName()
    {
        return "icmutex-for-command1-and-command2";
    }

    // ...
}
```

### Custom mutex file storage

[](#custom-mutex-file-storage)

If you're using `file` strategy, mutex files will be stored in the `storage/app` folder, by default.

You can change the storage folder by overriding `getMutexFileStorage` method in your command:

```
class ExampleCommand extends Command
{
    use WithoutOverlapping;

    public function getMutexFileStorage()
    {
        return storage_path('my/custom/path');
    }

    // ...
}
```

Troubleshooting
---------------

[](#troubleshooting)

### Trait included, but nothing happens?

[](#trait-included-but-nothing-happens)

Note, that `WithoutOverlapping` trait is overriding `initialize` method:

```
trait WithoutOverlapping
{
    protected function initialize(InputInterface $input, OutputInterface $output)
    {
        $this->initializeMutex();
    }

    // ...
}
```

If your command is overriding `initialize` method too, then you should call `initializeMutex` method by yourself:

```
class ExampleCommand extends Command
{
    use WithoutOverlapping;

    protected function initialize(InputInterface $input, OutputInterface $output)
    {
        $this->initializeMutex();

        $this->foo = $this->argument('foo');
        $this->bar = $this->argument('bar');
        $this->baz = $this->argument('baz');
    }

    // ...
}
```

### Several traits conflict?

[](#several-traits-conflict)

If you're using another `illuminated/console-%` package, then you can find yourself getting into the "traits conflict".

For example, if you're trying to build [loggable command](https://github.com/dmitry-ivanov/laravel-console-logger), which is protected against overlapping:

```
class ExampleCommand extends Command
{
    use Loggable;
    use WithoutOverlapping;

    // ...
}
```

You'll get the fatal error - the traits conflict, because of both of these traits are overriding `initialize` method:

> If two traits insert a method with the same name, a fatal error is produced, if the conflict is not explicitly resolved.

Override `initialize` method by yourself, and initialize traits in required order:

```
class ExampleCommand extends Command
{
    use Loggable;
    use WithoutOverlapping;

    protected function initialize(InputInterface $input, OutputInterface $output)
    {
        $this->initializeMutex();
        $this->initializeLogging();
    }

    // ...
}
```

License
-------

[](#license)

The MIT License. Please see [License File](LICENSE) for more information.

[![Support on Patreon](https://user-images.githubusercontent.com/1286821/43086829-ff7c006e-8ea6-11e8-8b03-ecf97ca95b2e.png)](https://patreon.com/dmitryivanov)

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 98.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~16 days

Recently: every ~57 days

Total

78

Last Release

2392d ago

Major Versions

0.1.9 → 1.0.02016-05-31

1.5.0 → 5.1.02017-09-08

5.8.0 → 6.0.02019-09-13

PHP version history (7 changes)0.1.1PHP &gt;=7.0.0

0.1.2PHP &gt;=5.4.0

1.0.2PHP &gt;=5.5.9

1.1.6PHP &gt;=5.6.4

5.5.0PHP &gt;=7.0

5.6.0PHP ^7.1.3

6.0.0PHP ^7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/1aa4f2d55f5ce6343cdbc6311c0a7b6a499be8924272ba67b645f062a86c8e0b?d=identicon)[umertasov](/maintainers/umertasov)

---

Top Contributors

[![dmitry-ivanov](https://avatars.githubusercontent.com/u/1286821?v=4)](https://github.com/dmitry-ivanov "dmitry-ivanov (358 commits)")[![dmirogin](https://avatars.githubusercontent.com/u/5470439?v=4)](https://github.com/dmirogin "dmirogin (2 commits)")[![neurohotep](https://avatars.githubusercontent.com/u/12581971?v=4)](https://github.com/neurohotep "neurohotep (1 commits)")[![SBLAdam](https://avatars.githubusercontent.com/u/81582882?v=4)](https://github.com/SBLAdam "SBLAdam (1 commits)")[![vimesd](https://avatars.githubusercontent.com/u/12581971?v=4)](https://github.com/vimesd "vimesd (1 commits)")

---

Tags

consolelaravelmutexcommandlockeroverlapping

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/neurohotep-console-mutex/health.svg)

```
[![Health](https://phpackages.com/badges/neurohotep-console-mutex/health.svg)](https://phpackages.com/packages/neurohotep-console-mutex)
```

###  Alternatives

[illuminated/console-mutex

Mutex for Laravel Console Commands.

146938.2k1](/packages/illuminated-console-mutex)[nunomaduro/laravel-console-menu

Laravel Console Menu is an output method for your Laravel/Laravel Zero commands.

815412.0k48](/packages/nunomaduro-laravel-console-menu)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
