Skip to content

Commit

Permalink
added factory
Browse files Browse the repository at this point in the history
  • Loading branch information
kriswallsmith committed May 18, 2015
1 parent d41293b commit 01e69dc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
59 changes: 59 additions & 0 deletions src/Spork/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of Spork, an OpenSky project.
*
* (c) OpenSky Project Inc
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Spork;

use Spork\Batch\BatchJob;
use Spork\Batch\Strategy\StrategyInterface;

class Factory
{
/**
* Creates a new batch job instance.
*
* @param ProcessManager $manager The process manager
* @param null $data Data for the batch job
* @param StrategyInterface $strategy The strategy
*
* @return BatchJob A new batch job instance
*/
public function createBatchJob(ProcessManager $manager, $data = null, StrategyInterface $strategy = null)
{
return new BatchJob($manager, $data, $strategy);
}

/**
* Creates a new shared memory instance.
*
* @param integer $pid The child process id or null if this is the child
* @param integer $signal The signal to send after writing to shared memory
*
* @return SharedMemory A new shared memory instance
*/
public function createSharedMemory($pid = null, $signal = null)
{
return new SharedMemory($pid, $signal);
}

/**
* Creates a new fork instance.
*
* @param int $pid Process id
* @param SharedMemory $shm Shared memory
* @param bool $debug Debug mode
*
* @return Fork A new fork instance
*/
public function createFork($pid, SharedMemory $shm, $debug = false)
{
return new Fork($pid, $shm, $debug);
}
}
13 changes: 7 additions & 6 deletions src/Spork/ProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Spork;

use Spork\Batch\BatchJob;
use Spork\Batch\Strategy\StrategyInterface;
use Spork\EventDispatcher\EventDispatcher;
use Spork\EventDispatcher\EventDispatcherInterface;
Expand All @@ -24,16 +23,18 @@
class ProcessManager
{
private $dispatcher;
private $factory;
private $debug;
private $zombieOkay;
private $signal;

/** @var Fork[] */
private $forks;

public function __construct(EventDispatcherInterface $dispatcher = null, $debug = false)
public function __construct(EventDispatcherInterface $dispatcher = null, Factory $factory = null, $debug = false)
{
$this->dispatcher = $dispatcher ?: new EventDispatcher();
$this->factory = $factory ?: new Factory();
$this->debug = $debug;
$this->zombieOkay = false;
$this->forks = array();
Expand Down Expand Up @@ -72,7 +73,7 @@ public function zombieOkay($zombieOkay = true)

public function createBatchJob($data = null, StrategyInterface $strategy = null)
{
return new BatchJob($this, $data, $strategy);
return $this->factory->createBatchJob($this, $data, $strategy);
}

public function process($data, $callable, StrategyInterface $strategy = null)
Expand Down Expand Up @@ -101,7 +102,7 @@ public function fork($callable)
$this->forks = array();

// setup the shared memory
$shm = new SharedMemory(null, $this->signal);
$shm = $this->factory->createSharedMemory(null, $this->signal);
$message = new ExitMessage();

// phone home on shutdown
Expand Down Expand Up @@ -146,9 +147,9 @@ public function fork($callable)
}

// connect to shared memory
$shm = new SharedMemory($pid);
$shm = $this->factory->createSharedMemory($pid);

return $this->forks[$pid] = new Fork($pid, $shm, $this->debug);
return $this->forks[$pid] = $this->factory->createFork($pid, $shm, $this->debug);
}

public function monitor($signal = SIGUSR1)
Expand Down

0 comments on commit 01e69dc

Please sign in to comment.