function useTick($concurrent, $numberOfJobs) { $logger = new Logger('swarm_logger'); $logger->pushProcessor(new MemoryUsageProcessor()); $swarm = new SwarmProcess($logger); $swarm->setMaxRunStackSize($concurrent); $counter = 0; // Now go run it: do { // If we have work to give the stack, then let's give it: if (++$counter <= $numberOfJobs) { $swarm->pushNativeCommandOnQueue(getCommand()); } } while ($swarm->tick()); }
<?php /** * This examples shows the simplest way of using the SwarmProcess class. * The logger is used purely to allow you to see what's happening. * If you don't provide the logger, the Psr\Log\NullLogger will be used internally. */ use Afrihost\SwarmProcess\SwarmProcess; use Monolog\Logger; chdir(__DIR__); require '../vendor/autoload.php'; $logger = new Logger('swarm_logger'); $swarmProcess = new SwarmProcess($logger); // Add a few things to do: $swarmProcess->pushNativeCommandOnQueue('sleep 9'); $swarmProcess->pushNativeCommandOnQueue('sleep 8'); $swarmProcess->pushNativeCommandOnQueue('sleep 7'); $swarmProcess->pushNativeCommandOnQueue('sleep 6'); $swarmProcess->pushNativeCommandOnQueue('sleep 5'); $swarmProcess->pushNativeCommandOnQueue('sleep 5'); $swarmProcess->pushNativeCommandOnQueue('sleep 4'); $swarmProcess->pushNativeCommandOnQueue('sleep 3'); $swarmProcess->pushNativeCommandOnQueue('sleep 2'); $swarmProcess->pushNativeCommandOnQueue('sleep 1'); $swarmProcess->setMaxRunStackSize(4); $swarmProcess->run();
/** * @expectedException \OutOfBoundsException */ public function testSetMaxRunStackSizeThrowsOutOfBounds() { $this->swarm->setMaxRunStackSize(-1); }
/** * The below illustrates how you can provide closure callbacks to the run method (you can provide either or both) * in order to have more (or the same) control over the queue and when it ends - while SwarmProcess is running * the jobs you gave it to run. * * This examples shows the simplest way of using the SwarmProcess class. * The logger is used purely to allow you to see what's happening. * If you don't provide the logger, the Psr\Log\NullLogger will be used internally. */ use Afrihost\SwarmProcess\SwarmProcess; use Monolog\Logger; use Symfony\Component\Process\Process; chdir(__DIR__); require '../vendor/autoload.php'; $logger = new Logger('swarm_logger'); $swarmProcess = new SwarmProcess($logger); // Add a few things to do: $swarmProcess->pushProcessOnQueue(new Process('sleep 9')); $swarmProcess->pushProcessOnQueue(new Process('sleep 8')); $swarmProcess->pushProcessOnQueue(new Process('sleep 7')); $swarmProcess->pushProcessOnQueue(new Process('sleep 6')); $swarmProcess->pushProcessOnQueue(new Process('sleep 5')); $swarmProcess->pushProcessOnQueue(new Process('sleep 5')); $swarmProcess->pushProcessOnQueue(new Process('sleep 4')); $swarmProcess->pushProcessOnQueue(new Process('sleep 3')); $swarmProcess->pushProcessOnQueue(new Process('sleep 2')); $swarmProcess->pushProcessOnQueue(new Process('sleep 1')); $swarmProcess->setMaxRunStackSize(4); /** * @return null|Process */
<?php /** * Almost exactly like simple-run.php - however this pushes the Process object. * * This examples shows the simplest way of using the SwarmProcess class. * The logger is used purely to allow you to see what's happening. * If you don't provide the logger, the Psr\Log\NullLogger will be used internally. */ use Afrihost\SwarmProcess\SwarmProcess; use Monolog\Logger; use Symfony\Component\Process\Process; chdir(__DIR__); require '../vendor/autoload.php'; $logger = new Logger('swarm_logger'); $swarmProcess = new SwarmProcess($logger); // Add a few things to do: $swarmProcess->pushProcessOnQueue(new Process('sleep 9')); $swarmProcess->pushProcessOnQueue(new Process('sleep 8')); $swarmProcess->pushProcessOnQueue(new Process('sleep 7')); $swarmProcess->pushProcessOnQueue(new Process('sleep 6')); $swarmProcess->pushProcessOnQueue(new Process('sleep 5')); $swarmProcess->pushProcessOnQueue(new Process('sleep 5')); $swarmProcess->pushProcessOnQueue(new Process('sleep 4')); $swarmProcess->pushProcessOnQueue(new Process('sleep 3')); $swarmProcess->pushProcessOnQueue(new Process('sleep 2')); $swarmProcess->pushProcessOnQueue(new Process('sleep 1')); $swarmProcess->setMaxRunStackSize(4); $swarmProcess->run();
* 2) The work stack is empty * * You can, off course, add an "|| true" to make it a never-ending loop. * * It is important, though, to protect your loop by wrapping your own code in a try-catch * * The logger is used purely to allow you to see what's happening. * If you don't provide the logger, the Psr\Log\NullLogger will be used internally. */ use Afrihost\SwarmProcess\SwarmProcess; use Monolog\Logger; use Symfony\Component\Process\Process; chdir(__DIR__); require '../vendor/autoload.php'; $logger = new Logger('swarm_logger'); $swarmProcess = new SwarmProcess($logger); // Build a list of things we'll be adding later on $add_these = array(); $add_these[] = new Process('sleep 9'); $add_these[] = new Process('sleep 8'); $add_these[] = new Process('sleep 7'); $add_these[] = new Process('sleep 6'); $add_these[] = new Process('sleep 5'); $add_these[] = new Process('sleep 5'); $add_these[] = new Process('sleep 4'); $add_these[] = new Process('sleep 3'); $add_these[] = new Process('sleep 2'); $add_these[] = new Process('sleep 1'); $swarmProcess->setMaxRunStackSize(4); do { try {