|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProcessMaker; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Collection; |
| 6 | +use Illuminate\Database\Eloquent\Relations\HasMany; |
| 7 | +use Illuminate\Support\Facades\Auth; |
| 8 | +use Illuminate\Support\Facades\Log; |
| 9 | +use ProcessMaker\Jobs\RunScriptTask; |
| 10 | +use ProcessMaker\Jobs\RunServiceTask; |
| 11 | +use ProcessMaker\Models\Comment; |
| 12 | +use ProcessMaker\Models\ProcessRequest; |
| 13 | +use ProcessMaker\Models\ProcessRequestLock; |
| 14 | +use ProcessMaker\Models\ProcessRequestToken; |
| 15 | +use ProcessMaker\Models\User; |
| 16 | +use ProcessMaker\Nayra\Bpmn\Models\ScriptTask; |
| 17 | +use ProcessMaker\Nayra\Bpmn\Models\ServiceTask; |
| 18 | +use ProcessMaker\Repositories\BpmnDocument; |
| 19 | + |
| 20 | +class RetryProcessRequest |
| 21 | +{ |
| 22 | + public static array $output = []; |
| 23 | + |
| 24 | + private static array $taskTypes = []; |
| 25 | + |
| 26 | + private ProcessRequest $processRequest; |
| 27 | + |
| 28 | + private BpmnDocument $bpmnDefinitions; |
| 29 | + |
| 30 | + public function __construct(ProcessRequest $processRequest) |
| 31 | + { |
| 32 | + $this->processRequest = $processRequest; |
| 33 | + |
| 34 | + $this->bpmnDefinitions = $this->getBpmnDefinitions(); |
| 35 | + |
| 36 | + $this->determineTaskTypes(); |
| 37 | + } |
| 38 | + |
| 39 | + public static function for(ProcessRequest $processRequest): self |
| 40 | + { |
| 41 | + return new self($processRequest); |
| 42 | + } |
| 43 | + |
| 44 | + public function hasRetriableTasks(): bool |
| 45 | + { |
| 46 | + return $this->retriableTasksQuery()->exists(); |
| 47 | + } |
| 48 | + |
| 49 | + public function retry(): void |
| 50 | + { |
| 51 | + if (!$this->hasRetriableTasks()) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + $this->unlockProcessRequest(); |
| 56 | + |
| 57 | + $this->getRetriableTasks()->each(function (ProcessRequestToken $token) { |
| 58 | + // Get the task to retry from the BPMN definitions |
| 59 | + $task = $this->bpmnDefinitions->getEvent($element = $token->element_id); |
| 60 | + |
| 61 | + // $job will remain false if we can't find the correct |
| 62 | + // job type to dispatch in order to retry the task |
| 63 | + $job = false; |
| 64 | + |
| 65 | + // Find the correct job type to retry the task |
| 66 | + switch ($task::class) { |
| 67 | + case ScriptTask::class: |
| 68 | + $job = RunScriptTask::class; |
| 69 | + break; |
| 70 | + |
| 71 | + case ServiceTask::class: |
| 72 | + $job = RunServiceTask::class; |
| 73 | + break; |
| 74 | + } |
| 75 | + |
| 76 | + if (!$job) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + $output = 'Retrying ' . class_basename($task) . " ({$element}) for Request::{$token->processRequest->id}"; |
| 81 | + |
| 82 | + static::$output[] = $output; |
| 83 | + |
| 84 | + Log::info($output, [ |
| 85 | + 'user_initializing_retry' => $this->initializingUser(), |
| 86 | + 'token' => $token, |
| 87 | + ]); |
| 88 | + |
| 89 | + $job::dispatch($token->processRequest->process, $token->processRequest, $token, []); |
| 90 | + }); |
| 91 | + |
| 92 | + $this->createRequestComment(); |
| 93 | + } |
| 94 | + |
| 95 | + protected function initializingUser(): User|bool |
| 96 | + { |
| 97 | + return Auth::check() ? Auth::user() : false; |
| 98 | + } |
| 99 | + |
| 100 | + protected function createRequestComment(): void |
| 101 | + { |
| 102 | + $comment = (new Comment)->fill([ |
| 103 | + 'type' => 'LOG', |
| 104 | + 'commentable_type' => ProcessRequest::class, |
| 105 | + 'commentable_id' => $this->processRequest->id, |
| 106 | + 'subject' => __('Request Retried'), |
| 107 | + ]); |
| 108 | + |
| 109 | + if ($user = $this->initializingUser()) { |
| 110 | + $comment->setAttribute('user_id', $user->id); |
| 111 | + $comment->setAttribute('body', $user->fullname . ' ' . __('retried the request from an error state')); |
| 112 | + } else { |
| 113 | + $comment->setAttribute('body', __('Request was retried from an error state')); |
| 114 | + } |
| 115 | + |
| 116 | + $comment->save(); |
| 117 | + } |
| 118 | + |
| 119 | + private function determineTaskTypes(): void |
| 120 | + { |
| 121 | + if (app()->runningInConsole()) { |
| 122 | + static::$taskTypes = ['scriptTask', 'serviceTask', 'task']; |
| 123 | + } else { |
| 124 | + static::$taskTypes = ['scriptTask']; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + protected function getRetriableTasks(): Collection |
| 129 | + { |
| 130 | + return $this->retriableTasksQuery()->get(); |
| 131 | + } |
| 132 | + |
| 133 | + protected function retriableTasksQuery(): HasMany |
| 134 | + { |
| 135 | + $tokensQuery = $this->processRequest->tokens(); |
| 136 | + |
| 137 | + $tokensQuery->whereIn('status', ['FAILING', 'ACTIVE', 'ERROR']); |
| 138 | + |
| 139 | + $tokensQuery->whereIn('element_type', static::$taskTypes); |
| 140 | + |
| 141 | + return $tokensQuery; |
| 142 | + } |
| 143 | + |
| 144 | + protected function unlockProcessRequest(): void |
| 145 | + { |
| 146 | + ProcessRequestLock::where('process_request_id', $this->processRequest->id) |
| 147 | + ->delete(); |
| 148 | + } |
| 149 | + |
| 150 | + protected function getBpmnDefinitions(): BpmnDocument |
| 151 | + { |
| 152 | + return $this->processRequest->process->getDefinitions(); |
| 153 | + } |
| 154 | +} |
0 commit comments