Skip to content

Commit

Permalink
[Continuation] Changed the ContinuationServer::__construct() method t…
Browse files Browse the repository at this point in the history
…o receive a GC object. (Issue piece#4)
  • Loading branch information
iteman committed Sep 18, 2012
1 parent b01ecfa commit d13a26f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
20 changes: 7 additions & 13 deletions src/Piece/Flow/Continuation/ContinuationServer.php
Expand Up @@ -60,7 +60,6 @@ class ContinuationServer
protected $activeFlowID;
protected $activeFlowExecutionTicket;
protected $gc;
protected $enableGC = false;
protected $flowExecution;

/**
Expand All @@ -80,18 +79,13 @@ class ContinuationServer

/**
* @param \Piece\Flow\PageFlow\PageFlowRepository $pageFlowRepository
* @param integer $enableGC
* @param integer $gcExpirationTime
* @param \Piece\Flow\Continuation\GC $gc
*/
public function __construct(PageFlowRepository $pageFlowRepository, $enableGC = false, $gcExpirationTime = 1440)
public function __construct(PageFlowRepository $pageFlowRepository, GC $gc = null)
{
if ($enableGC) {
$this->gc = new GC($gcExpirationTime);
$this->enableGC = true;
}

$this->flowExecution = new FlowExecution();
$this->pageFlowRepository = $pageFlowRepository;
$this->gc = $gc;
}

/**
Expand All @@ -117,7 +111,7 @@ public function addFlow($flowID, $source, $isExclusive = false)
*/
public function invoke($payload)
{
if ($this->enableGC) {
if (!is_null($this->gc)) {
$this->gc->setGCCallback(array($this->flowExecution, 'disableFlowExecution'));
$this->gc->mark();
}
Expand All @@ -130,7 +124,7 @@ public function invoke($payload)
$this->startFlowExecution($payload);
}

if ($this->enableGC && !$this->isExclusive()) {
if (!is_null($this->gc) && !$this->isExclusive()) {
$this->gc->update($this->activeFlowExecutionTicket);
}

Expand Down Expand Up @@ -211,7 +205,7 @@ public function clear()
$this->activeFlowID = null;
$this->activeFlowExecutionTicket = null;
$this->flowExecution->inactivateFlowExecution();
if ($this->enableGC) {
if (!is_null($this->gc)) {
$this->gc->sweep();
}
}
Expand Down Expand Up @@ -317,7 +311,7 @@ protected function prepare()
*/
protected function continueFlowExecution($payload)
{
if ($this->enableGC) {
if (!is_null($this->gc)) {
if ($this->gc->isMarked($this->activeFlowExecutionTicket)) {
$this->flowExecution->removeFlowExecution($this->activeFlowExecutionTicket, $this->activeFlowID);
throw new FlowExecutionExpiredException('The flow execution for the given flow execution ticket has expired.');
Expand Down
2 changes: 1 addition & 1 deletion src/Piece/Flow/Continuation/GC.php
Expand Up @@ -57,7 +57,7 @@ class GC
*
* @param integer $expirationTime
*/
public function __construct($expirationTime)
public function __construct($expirationTime = 1440)
{
$this->expirationTime = $expirationTime;
}
Expand Down
8 changes: 4 additions & 4 deletions test/Piece/Flow/Continuation/ContinuationServerTest.php
Expand Up @@ -548,7 +548,7 @@ public function testGettingFlowExecutionTicketByFlowName()
public function testFlowExecutionExpiredExceptionShouldBeRaisedWhenFlowExecutionHasExpired()
{
$flowName = 'FlowExecutionExpired';
$server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true)), true, 1);
$server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true)), new GC(1));
$server->addFlow($flowName, "{$this->cacheDirectory}/$flowName.yaml");
$server->setEventNameCallback(array($this, 'getEventName'));
$server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket'));
Expand All @@ -575,7 +575,7 @@ public function testFlowExecutionExpiredExceptionShouldBeRaisedWhenFlowExecution
public function testFlowExecutionExpiredExceptionShouldNotBeRaisedWhenFlowExecutionHasNotExpired()
{
$flowName = 'FlowExecutionExpired';
$server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true)), true, 2);
$server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true)), new GC(2));
$server->addFlow($flowName, "{$this->cacheDirectory}/$flowName.yaml");
$server->setEventNameCallback(array($this, 'getEventName'));
$server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket'));
Expand Down Expand Up @@ -617,7 +617,7 @@ public function testNewFlowExecutionShouldBeAbleToStartWithSameRequestAfterFlowE
{
$flowName = 'FlowExecutionExpired';
$this->flowID = $flowName;
$server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true)), true, 1);
$server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true)), new GC(1));
$server->addFlow($flowName, "{$this->cacheDirectory}/$flowName.yaml");
$server->setEventNameCallback(array($this, 'getEventName'));
$server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket'));
Expand Down Expand Up @@ -916,7 +916,7 @@ public function testFlowExecutionShouldWorkWithConfigDirectory()
public function testFlowExecutionExpiredExceptionShouldRaiseAfterSweepingIt()
{
$flowName = 'FlowExecutionExpired';
$server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true)), true, 1);
$server = new ContinuationServer(new PageFlowRepository(new PageFlowCacheFactory($this->cacheDirectory, true)), new GC(1));
$server->addFlow($flowName, "{$this->cacheDirectory}/$flowName.yaml");
$server->setEventNameCallback(array($this, 'getEventName'));
$server->setFlowExecutionTicketCallback(array($this, 'getFlowExecutionTicket'));
Expand Down

0 comments on commit d13a26f

Please sign in to comment.