Skip to content

Commit

Permalink
2023.09.30 Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
monkenWu committed Sep 30, 2023
2 parents 60928d4 + bd919e5 commit bda1fcd
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 146 deletions.
2 changes: 1 addition & 1 deletion develop/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"codeigniter4/framework": "^4",
"psr/http-message": "^1.0",
"guzzlehttp/guzzle": "7.5.0",
"sdpmlab/anser-action": "^0.3.3",
"sdpmlab/anser-action": "^0.3.10",
"predis/predis": "^2.0",
"friendsofphp/php-cs-fixer": "^3.13",
"zumba/json-serializer": "^3.0",
Expand Down
14 changes: 7 additions & 7 deletions develop/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 19 additions & 18 deletions src/Orchestration/Orchestrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class Orchestrator implements OrchestratorInterface
*/
protected ?SagaInterface $sagaInstance = null;

protected bool $useBackup = false;

/**
* The parameter of build funcion.
*
Expand Down Expand Up @@ -208,6 +210,7 @@ public function getFailActions(): array
final public function build(...$args)
{
$this->argsArray = func_get_args();
$this->orchestratorNumber = static::class . '\\' . md5(json_encode($this->argsArray) . uniqid("", true)) . '\\' . date("Y-m-d H:i:s");

call_user_func_array(array($this, "definition"), $this->argsArray);

Expand All @@ -230,7 +233,10 @@ final public function build(...$args)
*/
protected function handleSingleStep(StepInterface $step)
{
$cacheInstance = CacheFactory::getCacheInstance();
$cacheInstance = null;
if($this->useBackup){
$cacheInstance = CacheFactory::getCacheInstance();
}

// 將當前 Step 紀錄於 Saga
if (!is_null($this->sagaInstance)) {
Expand Down Expand Up @@ -264,12 +270,13 @@ protected function handleSingleStep(StepInterface $step)
*/
public function startAllStep(CacheHandlerInterface $cacheInstance = null)
{
$cacheInstance = $cacheInstance ?? CacheFactory::getCacheInstance();

$this->orchestratorNumber = static::class . '\\' . md5(json_encode($this->argsArray) . uniqid("", true)) . '\\' . date("Y-m-d H:i:s");
$cacheInstance = null;
if($this->useBackup){
$cacheInstance = CacheFactory::getCacheInstance();
}

// Set up the cache info/variable if developer set the cache instance.
if (!is_null($cacheInstance)) {
if ($this->useBackup) {
$this->cacheInitial($cacheInstance);
}

Expand Down Expand Up @@ -297,7 +304,7 @@ public function startAllStep(CacheHandlerInterface $cacheInstance = null)
// 當所有 Step 執行完成且都執行成功,則清除在快取的編排器
// 並儲存 Log 進資料庫
if (!is_null($cacheInstance)) {
$cacheInstance->clearOrchestrator($this->serverName, $this->orchestratorNumber);
$cacheInstance->clearOrchestrator($this);
}
}

Expand All @@ -321,24 +328,18 @@ protected function cacheInitial(CacheHandlerInterface $cacheInstance)
if ($this->sagaInstance === null) {
throw OrchestratorException::forSagaInstanceNotFoundInCache();
}

if (getenv("serverName")) {
$this->serverName = getenv("serverName");
}

if ($this->serverName === null) {
throw OrchestratorException::forServerNameNotFound();
}

$cacheInstance->initOrchestrator($this->serverName, $this->orchestratorNumber, $this);
$cacheInstance->initOrchestrator($this);
}

/**
* @deprecated
*/
public function reStartRuntimeOrchestrator()
{
$cacheInstance = CacheFactory::getCacheInstance();
$cacheInstance = null;
if($this->useBackup){
$cacheInstance = CacheFactory::getCacheInstance();
}

foreach ($this->steps as $step) {
if ($step->isSuccess() === true) {
Expand All @@ -359,7 +360,7 @@ public function reStartRuntimeOrchestrator()
}

if (!is_null($cacheInstance)) {
$cacheInstance->clearOrchestrator($this->orchestratorNumber);
$cacheInstance->clearOrchestrator($this);
}

$result = $this->defineResult();
Expand Down
1 change: 0 additions & 1 deletion src/Orchestration/OrchestratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use SDPMlab\Anser\Orchestration\StepInterface;
use SDPMlab\Anser\Exception\OrchestratorException;
use SDPMlab\Anser\Service\ActionInterface;
use SDPMlab\Anser\Orchestration\Saga\Cache\CacheHandlerInterface;
use SDPMlab\Anser\Orchestration\Saga\SagaInterface;

interface OrchestratorInterface
Expand Down
7 changes: 6 additions & 1 deletion src/Orchestration/Saga/Cache/BaseCacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use SDPMlab\Anser\Orchestration\Saga\Cache\CacheHandlerInterface;
use SDPMlab\Anser\Orchestration\OrchestratorInterface;
use Zumba\JsonSerializer\JsonSerializer;
use Zumba\JsonSerializer\ClosureSerializer\SuperClosureSerializer;
use Pingyi\JsonSerializer\ClosureSerializer\ClosureSerializer;

abstract class BaseCacheHandler implements CacheHandlerInterface
Expand All @@ -18,7 +19,11 @@ abstract class BaseCacheHandler implements CacheHandlerInterface

public function __construct()
{
$this->serializer = new JsonSerializer(new ClosureSerializer());
$this->serializer = new JsonSerializer();
$this->serializer->addClosureSerializer(new SuperClosureSerializer(
new ClosureSerializer()
));

}

public function serializeOrchestrator(OrchestratorInterface $orchestrator): string
Expand Down
9 changes: 6 additions & 3 deletions src/Orchestration/Saga/Cache/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

namespace SDPMlab\Anser\Orchestration\Saga\Cache;

use SDPMlab\Anser\Orchestration\Saga\Cache\Redis\RedisHandler;
use SDPMlab\Anser\Orchestration\Saga\Cache\Redis\PRedisHandler;
use SDPMlab\Anser\Orchestration\Saga\Cache\Redis\PhpRedisHandler;
use SDPMlab\Anser\Orchestration\Saga\Cache\CacheHandlerInterface;
use SDPMlab\Anser\Exception\RedisException;

class CacheFactory
{
const CACHE_DRIVER_PREDIS = "predis";

/**
* The mapping list of Cache Drivers instance.
*
* @var array
*/
private static $cacheMapping = [
"Redis" => RedisHandler::class
"predis" => PRedisHandler::class,
];

/**
Expand All @@ -35,7 +38,7 @@ class CacheFactory
* Initial the cache driver.
*
* @param string $driver
* @param string|array|null $config
* @param mixed $config
* @param string|array|null $option
* @return CacheHandlerInterface
*/
Expand Down
26 changes: 8 additions & 18 deletions src/Orchestration/Saga/Cache/CacheHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface CacheHandlerInterface
* @param OrchestratorInterface $runtimeOrchestrator
* @return CacheHandlerInterface
*/
public function initOrchestrator(string $serverName, string $orchestratorNumber, OrchestratorInterface $runtimeOrchestrator): CacheHandlerInterface;
public function initOrchestrator(OrchestratorInterface $runtimeOrchestrator): CacheHandlerInterface;

/**
* Set the runtime orchestor into redis after each step has finished.
Expand All @@ -32,33 +32,23 @@ public function setOrchestrator(OrchestratorInterface $runtimeOrchestrator): Cac
* @param string|null $orchestratorNumber
* @return boolean
*/
public function clearOrchestrator(string $serverName = null, string $orchestratorNumber = null): bool;
public function clearOrchestrator(OrchestratorInterface $runtimeOrchestrator): bool;

/**
* Get the runtime orchestor into redis by using orchestratorNumber.
*
* @param string|null $serverName
* @param string|null $orchestratorNumber
* @return OrchestratorInterface
*/
public function getOrchestrator(string $serverName = null, string $orchestratorNumber = null): OrchestratorInterface;

/**
* Get all runtime orchestrators by serverName.
* Get all runtime orchestrators by class name.
*
* @param string|null $serverName
* @param string $className
* @return array|null
* @return array<string,OrchestratorInterface> array<orchestratorNumber, OrchestratorInterface>
*/
public function getOrchestratorsByServerName(string $serverName = null, string $className): ?array;
public function getOrchestrators(string $className, ?string $serverName = null): ?array;

/**
* Get all runtime orchestrators by class name.
* Get all runtime orchestrators by class name with each server.
*
* @param string $className
* @return array|null <serverName, array>
* @return array<string,<string,OrchestratorInterface>> array<serverName, array<orchestratorNumber, OrchestratorInterface>>
*/
public function getOrchestratorsByClassName(string $className): ?array;
public function getServersOrchestrator(string $className): ?array;

/**
* Serialize the meta data of orchestrator,
Expand Down
70 changes: 70 additions & 0 deletions src/Orchestration/Saga/Cache/Redis/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace SDPMlab\Anser\Orchestration\Saga\Cache\Redis;

class config
{
/**
* The scheme of Redis.
*
* @var string
*/
public $scheme = 'tcp';

/**
* The host of Redis.
*
* @var string
*/
public $host = '127.0.0.1';

/**
* The port of Redis.
*
* @var integer
*/
public $port = 6379;

/**
* The timeout of Redis.
*
* @var integer|float
*/
public $timeout = 0;

/**
* The db of Redis.
*
* @var integer
*/
public $db = 0;

/**
* The serverName as the hashmap key.
* You can set this value to identify the server.
*
* @var string
*/
public $serverName = 'AnserServer';

public function __construct(
string $scheme = 'tcp',
string $host = 'localhost',
int $port = 6379,
int $timeout = 0,
int $db = 0,
string $serverName = 'AnserServer'
) {
$this->scheme = $scheme;
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
$this->db = $db;
$this->serverName = $serverName;
}

public function __sleep()
{
return ['scheme', 'host', 'port', 'timeout', 'db', 'serverName'];
}
}
Loading

0 comments on commit bda1fcd

Please sign in to comment.