FileLock Class Reference
[General]

Include files. More...

Inheritance diagram for FileLock:

ILock

List of all members.

Public Member Functions

 FileLock ($lockPath_)
 Constructor.
 setLockPath ($lockPath_)
 Set the lock path.
 getLockPath ()
 Get The path to where the lock files are stored.
 deleteAllLocks ()
 Delete all lock files, both expired and not expired.
 isLockExpired ($lockName_, $lockTime_=60, $compressName_=true)
 Check if the file is locked.

Private Member Functions

 _getFileName ($lockName_, $compressName_)
 Returns a valid file path for the $lockName_.

Private Attributes

 $_lockPath
 The path to where to save the lock files.


Detailed Description

Include files.

Does a server global lock on a lock name.

A file with the lock name are saved to the file system, if that file is older then $lockTime_ seconds this function returns true.

This is used to see how long ago a certatin operation was done. Example, If it's more then 15 minutes since we last did send and SMS message we can now send a new.

Example:

 $lock = new FileLock('/tmp/locks/');
 if ($lock->_isLockExpired('SMS'))
 {
   // Send SMS
 }

 if ($lock->_isLockExpired('/usr/root/master/%more?d=true', 60, TRUE))
 {
   // Send SMS
 }

Author:
Daniel Lindh <[email protected]>

Definition at line 35 of file FileLock.class.php.


Member Function Documentation

FileLock::_getFileName ( lockName_,
compressName_ 
) [private]

Returns a valid file path for the $lockName_.

Definition at line 164 of file FileLock.class.php.

References getLockPath().

Referenced by isLockExpired().

00165   {
00166     if ($compressName_)
00167     {
00168       $lockName_ = md5($lockName_);
00169     }
00170 
00171     $fileName = $this->getLockPath() . md5($lockName_) . '.lock';
00172 
00173     return $fileName;
00174   }

FileLock::deleteAllLocks (  ) 

Delete all lock files, both expired and not expired.

Returns:
- boolean - true if files where deleted successfully.

Definition at line 92 of file FileLock.class.php.

References getLockPath().

00093   {
00094     $path = $this->getLockPath();
00095 
00096     if
00097     (
00098       !empty($path) &&
00099       $path != '/'
00100     )
00101     {
00102       exec('rm ' . $path . '*.lock', $arr, $retval);
00103       return true;
00104     }
00105     else
00106     {
00107       return false;
00108     }
00109   }

FileLock::FileLock ( lockPath_  ) 

Constructor.

Parameters:
$lockPath_ - string - The path to where to save the lock files. Ie. /tmp/locks/

Definition at line 44 of file FileLock.class.php.

References setLockPath().

00045   {
00046     $this->setLockPath($lockPath_);
00047   }

FileLock::getLockPath (  ) 

Get The path to where the lock files are stored.

Ie. /tmp/locks

Returns:
- string -

Definition at line 80 of file FileLock.class.php.

Referenced by _getFileName(), and deleteAllLocks().

00081   {
00082     // @todo add assertlog
00083     //assertLog(!empty($this->_lockPath), EL_LEVEL_3, ECAT_DIAGNOSTIC, 'Couldnt create lockPath: ' . $this->_lockPath);
00084     return $this->_lockPath;
00085   }

FileLock::isLockExpired ( lockName_,
lockTime_ = 60,
compressName_ = true 
)

Check if the file is locked.

An entity is locked in the system, if that entity was locked more then $lockTime_ seconds ago, this funtion returns true.

This is used to see how long ago a certatin operation was done. Example, If it's more then 1 minute since we last did send an SMS message we can now send a new.

Parameters:
$lockName_ - string - The name of the lock/variable/file.
$lockTime_ - string - Seconds the lockTitle is locked before expiring.
$compressName_ - boolean - true - An md5 will be done on the filename/lockName_.
Returns:
- boolean -

Implements ILock.

Definition at line 133 of file FileLock.class.php.

References _getFileName().

00138   {
00139     $fileName = $this->_getFileName($lockName_, $compressName_);
00140 
00141     $retVal = false;
00142     if
00143     (
00144       !file_exists($fileName) ||
00145       filemtime($fileName) < (time() - $lockTime_)
00146     )
00147     {
00148       $handle = @fopen($fileName, 'w');
00149 
00150       if ($handle !== false)
00151       {
00152         fwrite($handle, $lockName_);
00153         $retVal = true;
00154         fclose($handle);
00155       }
00156     }
00157 
00158     return $retVal;
00159   }

FileLock::setLockPath ( lockPath_  ) 

Set the lock path.

Parameters:
$lockPath_ - string - The path to where to save the lock files. Ie. /tmp/locks/

Definition at line 56 of file FileLock.class.php.

Referenced by FileLock().

00057   {
00058     $this->_lockPath = rtrim($lockPath_, '/') . '/';
00059 
00060     if (!file_exists($this->_lockPath))
00061     {
00062       if (!@mkdir($this->_lockPath, 0755, true))
00063       {
00064         // @todo add assertlog
00065         //assertLog(false, EL_LEVEL_3, ECAT_DIAGNOSTIC, 'Couldnt create lockPath: ' . $this->_lockPath);
00066 
00067         echo('Couldnt create path');
00068         exit;
00069       }
00070     }
00071   }


Member Data Documentation

FileLock::$_lockPath [private]

The path to where to save the lock files.

Ie. /tmp/locks/

Definition at line 180 of file FileLock.class.php.


The documentation for this class was generated from the following file:

Generated on Wed May 6 23:28:24 2009 for fareofficelib by  doxygen 1.5.8