1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of PHPinnacle/Ensign. |
4
|
|
|
* |
5
|
|
|
* (c) PHPinnacle Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace PHPinnacle\Ensign; |
14
|
|
|
|
15
|
|
|
use Amp\Deferred; |
16
|
|
|
use Amp\Loop; |
17
|
|
|
use Amp\Promise; |
18
|
|
|
|
19
|
|
|
final class Task implements Promise |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
private $id; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Promise |
28
|
|
|
*/ |
29
|
|
|
private $promise; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Token |
33
|
|
|
*/ |
34
|
|
|
private $token; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param int $id |
38
|
|
|
* @param Promise $promise |
39
|
|
|
* @param Token $token |
40
|
|
|
*/ |
41
|
12 |
|
public function __construct(int $id, Promise $promise, Token $token) |
42
|
|
|
{ |
43
|
12 |
|
$this->id = $id; |
44
|
12 |
|
$this->promise = $promise; |
45
|
12 |
|
$this->token = $token; |
46
|
12 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return int |
50
|
|
|
*/ |
51
|
2 |
|
public function id(): int |
52
|
|
|
{ |
53
|
2 |
|
return $this->id; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $reason |
58
|
|
|
* |
59
|
|
|
* @return Task |
60
|
|
|
*/ |
61
|
1 |
|
public function cancel(string $reason = ''): self |
62
|
|
|
{ |
63
|
1 |
|
$this->token->cancel($this->id, $reason); |
64
|
|
|
|
65
|
1 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param int $timeout |
70
|
|
|
* |
71
|
|
|
* @return self |
72
|
|
|
*/ |
73
|
2 |
|
public function timeout(int $timeout): self |
74
|
|
|
{ |
75
|
2 |
|
$deferred = new Deferred; |
76
|
2 |
|
$resolved = false; |
77
|
|
|
|
78
|
2 |
|
$watcher = Loop::delay($timeout, function () use ($deferred, &$resolved) { |
79
|
1 |
|
if ($resolved) { |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
$resolved = true; |
84
|
|
|
|
85
|
1 |
|
$deferred->fail(new Exception\TaskTimeout($this->id)); |
86
|
2 |
|
}); |
87
|
2 |
|
Loop::unreference($watcher); |
88
|
|
|
|
89
|
2 |
|
$promise = $this->promise; |
90
|
2 |
|
$promise->onResolve(function () use ($deferred, $watcher, &$resolved) { |
91
|
1 |
|
if ($resolved) { |
92
|
1 |
|
return; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
$resolved = true; |
96
|
|
|
|
97
|
1 |
|
Loop::cancel($watcher); |
98
|
|
|
|
99
|
1 |
|
$deferred->resolve($this->promise); |
100
|
2 |
|
}); |
101
|
|
|
|
102
|
2 |
|
$this->promise = $deferred->promise(); |
103
|
|
|
|
104
|
2 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
12 |
|
public function onResolve(callable $onResolved) |
111
|
|
|
{ |
112
|
12 |
|
$this->token->guard(); |
113
|
11 |
|
$this->promise->onResolve($onResolved); |
114
|
11 |
|
} |
115
|
|
|
} |
116
|
|
|
|