linio/lock

用于 CLI 应用程序的单锁处理器

0.1.0 2016-08-12 00:32 UTC

This package is auto-updated.

Last update: 2024-08-28 19:01:54 UTC


README

Latest Stable Version License Build Status Scrutinizer Code Quality

Linio lock 是一个用于处理 CLI 应用程序中锁的轻量级库。

安装

安装 Linio lock 的推荐方法是通过 composer

{
    "require": {
        "linio/lock": "^0.1"
    }
}

测试

要运行测试套件,您需要通过 composer 安装依赖项,然后运行 phpspec。

$ composer install
$ phpspec run

用法

以下示例展示了 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();

待办事项

  • 抽象锁定机制,允许使用除文件锁之外的其他方法。
  • 正确测试 Linio\Lock\Lock 类(在开发第一项之后可实现)