Console command mutex (without overlapping)

Posted 3 years ago by dmitry.g.ivanov

Hi there!

As we all know, there are some mechanisms in Laravel, which help you to prevent schedule tasks overlapping:

You can just use withoutOverlapping method, and this will protect you from running more than one instance of your schedule command at the same time.

But what if someone call your protected schedule command from console manually? withoutOverlapping method wouldn't help in such situation. It handles just scheduled executions of the command.

So, I've found that it is a problem for one of my projects, and I've created a small package, which will prevent Laravel console commands overlapping, no matter from where it was called. Few times manually, from schedule + manually, etc. No matter - it just protects your command from overlapping.

Here are the links, already available through composer via packagist:

See readme please, but generally speaking, all you have to do is use Illuminated\Console\WithoutOverlapping trait in your console command class:

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminated\Console\WithoutOverlapping;

class Foo extends Command
    use WithoutOverlapping;

    // ...

And now your command is protected against overlapping:

➜ php artisan foo:bar baz
Command is running now!

Also, there are several strategies for preventing overlapping:

  • file (default)
  • mysql
  • redis
  • memcached

See readme for more information.

Hope it would be useful for someone!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.