Permalink
Browse files

Add real AsyncCommand

  • Loading branch information...
fritsjanb committed Jul 14, 2014
1 parent 9504476 commit f99adff982e92bbab0959a564ca14adb1dbd0736
Showing with 55 additions and 23 deletions.
  1. +55 −23 public-shop-api/Odesk/Phystrix/AbstractAsyncCommand.php
@@ -28,7 +28,8 @@
/**
* All Phystrix commands must extend this class
*/
abstract class AbstractCommand
abstract class AbstractAsyncCommand
extends AbstractCommand /* trolo */
{
const EVENT_SUCCESS = 'SUCCESS';
const EVENT_FAILURE = 'FAILURE';
@@ -216,53 +217,70 @@ public function execute()
// always adding the command to request log
$this->recordExecutedCommand();
// trying from cache first
if ($this->isRequestCacheEnabled()) {
$fromCache = $this->requestCache->get($this->getCommandKey(), $this->getCacheKey());
if ($fromCache !== null) {
$this->executionEvents[] = self::EVENT_RESPONSE_FROM_CACHE;
$metrics->markResponseFromCache();
return $fromCache;
}
}
$circuitBreaker = $this->getCircuitBreaker();
if (!$circuitBreaker->allowRequest()) {
$this->executionEvents[] = self::EVENT_SHORT_CIRCUITED;
$metrics->markShortCircuited();
return $this->getFallbackOrThrowException();
return \React\Promise\resolve($this->getFallbackOrThrowException());
}
$this->invocationStartTime = $this->getTimeInMilliseconds();
try {
$result = $this->run();
$this->recordExecutionTime();
$this->executionEvents[] = self::EVENT_SUCCESS;
$metrics->markSuccess();
$circuitBreaker->markSuccess();
} catch (BadRequestException $exception) {
// Treated differently and allowed to propagate without any stats tracking or fallback logic
$this->recordExecutionTime();
throw $exception;
$result = $this->run(); // shoulde be a promise
$success = function($value) use ($metrics, $circuitBreaker) {
$this->recordExecutionTime();
$this->executionEvents[] = self::EVENT_SUCCESS;
$metrics->markSuccess();
$circuitBreaker->markSuccess();
return $value;
};
$error = function($exception) use ($metrics, $circuitBreaker) {
$this->recordExecutionTime();
$this->executionException = $exception;
$this->executionEvents[] = self::EVENT_FAILURE;
$metrics->markFailure();
return $this->getFallbackOrThrowException($exception);
//return \React\Promise\resolve($this->getFallbackOrThrowException($exception));
};
$result = $result->then($success, $error);
// todo: could be BadRequestException
// but if we hit this path we failed to even call run(), which
// should not happen, because it should return a failed promise
} catch (Exception $exception) {
// Treated differently and allowed to propagate without any stats tracking or fallback logic
$this->recordExecutionTime();
$this->executionException = $exception;
$this->executionEvents[] = self::EVENT_FAILURE;
$metrics->markFailure();
$result = $this->getFallbackOrThrowException($exception);
return \React\Promise\reject($exception);
}
// putting the result into cache
if ($this->isRequestCacheEnabled()) {
$this->requestCache->put($this->getCommandKey(), $this->getCacheKey(), $result);
}
return $result;
}
/**
* The code to be executed
*
* @return mixed
*/
abstract protected function run();
/**
* Custom preparation logic, preceding command execution
*/
@@ -432,4 +450,18 @@ private function recordExecutedCommand()
$this->requestLog->addExecutedCommand($this);
}
}
// customized
private $eventLoop;
// todo: typehint
public function getEventLoop()
{
return $this->eventLoop;
}
public function setEventLoop($eventLoop)
{
$this->eventLoop = $eventLoop;
}
}

0 comments on commit f99adff

Please sign in to comment.