2017 © Pedro Peláez
 

library console-mutex

Prevents overlapping for Laravel console commands.

image

olafnorge/console-mutex

Prevents overlapping for Laravel console commands.

  • Wednesday, April 19, 2017
  • by olafnorge
  • Repository
  • 1 Watchers
  • 0 Stars
  • 341 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 7 Forks
  • 0 Open issues
  • 38 Versions
  • 0 % Grown

The README.md

Laravel Console Mutex

Latest Stable Version Latest Unstable Version Total Downloads License, (*1)

Prevents overlapping for Laravel console commands., (*2)

Table of contents

Requirements

  • PHP >=5.5.9
  • Laravel >=5.1

Usage

  1. Install package through composer:, (*3)

    composer require olafnorge/console-mutex
    
  2. Use olafnorge\Console\WithoutOverlapping trait:, (*4)

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

Strategies

Overlapping can be prevented by various strategies:, (*5)

  • 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., (*6)

You can change mutex strategy by specifying $mutexStrategy field:, (*7)

class ExampleCommand extends Command
{
    use WithoutOverlapping;

    protected $mutexStrategy = 'mysql';

    // ...
}

Or by using setMutexStrategy method:, (*8)

class ExampleCommand extends Command
{
    use WithoutOverlapping;

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

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

    // ...
}

Advanced

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., (*9)

You can change mutex timeout by specifying $mutexTimeout field:, (*10)

class ExampleCommand extends Command
{
    use WithoutOverlapping;

    protected $mutexTimeout = 3000; // milliseconds

    // ...
}

Or by using setMutexTimeout method:, (*11)

class ExampleCommand extends Command
{
    use WithoutOverlapping;

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

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

    // ...
}

There are three possible options for $mutexTimeout field:, (*12)

  • 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

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:, (*13)

class ExampleCommand extends Command
{
    use WithoutOverlapping;

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

    // ...
}

Troubleshooting

Trait included, but nothing happens?

Note, that WithoutOverlapping trait is overriding initialize method:, (*14)

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:, (*15)

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?

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, which is protected against overlapping:, (*16)

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:, (*17)

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

But don't worry, solution is very simple. Override initialize method by yourself, and initialize traits in required order:, (*19)

class ExampleCommand extends Command
{
    use Loggable;
    use WithoutOverlapping;

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

    // ...
}

The Versions

19/04 2017

dev-master

9999999-dev

Prevents overlapping for Laravel console commands.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dmitry Ivanov
by Volker Machon

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
by Volker Machon

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