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

Abandoned → [illuminated/console-mutex](/?search=illuminated%2Fconsole-mutex)ArchivedLibrary[CLI &amp; Console](/categories/cli)

olafnorge/console-mutex
=======================

Prevents overlapping for Laravel console commands.

1.4.5(9y ago)0348MITPHPPHP &gt;=5.5.9

Since May 26Pushed 9y ago1 watchersCompare

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

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

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

[](#laravel-console-mutex)

[![Latest Stable Version](https://camo.githubusercontent.com/7d640100e440ea2fa4f97f32241cd425dcdef6bc59132725f984850723d0de38/68747470733a2f2f706f7365722e707567782e6f72672f6f6c61666e6f7267652f636f6e736f6c652d6d757465782f762f737461626c65)](https://packagist.org/packages/olafnorge/console-mutex)[![Latest Unstable Version](https://camo.githubusercontent.com/f42e8f53a8dc1314567f6bae0328ba88d23823f35eaf93f96aa0de6d584dc189/68747470733a2f2f706f7365722e707567782e6f72672f6f6c61666e6f7267652f636f6e736f6c652d6d757465782f762f756e737461626c65)](https://packagist.org/packages/olafnorge/console-mutex)[![Total Downloads](https://camo.githubusercontent.com/b6c3dac9fe3e3101f1bf46f7150efa833ceb3dfbbbb85579caa777315d346f08/68747470733a2f2f706f7365722e707567782e6f72672f6f6c61666e6f7267652f636f6e736f6c652d6d757465782f646f776e6c6f616473)](https://packagist.org/packages/olafnorge/console-mutex)[![License](https://camo.githubusercontent.com/e194a6ade820262db227b44ed9ccc25f0251c3c12f783d18c1d59194349d824c/68747470733a2f2f706f7365722e707567782e6f72672f6f6c61666e6f7267652f636f6e736f6c652d6d757465782f6c6963656e7365)](https://packagist.org/packages/olafnorge/console-mutex)

Prevents overlapping for Laravel console commands.

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

[](#table-of-contents)

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

Requirements
------------

[](#requirements)

- `PHP >=5.5.9`
- `Laravel >=5.1`

Usage
-----

[](#usage)

1. Install package through `composer`:

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

    ```
    use olafnorge\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 a small applications, which are deployed on a single server. If your application is more complex and, for example, is deployed on a several nodes, then you probably would like to use some other mutex strategy.

You can change 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 mutex is checking for a running command, and if it finds such, it just exits. However, you can manually set timeout for a mutex, so it can wait for another command to finish it's execution, instead of just quitting immediately.

You can change 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 it's execution;

### Handle several commands

[](#handle-several-commands)

Sometimes it is useful to set common mutex for a 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, just override `getMutexName` method in your command:

```
class ExampleCommand extends Command
{
    use WithoutOverlapping;

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

    // ...
}
```

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 some other cool `olafnorge/console-%` packages, well, then you can find yourself getting "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 fatal error, the "traits conflict", because 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.

But don't worry, solution is very simple. 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();
    }

    // ...
}
```

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 99.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 ~9 days

Total

37

Last Release

3358d ago

Major Versions

0.1.9 → 1.0.02016-05-31

PHP version history (4 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

### Community

Maintainers

![](https://www.gravatar.com/avatar/59928c6c72d012846608f6301c74c40923705ec755ac4e5d738a79efc01e4aa2?d=identicon)[olafnorge](/maintainers/olafnorge)

---

Top Contributors

[![dmitry-ivanov](https://avatars.githubusercontent.com/u/1286821?v=4)](https://github.com/dmitry-ivanov "dmitry-ivanov (258 commits)")[![olafnorge](https://avatars.githubusercontent.com/u/1435932?v=4)](https://github.com/olafnorge "olafnorge (1 commits)")

---

Tags

consolelaravelmutexcommandlockeroverlapping

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[illuminated/console-mutex

Mutex for Laravel Console Commands.

147970.4k1](/packages/illuminated-console-mutex)[laravel/ai

The official AI SDK for Laravel.

1.0k2.1M163](/packages/laravel-ai)[nunomaduro/laravel-console-menu

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

815424.6k52](/packages/nunomaduro-laravel-console-menu)

PHPackages © 2026

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