t-kanstantsin/yii2-action-lock-behavior

Allow run only one action instance at once

2.0.0 2019-02-14 08:39 UTC

README

Yii widget

Latest Stable Version Total Downloads

Basic

Behavior allow deny multiple runs of same console application action (e.g. long time executing task initiated with cron) using yii\mutex\* package.

Lock source should be chosen carefully noting following:

  • yii\mutex\FileMutex simple but can be used only with one docker container instance because its not possible determine if process still running or ended in another container. Requires only writable directory

  • yii\mutex\DbMutex requires db connection. May be used only with single database instance.

Example

Using mutex from global config:


    public function behaviors(): array
    {
        return [
            'pid' => ActionLockBehavior::class,
        ];
    }

Define mutex on-the-fly:


    public function behaviors(): array
    {
        return [
            'pid' => [
                'class' => ActionLockBehavior::class,
                'mutex' => [
                    'class' => FileMutex::class,
                    'mutexPath' => \Yii::getAlias('@runtime/pid'),
                ],
            ],
        ];
    }