Skip to content

Commit aa5e748

Browse files
committed
增加异步命令执行,更新文档
1 parent 656638c commit aa5e748

File tree

4 files changed

+92
-15
lines changed

4 files changed

+92
-15
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
Encapsulate communication of services.
66

7-
**!!! At this time the package is still in developmental stage and should not be used in production.**
8-
97
## Installation
108

119
Require this package with composer:
@@ -43,21 +41,36 @@ use Per3evere\Preq\AbstractCommand;
4341
class Example extends AbstractCommand
4442
{
4543
/**
46-
* 执行命令
44+
* 同步执行命令.
4745
*
4846
* @return void
4947
*/
5048
public function run()
5149
{
5250
return 'run!';
5351
}
52+
53+
/**
54+
* 异步执行命令.
55+
*
56+
* @return \Guzzlehttp\Promise\Promise;
57+
*/
58+
public function runAsync()
59+
{
60+
// 返回注意返回类型.
61+
}
5462
}
5563
```
5664

5765
execute it
5866

5967
```php
6068
$command = app('preq')->getCommand(\App\Services\Example::class);
69+
70+
// 同步执行命令
6171
echo $command->execute();
72+
73+
// 异步执行命令
74+
$command->queue();
6275
```
6376

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
],
1414
"require": {
1515
"php": ">=5.6.4",
16-
"illuminate/support": "~5.1"
16+
"illuminate/support": "~5.1",
17+
"guzzlehttp/promises": "^1.0"
1718
},
1819
"require-dev": {
1920
"phpunit/phpunit": "~4.0|~5.0"

src/AbstractCommand.php

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Per3evere\Preq\Contract\Command as CommandContract;
99
use Illuminate\Support\Arr;
1010
use Throwable;
11+
use GuzzleHttp\Promise\PromiseInterface;
1112

1213
abstract class AbstractCommand implements CommandContract
1314
{
@@ -131,16 +132,6 @@ public function setRequestLog(RequestLog $requestLog)
131132
$this->requestLog = $requestLog;
132133
}
133134

134-
/**
135-
* 获取请求日志.
136-
*
137-
* @return void
138-
*/
139-
public function getRequestLog()
140-
{
141-
return $this->requestLog;
142-
}
143-
144135
/**
145136
* 初始化配置.
146137
*/
@@ -189,7 +180,7 @@ private function isRequestCacheEnabled()
189180

190181

191182
/**
192-
* 执行命令
183+
* 执行命令,同步
193184
* 加入处理逻辑.
194185
*
195186
* @return mixed
@@ -245,6 +236,68 @@ public function execute()
245236
return $result;
246237
}
247238

239+
/**
240+
* 异步执行命令.
241+
*
242+
* @return mixed
243+
*/
244+
public function queue()
245+
{
246+
$this->prepare();
247+
$metrics = $this->getMetrics();
248+
$cacheEnabled = $this->isRequestCacheEnabled();
249+
250+
$this->recordExecutedCommand();
251+
252+
if ($cacheEnabled) {
253+
$cacheHit = $this->requestCache->exists($this->getCommandKey(), $this->getCacheKey());
254+
255+
if ($cacheHit) {
256+
$metrics->markResponseFromCache();
257+
$this->recordExecutionEvent(self::EVENT_RESPONSE_FROM_CACHE);
258+
return $this->requestCache->get($this->getCommandKey(), $this->getCommandKey());
259+
}
260+
}
261+
262+
$circuitBreaker = $this->getCircuitBreaker();
263+
264+
if (! $circuitBreaker->allowRequest()) {
265+
$metrics->markShortCircuited();
266+
$this->recordExecutionEvent(self::EVENT_SHORT_CIRCUITED);
267+
return $this->getFallbackOrThrowException();
268+
}
269+
270+
$this->invocationStartTime = $this->getTimeInMilliseconds();
271+
272+
$promise = $this->runAsync();
273+
274+
if ($promise instanceof PromiseInterface) {
275+
$promise->then(
276+
function ($value) use ($metrics, $circuitBreaker) {
277+
$this->recordExecutionTime();
278+
$metrics->markSuccess();
279+
$circuitBreaker->markSuccess();
280+
$this->recordExecutionEvent(self::EVENT_SUCCESS);
281+
},
282+
function ($reason) use ($metrics) {
283+
$this->recordExecutionTime();
284+
285+
if ($reason instanceof BadRequestException) {
286+
throw $reason;
287+
}
288+
289+
$metrics->markFailure();
290+
$this->executionException = $reason;
291+
$this->recordExecutionEvent(self::EVENT_FAILURE);
292+
return Promise\promise_for($this->getFallbackOrThrowException($reason));
293+
}
294+
);
295+
296+
return $promise;
297+
}
298+
}
299+
300+
248301
/**
249302
* 执行命令前置操作.
250303
*/

src/CommandFactory.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,14 @@ public function getCommand($class)
8787

8888
return $command;
8989
}
90+
91+
/**
92+
* 获取请求日志.
93+
*
94+
* @return void
95+
*/
96+
public function getRequestLog()
97+
{
98+
return $this->requestLog;
99+
}
90100
}

0 commit comments

Comments
 (0)