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\TaskCommand;
use Multitron\Tree\TaskNode;
use Multitron\Tree\TaskTreeBuilder;
use Symfony\Component\Console\Attribute\AsCommand;
#[AsCommand(name: 'app:tasks')]
final class MyCommand extends TaskCommand
{
/**
* @return TaskNode[]
*/
public function getNodes(TaskTreeBuilder $b): array
{
return [
$cache = $b->group('cache-clear', [
$b->service(ClearCacheTask::class),
$b->service(ClearLogsTask::class),
]),
$b->service(AfterCacheClearTask::class, dependencies: [$cache]),
$first = $b->service(MyFirstTask::class),
$second = $b->service(MySecondTask::class, dependencies: [$first]),
$anotherSecond = $b->service(MyAnotherSecondTask::class, dependencies: [$first]),
$b->partitioned(MyPartitionedTask::class, 4, dependencies: [$second, $cache]),
];
}
}
use Multitron\Comms\TaskCommunicator;
final class MyTask implements Task
{
public function execute(TaskCommunicator $comm): void
{
$comm->cache->write(['foo' => ['bar' => 'baz']]);
$baz = $comm->cache->read(['foo' => ['bar']])->await()['foo']['bar']; // returns "baz"
// Read existing values first, then update
$existing = $comm->cache->read(['stats' => ['hits']])->await();
$hits = $existing['stats']['hits'] ?? 0;
$comm->cache->write(['stats' => ['hits' => $hits + 1]]);
}
}
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 // or implements PartitionedTaskInterface, use PartitionedTaskTrait
{
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;
use Multitron\Bridge\Native\MultitronFactory;
$factory = new MultitronFactory();
$outputFactory = new ChainProgressOutputFactory(
$factory->getProgressOutputFactory(),
new JsonOutputFactory(), // your own class for custom output
);
$factory->setProgressOutputFactory($outputFactory);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.