Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
117 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* The below illustrates how you can provide closure callbacks to the run a method on each completion of a process/job. | ||
* The idea is to provide a way to make decisions on the result of the job, when it is done. | ||
* | ||
* I order to achieve this configuration, a Configuration object is passed in. | ||
* | ||
* 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\Configuration; | ||
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, (new Configuration())->setCompletedCallback(function(Process $process) { | ||
|
||
})); | ||
|
||
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
namespace Afrihost\SwarmProcess; | ||
|
||
class Configuration | ||
{ | ||
/** @var callable */ | ||
private $completedCallback; | ||
|
||
/** | ||
* @return callable | ||
*/ | ||
public function getCompletedCallback() | ||
{ | ||
return $this->completedCallback; | ||
} | ||
|
||
/** | ||
* @param callable $completedCallback | ||
* @return Configuration | ||
* @throws \Exception | ||
*/ | ||
public function setCompletedCallback($completedCallback) | ||
{ | ||
if (!is_callable($completedCallback)) { | ||
throw new \Exception('Must provide callable as callback'); | ||
} | ||
|
||
$this->completedCallback = $completedCallback; | ||
return $this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters