mutex Provides a locking mechanism with timeout functionality

  1. Documentation
  2. Change Log

This extension can be used in case a certain part of your application should only run ONCE at a time. For example you may have a cronjob console command that is executed every minute regardless of how long the action in the cronjob takes. See Mutex article on Wikipedia.

Resources

Documentation

Requirements
  • Yii 1.0 or above
Installation
  • Extract the release file under protected/extensions
  • Define component in config
'components' => array(
   ...
   'mutex' => array(
      'class' => 'application.extensions.EMutex',
   ),
   ...
),
Usage

See the following code example:

// Check if we have a lock already. If not set one which
// expires automatically after 10 minutes.
if (Yii::app()->mutex->lock('some-unique-id', 600))
{

   // Do some time-expensive stuff here...
   // sleep(10) as example

   // and after that release the lock...
   Yii::app()->mutex->unlock();

}
else
{
   // The lock does already exist so we exit
   echo "Already working on it...";
   exit;
}

The $timeout variable is there to ensure that the lock isn't infinite in case of things like a server crash. Means if you don't define a timeout and unlock() isn't called for some reason, the lock will stay forever.

All locks are represented in a single file. You can change the $mutexFile variable to change the path of this file (defaults to the Yii runtime path + /mutex.bin).

You can also define an $id when unlocking. That means in one cronjob you could setup a lock and in another one you could release that lock. For local created locks (current request), only unlock() works to make sure nested locks will get released in order.

Some more usage examples:

// Waiting for a lock to get released (spin lock)
// Make sure to call sleep() inside of the loop, because everytime
// you call lock(), the $mutexFile gets read (physical file-access).

while (!Yii::app()->mutex->lock('id'))
{
   sleep(1);
}

...

Yii::app()->mutex->unlock();
// Nested locks

if (Yii::app()->mutex->lock('id1'))
{

   while (!Yii::app()->mutex->lock('id2'))
   {
      sleep(1);
   }

   ...

   if (Yii::app()->mutex->lock('id3'))
   {

      ...

      Yii::app()->mutex->unlock(); // releases "id3"

   }

   ...

   Yii::app()->mutex->unlock(); // releases "id2"
   Yii::app()->mutex->unlock(); // releases "id1"

}

You may use __CLASS__ as $id for example in a console command ;-)

Change Log

April 12, 2010
  • Rewritten. Now using one file to store all the locks. See forum discussion and updated documentation for more info.
April 3, 2010
  • The application name is now considered for the unqiue lock file names. So it's possible to use one lock file directory for different applications without conflicts.
March 29, 2010
  • Initial release.
13 0
14 followers
1 960 downloads
Yii Version: 1.1
License: (not set)
Category: Others
Tags:
Developed by: Y!!
Created on: Mar 29, 2010
Last updated: 14 years ago

Downloads

show all