Command queue library
This library delivers basic interfaces and tools to create command backend delegation system, which allows
passing tasks for processing by scallable backend worker infrastructure., (*1)
, (*2)
Library created in cooperation with Isobar Poland., (*3)
, (*4)
Installation
Step 1: Download the library
Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:, (*5)
$ composer require gendoria/command-queue "~0.2.0"
This command requires you to have Composer installed globally, as explained
in the installation chapter
of the Composer documentation., (*6)
Usage
This library provides building blocks to create command queue system, where you delgate
your tasks to a pools of backend workers for further processing., (*7)
By itself, it does have a simple implementation of 'direct' command queue,
where commands are not sent, but executed locally by a driver., (*8)
The simplest example of working queue chain using direct processing driver is below., (*9)
use Gendoria\CommandQueue\Command\CommandInterface;
use Gendoria\CommandQueue\CommandProcessor\CommandProcessorInterface;
use Gendoria\CommandQueue\ProcessorFactory\ProcessorFactory;
use Gendoria\CommandQueue\SendDriver\DirectProcessingDriver;
use Psr\Log\LoggerInterface;
class SimpleCommand implements CommandInterface
{
public $testData;
public function __construct($testData)
{
$this->testData = $testData;
}
}
class SimpleProcessor implements CommandProcessorInterface
{
/**
*
* @var LoggerInterface
*/
private $logger;
/**
* Process command.
*
* @param SimpleCommand $command
*/
public function process(CommandInterface $command)
{
echo "Command class: ".get_class($command)."\n";
echo "Command payload: ".$command->testData."\n";
echo "\n";
}
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function supports(CommandInterface $command)
{
return $command instanceof SimpleCommand;
}
}
$simpleProcessor = new SimpleProcessor();
$processorFactory = new ProcessorFactory();
$processorFactory->registerProcessorForCommand(SimpleCommand::class, $simpleProcessor);
$driver = new DirectProcessingDriver($processorFactory);
for ($k = 0; $k < 5; $k++) {
$command = new SimpleCommand("Test ".($k+1));
$driver->send($command);
sleep(1);
}
You can find this example in examples/direct.php
file. If you run it, you should see the following output:, (*10)
$ php example/direct.php
Command class: SimpleCommand
Command payload: Test 1
Command class: SimpleCommand
Command payload: Test 2
Command class: SimpleCommand
Command payload: Test 3
Command class: SimpleCommand
Command payload: Test 4
Command class: SimpleCommand
Command payload: Test 5
Direct processing driver is not very usable in real world application, as it does not delegate any tasks.
But you can use it at an early stage of your application development, when you know, for tasks you want
to delegate to backend processing at later date., (*11)