forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRetryProcessRequest.php
More file actions
221 lines (169 loc) · 6.12 KB
/
RetryProcessRequest.php
File metadata and controls
221 lines (169 loc) · 6.12 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
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
<?php
namespace ProcessMaker;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use ProcessMaker\Facades\WorkflowManager;
use ProcessMaker\Jobs\RunScriptTask;
use ProcessMaker\Jobs\RunServiceTask;
use ProcessMaker\Models\Comment;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestLock;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\User;
use ProcessMaker\Nayra\Bpmn\Models\ScriptTask;
use ProcessMaker\Nayra\Bpmn\Models\ServiceTask;
use ProcessMaker\Nayra\Contracts\Bpmn\ScriptTaskInterface;
use ProcessMaker\Repositories\BpmnDocument;
class RetryProcessRequest
{
public static array $output = [];
private static array $taskTypes = [];
private ProcessRequest $processRequest;
private BpmnDocument $bpmnDefinitions;
public function __construct(ProcessRequest $processRequest)
{
$this->processRequest = $processRequest;
$this->bpmnDefinitions = $this->getBpmnDefinitions();
$this->determineTaskTypes();
}
public static function for(ProcessRequest $processRequest): self
{
return new self($processRequest);
}
public function isChildRequest(): bool
{
return $this->processRequest->parentRequest()->exists();
}
public function hasRetriableTasks(): bool
{
return $this->retriableTasksQuery()->exists();
}
public function getRetriableTasks(): Collection
{
return $this->retriableTasksQuery()->get();
}
public function hasNonRetriableTasks(): bool
{
$currentTaskTypes = static::$taskTypes;
$this->determineTaskTypes(true);
$tasksQuery = $this->retriableTasksQuery();
$tasksQuery = $tasksQuery->whereNotIn('element_type', $currentTaskTypes);
$hasNonRetriableTasks = $tasksQuery->exists();
$this->determineTaskTypes();
return $hasNonRetriableTasks;
}
public function retry(): void
{
if (!$this->hasRetriableTasks()) {
return;
}
$this->unlockProcessRequest();
$this->getRetriableTasks()->each(function (ProcessRequestToken $token) {
// Load the token instance
$token = $token->loadTokenInstance();
// Get the task to retry from the BPMN definitions
$task = $this->bpmnDefinitions->getEvent($element = $token->element_id);
if (!$task) {
return;
}
$this->clearTokenErrors($token);
if ($task instanceof ScriptTask) {
WorkflowManager::runScripTask($task, $token);
} elseif ($task instanceof ServiceTask) {
WorkflowManager::runServiceTask($task, $token);
}
static::$output[] = $this->formatOutput($task, $element, $token);
});
$this->createRequestComment();
}
/**
* Clear previous ProcessRequestToken errors off of its parent ProcessRequest(s)
*
* @param \ProcessMaker\Models\ProcessRequestToken $token
*
* @return void
*/
public function clearTokenErrors(ProcessRequestToken $token): void
{
$errors = collect($token->processRequest->errors ?? []);
if ($errors->isEmpty()) {
return;
}
$errors = $errors->keyBy('element_id');
if ($errors->has($element = $token->element_id)) {
$errors->forget($element);
}
$token->processRequest->errors = $errors->all();
if (!$token->processRequest->isNonPersistent()) {
$token->processRequest->save();
}
}
public function formatOutput($task, $element, $token): string
{
return 'Retrying ' . class_basename($task) . " ({$element}) for Request::{$token->processRequest->id}";
}
public function initializingUser(): User|bool
{
return Auth::check() ? Auth::user() : false;
}
public function createRequestComment(): void
{
$comment = (new Comment)->fill([
'type' => 'LOG',
'commentable_type' => ProcessRequest::class,
'commentable_id' => $this->processRequest->id,
'subject' => __('Request Retried'),
]);
if ($user = $this->initializingUser()) {
$comment->setAttribute('user_id', $user->id);
$comment->setAttribute('body', $user->fullname . ' ' . __('retried the request from an error state'));
} else {
$comment->setAttribute('body', __('Request was retried from an error state'));
}
$comment->save();
}
private function determineTaskTypes(bool $all = false): void
{
if ($all || app()->runningInConsole()) {
static::$taskTypes = ['scriptTask', 'serviceTask', 'task'];
} else {
static::$taskTypes = ['scriptTask'];
}
}
public function retriableTasksQuery(): HasMany
{
$tokensQuery = $this->processRequest->tokens();
$tokensQuery->whereIn('status', ['FAILING', 'ACTIVE', 'ERROR']);
$tokensQuery->whereIn('element_type', static::$taskTypes);
return $tokensQuery;
}
public function unlockProcessRequest(): void
{
ProcessRequestLock::whereIn('process_request_id', $this->getRequestsFromTokens()->all())
->delete();
}
public function reactivateRequest(): bool
{
$reactivated = true;
$this->getRequestsFromTokens()->each(
function ($request) use ($reactivated) {
$success = ProcessRequest::findOrFail($request)
->fill(['status' => 'ACTIVE'])
->save();
if (!$success) {
$reactivated = false;
}
});
return $reactivated;
}
public function getRequestsFromTokens()
{
return $this->getRetriableTasks()->pluck('process_request_id');
}
public function getBpmnDefinitions(): BpmnDocument
{
return $this->processRequest->process->getDefinitions();
}
}