-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Workflow.php
239 lines (194 loc) · 6.92 KB
/
Workflow.php
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
declare(strict_types=1);
namespace Workflow;
use BadMethodCallException;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Container\Container;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Routing\RouteDependencyResolverTrait;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
use React\Promise\PromiseInterface;
use Throwable;
use Workflow\Events\WorkflowCompleted;
use Workflow\Middleware\WithoutOverlappingMiddleware;
use Workflow\Models\StoredWorkflow;
use Workflow\Serializers\Y;
use Workflow\States\WorkflowCompletedStatus;
use Workflow\States\WorkflowRunningStatus;
use Workflow\States\WorkflowWaitingStatus;
use Workflow\Traits\Sagas;
class Workflow implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use RouteDependencyResolverTrait;
use Sagas;
use SerializesModels;
public int $tries = 0;
public int $maxExceptions = 0;
public $arguments;
public $coroutine;
public int $index = 0;
public $now;
public bool $replaying = false;
private Container $container;
public function __construct(
public StoredWorkflow $storedWorkflow,
...$arguments
) {
$this->arguments = $arguments;
if (property_exists($this, 'connection')) {
$this->onConnection($this->connection);
}
if (property_exists($this, 'queue')) {
$this->onQueue($this->queue);
}
$this->afterCommit = true;
}
public function query($method)
{
$this->replaying = true;
$this->handle();
return $this->{$method}();
}
public function middleware()
{
$parentWorkflow = $this->storedWorkflow->parents()
->first();
if ($parentWorkflow) {
return [
new WithoutOverlappingMiddleware($parentWorkflow->id, WithoutOverlappingMiddleware::ACTIVITY, 0, 15),
];
}
return [
new WithoutOverlappingMiddleware($this->storedWorkflow->id, WithoutOverlappingMiddleware::WORKFLOW, 0, 15),
];
}
public function failed(Throwable $throwable): void
{
try {
$this->storedWorkflow->toWorkflow()
->fail($throwable);
} catch (\Spatie\ModelStates\Exceptions\TransitionNotFound) {
return;
}
}
public function handle(): void
{
if (! method_exists($this, 'execute')) {
throw new BadMethodCallException('Execute method not implemented.');
}
$this->container = App::make(Container::class);
try {
if (! $this->replaying) {
$this->storedWorkflow->status->transitionTo(WorkflowRunningStatus::class);
}
} catch (\Spatie\ModelStates\Exceptions\TransitionNotFound) {
if ($this->storedWorkflow->toWorkflow()->running()) {
$this->release();
}
return;
}
$parentWorkflow = $this->storedWorkflow->parents()
->first();
$log = $this->storedWorkflow->logs()
->whereIndex($this->index)
->first();
$this->storedWorkflow
->signals()
->when($log, static function ($query, $log): void {
$query->where('created_at', '<=', $log->created_at->format('Y-m-d H:i:s.u'));
})
->each(function ($signal): void {
$this->{$signal->method}(...Y::unserialize($signal->arguments));
});
if ($parentWorkflow) {
$this->now = Carbon::parse($parentWorkflow->pivot->parent_now);
} else {
$this->now = $log ? $log->now : Carbon::now();
}
WorkflowStub::setContext([
'storedWorkflow' => $this->storedWorkflow,
'index' => $this->index,
'now' => $this->now,
'replaying' => $this->replaying,
]);
$this->coroutine = $this->{'execute'}(...$this->resolveClassMethodDependencies(
$this->arguments,
$this,
'execute'
));
while ($this->coroutine->valid()) {
$this->index = WorkflowStub::getContext()->index;
$nextLog = $this->storedWorkflow->logs()
->whereIndex($this->index)
->first();
if ($log) {
$this->storedWorkflow
->signals()
->where('created_at', '>', $log->created_at->format('Y-m-d H:i:s.u'))
->when($nextLog, static function ($query, $nextLog): void {
$query->where('created_at', '<=', $nextLog->created_at->format('Y-m-d H:i:s.u'));
})
->each(function ($signal): void {
$this->{$signal->method}(...Y::unserialize($signal->arguments));
});
}
$log = $nextLog;
$this->now = $log ? $log->now : Carbon::now();
WorkflowStub::setContext([
'storedWorkflow' => $this->storedWorkflow,
'index' => $this->index,
'now' => $this->now,
'replaying' => $this->replaying,
]);
$current = $this->coroutine->current();
if ($current instanceof PromiseInterface) {
$resolved = false;
$current->then(function ($value) use (&$resolved): void {
$resolved = true;
$this->coroutine->send($value);
});
if (! $resolved) {
if (! $this->replaying) {
$this->storedWorkflow->status->transitionTo(WorkflowWaitingStatus::class);
}
return;
}
} else {
throw new Exception('something went wrong');
}
}
if (! $this->replaying) {
try {
$return = $this->coroutine->getReturn();
} catch (Throwable $th) {
throw new Exception('Workflow failed.');
}
$this->storedWorkflow->output = Y::serialize($return);
$this->storedWorkflow->status->transitionTo(WorkflowCompletedStatus::class);
WorkflowCompleted::dispatch(
$this->storedWorkflow->id,
json_encode($return),
now()
->format('Y-m-d\TH:i:s.u\Z')
);
if ($parentWorkflow) {
ChildWorkflow::dispatch(
$parentWorkflow->pivot->parent_index,
$this->now,
$this->storedWorkflow,
$return,
$parentWorkflow
);
}
}
}
}