forked from geekdevs/spork
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from DACSoftware/master
Merge pool processing feature
Showing
6 changed files
with
171 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Spork; | ||
|
||
use Spork\Exception\UnexpectedTypeException; | ||
|
||
abstract class AbstractJob | ||
{ | ||
protected $manager; | ||
protected $data; | ||
protected $name; | ||
protected $callback; | ||
|
||
public function __construct(ProcessManager $manager, $data = null) | ||
{ | ||
$this->manager = $manager; | ||
$this->data = $data; | ||
$this->name = '<anonymous>'; | ||
} | ||
|
||
public function setName($name) | ||
{ | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
|
||
public function setData($data) | ||
{ | ||
$this->data = $data; | ||
|
||
return $this; | ||
} | ||
|
||
public function setCallback($callback) | ||
{ | ||
if (!is_callable($callback)) { | ||
throw new UnexpectedTypeException($callback, 'callable'); | ||
} | ||
|
||
$this->callback = $callback; | ||
|
||
return $this; | ||
} | ||
|
||
public function execute($callback = null) | ||
{ | ||
if (null !== $callback) { | ||
$this->setCallback($callback); | ||
} | ||
|
||
return $this->manager->fork($this)->setName($this->name); | ||
} | ||
|
||
/** | ||
* Runs in a child process. | ||
* | ||
* @see execute() | ||
*/ | ||
abstract public function __invoke(); | ||
|
||
} |
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
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,58 @@ | ||
<?php | ||
|
||
namespace Spork\Pool; | ||
|
||
use Spork\AbstractJob; | ||
use Spork\Batch\BatchRunner; | ||
use Spork\ProcessManager; | ||
|
||
class PoolJob extends AbstractJob | ||
{ | ||
protected $poolSize; | ||
|
||
public function __construct(ProcessManager $manager, $data, $pollSize = 3) | ||
{ | ||
parent::__construct($manager, $data); | ||
|
||
$this->poolSize = $pollSize; | ||
} | ||
|
||
/** | ||
* Runs in a child process. | ||
* | ||
* @see execute() | ||
*/ | ||
public function __invoke() | ||
{ | ||
$forks = array(); | ||
$results = array(); | ||
$batches = $this->data; | ||
$index = 0; | ||
while (count($batches) > 0) { | ||
while (count($forks) < $this->poolSize) { | ||
$batch = array_splice($batches, 0, 1); | ||
$fork = $this->manager->fork(new BatchRunner($batch, $this->callback)) | ||
->setName(sprintf('%s part #%d', $this->name, $index)); | ||
$forks[$fork->getPid()] = $fork; | ||
$index++; | ||
} | ||
do { | ||
$endedFork = $this->manager->waitForNext(); | ||
} while (!isset($endedFork)); | ||
|
||
$results = array_merge($results, $endedFork->getResult()); | ||
unset($forks[$endedFork->getPid()]); | ||
|
||
$endedFork = null; | ||
} | ||
// block until all forks have exited | ||
$this->manager->wait(); | ||
|
||
|
||
foreach ($forks as $fork) { | ||
$results = array_merge($results, $fork->getResult()); | ||
} | ||
|
||
return $results; | ||
} | ||
} |
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