2017 © Pedro Peláez
 

library console-mutex

Mutex for Laravel console commands.

image

illuminated/console-mutex

Mutex for Laravel console commands.

  • Monday, July 23, 2018
  • by dmitry-ivanov
  • Repository
  • 5 Watchers
  • 45 Stars
  • 24,651 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 7 Forks
  • 0 Open issues
  • 68 Versions
  • 24 % Grown

The README.md

Mutex for Laravel Console Commands, (*1)

Laravel Console Mutex

Buy me a coffee, (*2)

StyleCI Build Status Coverage Status, (*3)

Packagist Version Packagist Stars Packagist Downloads Packagist License, (*4)

Mutex for Laravel Console Commands., (*5)

Laravel Console Mutex
11.x Support
10.x 10.x
9.x 9.x
8.x 8.x
7.x 7.x
6.x 6.x
5.8.* 5.8.*
5.7.* 5.7.*
5.6.* 5.6.*
5.5.* 5.5.*
5.4.* 5.4.*
5.3.* 5.3.*
5.2.* 5.2.*
5.1.* 5.1.*

Laravel Console Mutex - Demo, (*6)

Table of contents

Usage

  1. Install the package via Composer:, (*7)

    shell script composer require illuminated/console-mutex, (*8)

  2. Use Illuminated\Console\WithoutOverlapping trait:, (*9)

    use Illuminated\Console\WithoutOverlapping;
    
    class ExampleCommand extends Command
    {
        use WithoutOverlapping;
    
        // ...
    }
    

Strategies

Mutex can prevent overlapping by using various strategies:, (*10)

  • file (default)
  • mysql
  • redis
  • memcached

The default file strategy is acceptable for small applications, which are deployed on a single server. If your application is more complex and deployed on several nodes, you should consider using another mutex strategy., (*11)

You can change strategy by using the $mutexStrategy field:, (*12)

class ExampleCommand extends Command
{
    use WithoutOverlapping;

    protected string $mutexStrategy = 'mysql';

    // ...
}

Or by using the setMutexStrategy() method:, (*13)

class ExampleCommand extends Command
{
    use WithoutOverlapping;

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

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

    // ...
}

Advanced

Set custom timeout

By default, if mutex sees that the command is already running, it will immediately quit. You can change that behavior by setting a timeout in which mutex can wait for another running command to finish its execution., (*14)

You can set the timeout by specifying the $mutexTimeout field:, (*15)

class ExampleCommand extends Command
{
    use WithoutOverlapping;

    // In milliseconds
    protected ?int $mutexTimeout = 3000;

    // ...
}

Or by using the setMutexTimeout() method:, (*16)

class ExampleCommand extends Command
{
    use WithoutOverlapping;

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

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

    // ...
}

Here's how the $mutexTimeout field is treated:, (*17)

  • 0 - no waiting (default);
  • {int} - wait for the given number of milliseconds;
  • null - wait for the running command to finish its execution;

Handle multiple commands

Sometimes it might be useful to have a shared mutex for multiple commands. You can easily achieve that by setting the same mutex name for all of those commands., (*18)

You should use the getMutexName() method for that:, (*19)

class ExampleCommand extends Command
{
    use WithoutOverlapping;

    public function getMutexName()
    {
        return 'shared-for-command1-and-command2';
    }

    // ...
}

Set custom storage folder

If you're using the file strategy, mutex files would be stored in the storage/app folder., (*20)

You can change that by overriding the getMutexFileStorage() method:, (*21)

class ExampleCommand extends Command
{
    use WithoutOverlapping;

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

    // ...
}

Troubleshooting

Trait included, but nothing happens?

WithoutOverlapping trait overrides the initialize() method:, (*22)

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

        parent::initialize($input, $output);
    }

    // ...
}

If your command overrides the initialize() method too, you have to call the initializeMutex() method by yourself:, (*23)

class ExampleCommand extends Command
{
    use WithoutOverlapping;

    protected function initialize(InputInterface $input, OutputInterface $output)
    {
        // You have to call it first
        $this->initializeMutex();

        // Then goes your custom code
        $this->foo = $this->argument('foo');
        $this->bar = $this->argument('bar');
        $this->baz = $this->argument('baz');
    }

    // ...
}

Several traits conflict?

If you're using another illuminated/console-% package, you'll get the "traits conflict" error., (*24)

For example, if you're building a loggable command, which doesn't allow overlapping:, (*25)

class ExampleCommand extends Command
{
    use Loggable;
    use WithoutOverlapping;

    // ...
}

You'll get the traits conflict, because both of those traits are overriding the initialize() method:, (*26)

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

To fix that - override the initialize() method and resolve the conflict:, (*28)

class ExampleCommand extends Command
{
    use Loggable;
    use WithoutOverlapping;

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

    // ...
}

Sponsors

Laravel Idea
Material Theme UI Plugin
, (*29)

License

Laravel Console Mutex is open-sourced software licensed under the MIT license., (*30)

Buy me a coffee , (*31)

The Versions

23/07 2018

5.6.x-dev

5.6.9999999.9999999-dev

Mutex for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

23/07 2018

5.6.3

5.6.3.0

Mutex for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

23/07 2018

dev-master

9999999-dev

Mutex for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

23/07 2018

5.5.x-dev

5.5.9999999.9999999-dev

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

23/07 2018

5.4.x-dev

5.4.9999999.9999999-dev

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

23/07 2018

5.3.x-dev

5.3.9999999.9999999-dev

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

23/07 2018

5.2.x-dev

5.2.9999999.9999999-dev

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

23/07 2018

5.1.x-dev

5.1.9999999.9999999-dev

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

16/07 2018

5.6.2

5.6.2.0

Mutex for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

14/07 2018

5.6.1

5.6.1.0

The mutex for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

18/02 2018

5.6.0

5.6.0.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

24/01 2018

5.5.5

5.5.5.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

19/01 2018

5.5.4

5.5.4.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

19/01 2018

5.4.2

5.4.2.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

19/01 2018

5.3.2

5.3.2.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

19/01 2018

5.2.2

5.2.2.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

19/01 2018

5.1.2

5.1.2.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

12/01 2018

5.5.3

5.5.3.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

12/01 2018

5.4.1

5.4.1.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

12/01 2018

5.3.1

5.3.1.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

12/01 2018

5.2.1

5.2.1.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

12/01 2018

5.1.1

5.1.1.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

25/12 2017

5.5.2

5.5.2.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

11/12 2017

5.5.1

5.5.1.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

08/09 2017

5.5.0

5.5.0.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

08/09 2017

5.4.0

5.4.0.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

08/09 2017

5.3.0

5.3.0.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

08/09 2017

5.2.0

5.2.0.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

08/09 2017

5.1.0

5.1.0.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

01/09 2017

1.5.0

1.5.0.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

08/08 2017

1.4.6

1.4.6.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

19/04 2017

1.4.5

1.4.5.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

31/03 2017

1.4.4

1.4.4.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

28/03 2017

1.4.3

1.4.3.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

15/03 2017

1.4.2

1.4.2.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

15/03 2017

1.4.1

1.4.1.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

09/03 2017

1.4.0

1.4.0.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

07/03 2017

1.3.2

1.3.2.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

07/03 2017

1.3.1

1.3.1.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

07/03 2017

1.3.0

1.3.0.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

20/02 2017

1.2.1

1.2.1.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

31/01 2017

1.2.0

1.2.0.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

24/12 2016

1.1.13

1.1.13.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

09/12 2016

1.1.12

1.1.12.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

29/11 2016

1.1.11

1.1.11.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

24/11 2016

1.1.10

1.1.10.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

10/11 2016

1.1.9

1.1.9.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

10/11 2016

1.1.8

1.1.8.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov

laravel command console locker mutex overlapping

14/10 2016

1.1.7

1.1.7.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

14/09 2016

1.1.6

1.1.6.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

13/09 2016

1.1.5

1.1.5.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

19/07 2016

1.1.4

1.1.4.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

07/07 2016

1.1.3

1.1.3.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

02/07 2016

1.1.2

1.1.2.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

27/06 2016

1.1.1

1.1.1.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

25/06 2016

1.1.0

1.1.0.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

25/06 2016

1.0.2

1.0.2.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

14/06 2016

1.0.1

1.0.1.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

31/05 2016

1.0.0

1.0.0.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

31/05 2016

0.1.9

0.1.9.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

31/05 2016

0.1.8

0.1.8.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

31/05 2016

0.1.7

0.1.7.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

31/05 2016

0.1.6

0.1.6.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

31/05 2016

0.1.5

0.1.5.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

31/05 2016

0.1.4

0.1.4.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

26/05 2016

0.1.3

0.1.3.0

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

26/05 2016

0.1.2

0.1.2.0

Prevents Laravel console commands overlapping

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping

26/05 2016

0.1.1

0.1.1.0

Prevents Laravel console commands overlapping

  Sources   Download

MIT

The Requires

 

by Dmitry Ivanov

laravel command console locker mutex overlapping