-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSagaRedisController.php
More file actions
124 lines (111 loc) · 4.7 KB
/
SagaRedisController.php
File metadata and controls
124 lines (111 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
declare(strict_types=1);
/**
* This file is part of DTM-PHP.
*
* @license https://github.com/dtm-php/dtm-sample/blob/master/LICENSE
*/
namespace App\Controller;
use DtmClient\Saga;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Redis\Redis;
#[Controller(prefix: '/saga/redis')]
class SagaRedisController extends AbstractSagaController
{
#[Inject]
protected Redis $redis;
#[RequestMapping(path: 'successCase')]
public function successCase(Saga $saga): string
{
$this->initAccountAmount(100);
$payload = $this->buildPayload(50);
$saga->init();
$saga->add($this->serviceUri . '/saga/redis/transOut', $this->serviceUri . '/saga/redis/transOutCompensate', $payload);
$saga->add($this->serviceUri . '/saga/redis/transIn', $this->serviceUri . '/saga/redis/transInCompensate', $payload);
$saga->submit();
return 'Submitted';
}
#[RequestMapping(path: 'rollbackCase')]
public function rollbackCase(Saga $saga): string
{
$this->initAccountAmount(20);
$payload = $this->buildPayload(50);
$saga->init();
$saga->add($this->serviceUri . '/saga/redis/transOut', $this->serviceUri . '/saga/redis/transOutCompensate', $payload);
$saga->add($this->serviceUri . '/saga/redis/transIn', $this->serviceUri . '/saga/redis/transInCompensate', $payload);
$saga->submit();
return 'Submitted';
}
#[RequestMapping(path: 'concurrentCase')]
public function concurrentCase(Saga $saga): string
{
$this->initAccountAmount(100);
$payload = $this->buildPayload(50);
$saga->init();
$saga->add($this->serviceUri . '/saga/redis/transOut', $this->serviceUri . '/saga/redis/transOutCompensate', $payload);
$saga->add($this->serviceUri . '/saga/redis/transOut', $this->serviceUri . '/saga/redis/transOutCompensate', $payload);
$saga->add($this->serviceUri . '/saga/redis/transIn', $this->serviceUri . '/saga/redis/transInCompensate', $payload);
$saga->add($this->serviceUri . '/saga/redis/transIn', $this->serviceUri . '/saga/redis/transInCompensate', $payload);
$saga->enableConcurrent();
$saga->addBranchOrder(2, [0, 1]);
$saga->addBranchOrder(3, [0, 1]);
$saga->submit();
return 'Submitted';
}
#[RequestMapping(path: 'transOut')]
public function transOut(RequestInterface $request, ResponseInterface $response): string|ResponseInterface
{
$amount = $request->input('amount');
if (is_null($amount)) {
return $response->withStatus(409, 'Amount is required');
}
$result = $this->redisCheckAdjustAmount($this->getRedisAccountKey(self::TRANS_OUT_ID), -$amount, 7 * 86400);
if ($result === false) {
return $response->withStatus(409);
}
return $response->withStatus(200);
}
#[RequestMapping(path: 'transOutCompensate')]
public function transOutCompensate(RequestInterface $request, ResponseInterface $response): ResponseInterface
{
$amount = $request->input('amount');
if (is_null($amount)) {
return $response->withStatus(409, 'Amount is required');
}
$result = $this->redisCheckAdjustAmount($this->getRedisAccountKey(self::TRANS_OUT_ID), $amount, 7 * 86400);
if ($result === false) {
return $response->withStatus(409);
}
return $response->withStatus(200);
}
#[RequestMapping(path: 'transIn')]
public function transIn(RequestInterface $request, ResponseInterface $response): ResponseInterface
{
$amount = $request->input('amount');
if (is_null($amount)) {
return $response->withStatus(409, 'Amount is required');
}
$result = $this->redisCheckAdjustAmount($this->getRedisAccountKey(self::TRANS_IN_ID), $amount, 7 * 86400);
if ($result === false) {
return $response->withStatus(409);
}
return $response->withStatus(200);
}
#[RequestMapping(path: 'transInCompensate')]
public function transInCompensate(RequestInterface $request, ResponseInterface $response): ResponseInterface
{
$amount = $request->input('amount');
if (is_null($amount)) {
return $response->withStatus(409, 'Amount is required');
}
$result = $this->redisCheckAdjustAmount($this->getRedisAccountKey(self::TRANS_IN_ID), -$amount, 1800);
if ($result === false) {
return $response->withStatus(409);
}
return $response->withStatus(200);
}
}