deepeloper/lib-process

PHP-process Lock library

1.0.1 2024-07-01 08:39 UTC

This package is auto-updated.

Last update: 2025-03-29 00:54:25 UTC


README

Packagist version PHP from Packagist GitHub license GitHub issues Packagist CI codecov

Donation

Compatibility

PHP 7.4

Installing

Run composer require deepeloper/lib-process.

Usage

use deepeloper\Lib\Process\Lock;
use deepeloper\Lib\Process\Lock\Storage;

try {
    $lock = new Lock(
        Storage::getLayer("Filesystem", ['path' => "path/to/lock"]),
        60 * 5, // 5 minutes
        true, // Destroy previous lock, false by default
        // "...", // custom lock id
    );
} catch (RuntimeException $e) {
    switch ($e->getCode()) {
        case Lock::EXISTING_IS_VALID:
            // Previous lock is valid, interrupt process.
            die;
        default:
            throw $e;
    }
}

// Long time loop
while (true) {
    // ...
    try {
        $lock->update(true); // true to call \set_time_limit(), false by default
    } catch (RuntimeException $e) {
        // Lock was destroyed by another instance of the daemon
    }
}