Skip to content

Commit

Permalink
Merge e1c6586 into c39bd55
Browse files Browse the repository at this point in the history
  • Loading branch information
sarelvdwalt committed Feb 25, 2019
2 parents c39bd55 + e1c6586 commit 05f4171
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 17 deletions.
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -95,6 +95,19 @@ $swarm->run(
);
```

#### Completion Callback

As of version 1.2 you will be able to provide a callback to be called upon completion of each process. The aim ere is to use it to ascertain what the exitCode was, for example. A use case would be to reschedule the process in the case of failure, or to log te failure for a human to look into.

Here's how you would use it:

```php
$swarmProcess = new SwarmProcess($logger);
$swarmProcess->setCompletedCallback(function(Symfony\Component\Process\Process $process) {
// do something with the $process returned, checking it's exit code, and perhaps putting it back on the stack
});
```

#### Examples:

You may also look at the examples provided in the `examples` folder. Run them using:
Expand Down
41 changes: 41 additions & 0 deletions examples/simple-run-with-completion-callback.php
@@ -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();
32 changes: 32 additions & 0 deletions src/Configuration.php
@@ -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;
}

}
14 changes: 4 additions & 10 deletions src/SwarmProcess.php
@@ -1,10 +1,4 @@
<?php
/**
* User: sarel
* Date: 2015/12/29
* Time: 17:48
*/

namespace Afrihost\SwarmProcess;

use Symfony\Component\Process\Process;
Expand Down Expand Up @@ -75,6 +69,10 @@ public function tick()
}
unset($this->currentRunningStack[$runningProcessKey]);
$this->logger->info($logMessage);

if (is_callable($this->getConfiguration()->getCompletedCallback())) {
call_user_func($this->getConfiguration()->getCompletedCallback(), $runningProcess);
}
}
}

Expand Down Expand Up @@ -170,7 +168,6 @@ public function setMaxRunStackSize($maxRunStackSize)
return $this;
}


/**
* Get the number of successful processes that have completed.
*
Expand All @@ -181,7 +178,4 @@ public function getSuccessfulProcessCount()
return $this->successfulProcessCount;
}




}
34 changes: 27 additions & 7 deletions src/SwarmProcessBase.php
@@ -1,10 +1,4 @@
<?php
/**
* User: sarel
* Date: 2016/01/18
* Time: 08:16
*/

namespace Afrihost\SwarmProcess;

use Psr\Log\LoggerAwareTrait;
Expand All @@ -15,15 +9,41 @@ abstract class SwarmProcessBase
{
use LoggerAwareTrait;

/** @var $configuration Configuration */
protected $configuration;

/**
* @return Configuration
*/
public function getConfiguration()
{
return $this->configuration;
}

/**
* @param Configuration $configuration
* @return SwarmProcessBase
*/
public function setConfiguration($configuration)
{
$this->configuration = $configuration;
return $this;
}

/**
* SwarmProcess constructor.
*
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger = null)
public function __construct(LoggerInterface $logger = null, Configuration $configuration = null)
{
$this->setLogger($logger ?: new NullLogger());

$this->logger->debug('__construct(ed) SwarmProcess');

if (null === $configuration) {
$configuration = new Configuration();
}
$this->configuration = $configuration;
}
}

0 comments on commit 05f4171

Please sign in to comment.