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\Container\TaskRootTree;
use Psr\Container\ContainerInterface;

class MyTaskTree extends TaskRootTree
{
    public function getNodes(): iterable
    {
        yield $cacheClear = $this->group('cache-clear', [
            $this->task(ClearCacheTask::class),
            $this->task(ClearLogsTask::class),
        ]);
        yield $this->task(OtherCacheClearTask::class)->belongsTo($cacheClear);
        
        yield $this->task(MyFirstTask::class);
        yield $secondTask = $this->task(MySecondTask::class);
        yield $thirdTask = $this->task(MyThirdTask::class)->dependsOn($secondTask);
        yield $this->partitioned(MyPartitionedTask::class, 4)->dependsOn($thirdTask, $cacheClear);
        
    }
}

use Multitron\Multitron;
use Multitron\Console\MultitronConfig;
use Multitron\Error\TracyErrorHandler;
use Symfony\Component\Console\Application;

/** @var \Multitron\Container\TaskRootTree $taskTree */
$taskTree = new MyTaskTree($container);
$config = new MultitronConfig(
    bootstrapPath: '/path/to/bootstrap.php', // Path to the bootstrap file that returns an instance of a PSR container or Nette Container
    concurrentTasks: 4, // Number of concurrent tasks
    errorHandler: new TracyErrorHandler(), // see below for PSR logger
);

$application = new Application();
$application->add($taskTree->buildCommand());
$application->run();

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Multitron\Error\PsrLogErrorHandler;

$logger = new Logger('multitron');
$logger->pushHandler(new StreamHandler('path/to/logfile.log', Logger::ERROR));

$errorHandler = new PsrLogErrorHandler($logger);

$errorHandler = new Multitron\Error\TracyErrorHandler();

use Multitron\Output\TableOutput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;

$tableOutput = new TableOutput();
$tableOutput->configure($inputConfiguration);

use Multitron\Container\Node\PartitionedTaskNodeGroup;

$partitionedNode = new PartitionedTaskNodeGroup("MyPartitionedTask", function() use ($container) {
    return $container->get(MyPartitionedTask::class);
}, 4); // Partition into 4 chunks

   $data = $comm->read('task_result');
   if ($data !== null) {
       // Process the retrieved data
   } else {
       // Handle the absence of data
   }
   

   $subData = $comm->readSubset('user_emails', [123,126]); // Retrieve emails for user IDs 123 and 126
   // Note: Similarly to array_intersect_key(), only the existing keys are returned
   $email = $subData[123] ?? null;
   

   $dataToStore = [123 => '[email protected]', 124 => '[email protected]'];
   $comm->write('user_emails', $dataToStore)->await();
   

   $newResults = ['subtask1' => ['status' => 'done'], 'subtask2' => ['status' => 'pending']];
   $comm->merge('project_results', $newResults, 1)->await();
   // The project_results key will have its data updated without overwriting existing entries