PHP code example of riki137 / multitron

1. Go to this page and download the library: Download riki137/multitron 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/ */

    

riki137 / multitron example snippets


use Multitron\Comms\TaskCommunicator;
use Multitron\Execution\Task;

final class HelloTask implements Task
{
    public function execute(TaskCommunicator $comm): void
    {
        $comm->log('Hello from a worker');
    }
}

use Multitron\Console\AbstractMultitronCommand;
use Multitron\Orchestrator\TaskOrchestrator;
use Multitron\Tree\TaskTreeBuilder;
use Multitron\Tree\TaskTreeBuilderFactory;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'app:tasks')]
final class MyCommand extends AbstractMultitronCommand
{
    public function __construct(TaskTreeBuilderFactory $factory, TaskOrchestrator $orchestrator)
    {
        parent::__construct($factory, $orchestrator);
    }

    public function getNodes(TaskTreeBuilder $b): void
    {
        $cache = $b->group('cache-clear', [
            $b->service(ClearCacheTask::class),
            $b->service(ClearLogsTask::class),
        ]);

        $b->service(OtherCacheClearTask::class, [$cache]);
        $b->service(MyFirstTask::class);
        $second = $b->service(MySecondTask::class);
        $third = $b->service(MyThirdTask::class, [$second]);
        $b->partitioned(MyPartitionedTask::class, 4, [$third, $cache]);
    }
}

use Multitron\Comms\TaskCommunicator;

final class MyTask implements Task
{
    public function execute(TaskCommunicator $comm): void
    {
        $comm->cache->write(['foo' => ['bar' => 'baz']], 2);
        $baz = $comm->cache->read(['foo' => ['bar']])->await()['foo']['bar']; // baz
        $comm->cache->write(['stats' => ['hits' => ($values['stats']['hits'] ?? 0) + 1]], 2);
    }
}

final class DownloadTask implements Task
{
    public function execute(TaskCommunicator $comm): void
    {
        $comm->progress->setTotal(100);
        for ($i = 0; $i < 100; $i++) {
            // ... work
            $comm->progress->addDone();
        }
    }
}

use Multitron\Tree\Partition\PartitionedTask;

final class BuildReportTask extends PartitionedTask
{
    public function execute(TaskCommunicator $comm): void
    {
        $comm->log("processing part {$this->partitionIndex} of {$this->partitionCount}");
    }
}

$builder->partitioned(BuildReportTask::class, 4);

final class ProcessUsersTask implements Task
{
    public function execute(TaskCommunicator $comm): void
    {
        $limit = (int)($comm->getOption('limit') ?? 0);
        // ... process with the given $limit
    }
}

use Multitron\Orchestrator\Output\ChainProgressOutputFactory;
use Multitron\Orchestrator\Output\TableOutputFactory;

$factory = new ChainProgressOutputFactory(
    new TableOutputFactory(),
    new JsonOutputFactory(), // your custom factory
);
$orchestrator = new TaskOrchestrator($ipc, $container, $execFactory, $factory, $handlerFactory);