PHP code example of asinfotrack / yii2-semaphore

1. Go to this page and download the library: Download asinfotrack/yii2-semaphore library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

asinfotrack / yii2-semaphore example snippets


return [
    //...
    'components' => [        
        //...        
        'semaphore' => [
            //use the file based implementation
            'class' => \asinfotrack\yii2\semaphore\components\FileSemaphore::class,
            'lockFolderAlias' => '@runtime/semaphores',
        ],
        //...
    ],
    //...
];

class SemaphoreDemoController extends \yii\console\Controller
{
    
    public function actionWaitForLock()
    {
        Yii::$app->semaphore->acquire('my-lock', true);

        //do the actual work

        Yii::$app->semaphore->release('my-lock');
    }
    
    public function actionSkipIfNotAvailable()
    {
        if (!Yii::$app->semaphore->acquire('my-lock', false)) {
            $this->stderr('Lock already taken');
            return ExitCode::UNAVAILABLE;
        }

        //do the actual work
    
        Yii::$app->semaphore->release('my-lock');
    }

}