robsonalvesbh/semaphoro

There is no license information available for the latest version (v1.0.1) of this package.

This library will help you to run multiple process with PHP.

v1.0.1 2019-01-29 00:43 UTC

This package is auto-updated.

Last update: 2024-04-29 03:48:04 UTC


README

Scrutinizer Code Quality Code Coverage Build Status Code Intelligence Status

Semaphoro

This library will help you to run multiple process with PHP.

Semaphoro library performs the orchestration of the processes avoiding that two or more workers that are running in parallel run the same processes, avoiding duplication of processes and still have a contingency to process again in case some process fails.

How to use

Set a storage

Parameters
  • Predis/Client $redisClient - required
  • String $prefix - optional (default value: semaphoro)

prefix is a namespace for redis.

$redis = new Redis($redisClient, $prefix);

Set a handler

Parameters
  • StorageInterface $storage - required
  • int $rangeLength - optional (default value: 50)

rangeLength is the quantity of process in a range

$rangeHandler = new RangeHandler($storage, $rangeLength);

The RangeHandler is projected to work with incremental numbers like ID

Get semaphoro

Parameters
  • HandlerInterface $handler - required
$semaphoro = new Semaphoro($handler);
Methods
getAvailableProcess()

Get the next range available

$semaphoro->getAvailableProcess();
return
  • ProcessInterface
setUnprocessed()

Set unprocessed status when occurring an error

Parameters
  • ProcessInterface $process - required
$semaphoro->setUnprocessed($process);
return
  • void
remove()

Remove process from semaphoro when the process is finished

Parameters
  • ProcessInterface $process - required
$semaphoro->remove($process);
return
  • void

Code example

<?php
require_once 'vendor/autoload.php';

use Predis\Client;
use Semaphoro\Handlers\RangeHandler;
use Semaphoro\Semaphoro;
use Semaphoro\Storages\Redis;

$redis = new Redis(new Client([
    'scheme' => 'tcp',
    'host' => 'redis',
    'port' => 6379,
]));
$rangeHandler = new RangeHandler($redis);
$semaphoro = new Semaphoro($rangeHandler);
$process = $semaphoro->getAvailableProcess();

try {
    /**
     * YOUR CODE HERE
     */

    $semaphoro->remove($process);
} catch (Throwable $e) {
    $semaphoro->setUnprocessed($process);
}

Credits