1. Go to this page and download the library: Download drlenux/workers-run library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
drlenux / workers-run example snippets
use DrLenux\WorkersRun\messenger\SemaphoreDb;
use DrLenux\WorkersRun\messenger\config\SemaphoreDbConfig;
use DrLenux\WorkersRun\lock\SemaphoreLock;
return [
'messenger' => [
'lockStore' => SemaphoreLock::getStore(),
'db' => [
'class' => SemaphoreDb::class,
'config' => new SemaphoreDbConfig(123, 500),
]
]
];
namespace example;
use DrLenux\WorkersRun\Messenger;
use DrLenux\WorkersRun\IWork;
use DrLenux\WorkersRun\MessengerResponse;
/**
* Class Work
* @package example
*/
class Work implements IWork
{
const U_SLEEP = 1000000;
/**
* @var float|int
*/
private $time = 0;
/**
* @var string
*/
private $message = '';
/**
* Work constructor.
* @param float $time
* @param string $message
*/
public function __construct(float $time, string $message)
{
$this->time = $time;
$this->message = $message;
}
/**
* @param Messenger $messenger
* @return MessengerResponse|null
*/
public function run(Messenger $messenger): ?MessengerResponse
{
usleep($this->time * self::U_SLEEP);
return (new MessengerResponse(WorkMessageEcho::class, getmypid(), $this->message));
}
}
namespace example;
use DrLenux\WorkersRun\Messenger;
use DrLenux\WorkersRun\IWork;
use DrLenux\WorkersRun\MessengerResponse;
/**
* Class WorkMessageEcho
* @package example
*/
class WorkMessageEcho implements IWork
{
/**
* @param Messenger $messenger
* @return MessengerResponse|null
* @throws \Exception
*/
public function run(Messenger $messenger): ?MessengerResponse
{
$count = 0;
$maxCount = 5;
while (true) {
if ($count++ >= $maxCount) {
break;
}
$res = $messenger->read(__CLASS__);
if (count($res)) {
foreach ($res as $item) {
/** @var MessengerResponse $item */
echo $item->getMessage();
}
$count = 0;
}
usleep(250000);
}
return null;
}
}