Skip to content

Commit

Permalink
2023.09.17 Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
monkenWu committed Sep 17, 2023
2 parents 9983bc6 + fc9a7c7 commit 8bea6a6
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 25 deletions.
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ version: "3"
services:
anser_service:
image: webdevops/php-nginx-dev:8.1
platform: linux/x86_64
ports:
- 8080:80
working_dir: /app
Expand All @@ -17,6 +18,7 @@ services:
- './src:/src'
order_service:
image: webdevops/php-nginx-dev:8.1
platform: linux/x86_64
ports:
- 8081:80
working_dir: /app
Expand All @@ -31,6 +33,7 @@ services:
- './src:/src'
product_service:
image: webdevops/php-nginx-dev:8.1
platform: linux/x86_64
ports:
- 8082:80
working_dir: /app
Expand All @@ -45,6 +48,7 @@ services:
- './src:/src'
payment_service:
image: webdevops/php-nginx-dev:8.1
platform: linux/x86_64
ports:
- 8083:80
working_dir: /app
Expand All @@ -59,6 +63,7 @@ services:
- './src:/src'
anser_DB:
image: mysql:5.7
platform: linux/x86_64
environment:
MYSQL_DATABASE: ci4
MYSQL_ROOT_PASSWORD: root
Expand Down
11 changes: 11 additions & 0 deletions src/Exception/OrchestratorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,15 @@ public static function forStepExecuteFail($className, $motion): OrchestratorExce
{
return new self("The orchestrator - {$className} {$motion} Fail at " . date("Y-m-d H:i:s"));
}

public static function forDefinitionNotOverride($className): OrchestratorException
{
return new self("The orchestrator - {$className} definition not override.");
}

public static function forDefineFailResultNotOverride($className): OrchestratorException
{
return new self("The orchestrator - {$className} defineFailResult not override.");
}

}
46 changes: 31 additions & 15 deletions src/Orchestration/Orchestrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use SDPMlab\Anser\Orchestration\Saga\Cache\CacheHandlerInterface;
use SDPMlab\Anser\Service\ActionInterface;

abstract class Orchestrator implements OrchestratorInterface
class Orchestrator implements OrchestratorInterface
{
/**
* 儲存被新增的 step 實體
Expand Down Expand Up @@ -173,17 +173,15 @@ public function checkAliasInAllSteps(string $alias): bool
* 取得目前 Orchestrator Steps 中符合傳入別名的 Action 實體
*
* @param string $alias
* @return ActionInterface
* @throws OrchestratorException
* @return ?ActionInterface 回傳 Action 實體,若碰到不存在或尚未執行到的 Action 則回傳 null
*/
public function getStepAction(string $alias): ActionInterface
public function getStepAction(string $alias): ?ActionInterface
{
foreach ($this->steps as $step) {
if (!$step->aliasNonRepeat($alias)) {
return $step->getStepAction($alias);
}
}
throw OrchestratorException::forActionNotFound($alias);
}

/**
Expand Down Expand Up @@ -219,7 +217,11 @@ final public function build(...$args)

$this->startAllStep();

$result = $this->defineResult();
if($this->isSuccess()){
$result = $this->defineResult();
} else{
$result = $this->defineFailResult();
}

return $result;
}
Expand Down Expand Up @@ -256,6 +258,7 @@ protected function handleSingleStep(StepInterface $step)
$this->isSuccess = false;
}
}

}

/**
Expand All @@ -276,18 +279,20 @@ public function startAllStep(CacheHandlerInterface $cacheInstance = null)

foreach ($this->steps as $step) {
$this->handleSingleStep($step);

//若有執行交易,中止 Step 的執行,並開始補償

if ($this->isSuccess() === false) {
if (is_null($this->sagaInstance) === false) {
if (!$this->startOrchCompensation() || is_null($this->startOrchCompensation())) {
throw OrchestratorException::forStepExecuteFail(static::class, "compensate");
if($this->defineFailResult() instanceof OrchestratorException) {
throw OrchestratorException::forStepExecuteFail(static::class, "compensate");
}
}
} else {
throw OrchestratorException::forStepExecuteFail(static::class, "run");
if($this->defineFailResult() instanceof OrchestratorException) {
throw OrchestratorException::forStepExecuteFail(static::class, "run");
}
}

break;
}
}
Expand Down Expand Up @@ -384,20 +389,31 @@ public function isCompensationSuccess()
}

/**
* 定義 Orchestrator 的編排細節。開發者必須覆寫這個方法。
* 定義 Orchestrator 的編排細節。
* 開發者必須覆寫這個方法。
*
* @return void
*/
abstract protected function definition();
protected function definition(){
throw OrchestratorException::forDefinitionNotOverride(static::class);
}

/**
* 定義整個 Orchestrator 成功後的回傳內容。
* 開發者可以覆寫這個方法。
* 開發者可以覆寫這個方法,若沒有覆寫則會回傳 isSuccess 的值
*
* @return void
*/
protected function defineResult()
{
return $this->isSuccess;
}

/**
* 定義整個 Orchestrator 失敗後的回傳內容。
* 開發者若沒有覆寫這個方法,則會在失敗時拋出例外。
*
*/
protected function defineFailResult()
{
return OrchestratorException::forDefinitionNotOverride(static::class);;
}
}
5 changes: 2 additions & 3 deletions src/Orchestration/OrchestratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ public function checkAliasInAllSteps(string $alias): bool;
* 取得目前 Orchestrator Steps 中符合傳入別名的 Action 實體
*
* @param string $alias
* @return ActionInterface
* @throws OrchestratorException
* @return ?ActionInterface
*/
public function getStepAction(string $alias): ActionInterface;
public function getStepAction(string $alias): ?ActionInterface;

/**
* 取得在所有 Step 中失敗的 Action 列表,若全數成功則回傳空陣列。
Expand Down
16 changes: 11 additions & 5 deletions src/Orchestration/Step.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,19 @@ protected function handleMultipleActions(): void
*
* @param string $alias 別名
* @param callable $getActionCallable 外部傳入的 Callable
* @return ActionInterface
* @return ?ActionInterface
* @throws StepException
*/
protected function handleActionCallable(
string $alias,
callable $getActionCallable
): ActionInterface {
): ?ActionInterface {
//如果 Orchestrator 執行失敗,則不執行 Callable,直接宣告 null
if($this->orchestrator->isSuccess() == false){
$this->actionList[$alias] = null;
return null;
}

$action = $getActionCallable($this->orchestrator);
if ($action instanceof ActionInterface) {
$this->actionList[$alias] = $action;
Expand All @@ -193,9 +199,9 @@ protected function handleActionCallable(
/**
* 取得 Action 實體
*
* @return ActionInterface
* @return ?ActionInterface 回傳 Action 實體,若不存在、尚未被執行則回傳 null
*/
public function getStepAction(string $alias): ActionInterface
public function getStepAction(string $alias): ?ActionInterface
{
// If the step is not run yet and it's action type callable,
// call the function to handle it.
Expand All @@ -204,7 +210,7 @@ public function getStepAction(string $alias): ActionInterface
}

if (!isset($this->actionList[$alias])) {
throw StepException::forUndefinedStepAction($alias);
return null;
}
return $this->actionList[$alias];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Orchestration/StepInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public function start(): StepInterface;
/**
* 取得 Action 實體
*
* @return ActionInterface
* @return ?ActionInterface
*/
public function getStepAction(string $alias): ActionInterface;
public function getStepAction(string $alias): ?ActionInterface;

/**
* 取得 Action 列表
Expand Down

0 comments on commit 8bea6a6

Please sign in to comment.