linio / lock
Simple lock handler for cli applications
Installs: 1 560
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 58
Forks: 0
Open Issues: 1
Requires
- php: >=7.0
Requires (Dev)
- mikey179/vfsstream: ^1.6
- phpspec/phpspec: ^3.0
This package is auto-updated.
Last update: 2024-10-28 19:23:55 UTC
README
Linio lock is a small library to handle locks in CLI applications.
Install
The recommended way to install Linio lock is through composer.
{ "require": { "linio/lock": "^0.1" } }
Tests
To run the test suite, you need install the dependencies via composer, then run phpspec.
$ composer install
$ phpspec run
Usage
The following example shows all the features of linio/lock:
<?php use Linio\Lock\Lock; // Define options for the forced release. $options = getopt('f', ['force']); // Create the lock instance. $lock = new Lock('lock_name'); // Create the lock file with the pid inside. $lock->acquire(); // Check if the application is locked. if ($lock->isLocked()) { // If the '-f' or '--force' cli option is set. if (isset($options['f']) || isset($options['force'])) { // Release the lock killing the running process. $lock->forceRelease(); } else { // Do not execute the application if it is locked. die('Another instance of the application is running'); } } Application::run(); // Release the lock after the execution $lock->release();
To-do
- Abstract locking mechanisms, allowing another methods beyond file lock.
- Properly test the
Linio\Lock\Lock
class (achievable after the development of the first item)