Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Start refactoring for async script microservice
  • Loading branch information
nolanpro committed Jan 25, 2025
commit d27abb22726d09e8efa317ca509143fbdc624d14
93 changes: 65 additions & 28 deletions ProcessMaker/Jobs/ErrorHandling.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ProcessMaker\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
Expand All @@ -12,7 +13,10 @@
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\Script;
use ProcessMaker\Models\ScriptVersion;
use ProcessMaker\Nayra\Contracts\Bpmn\ActivityInterface;
use ProcessMaker\Nayra\Contracts\Bpmn\ScriptTaskInterface;
use ProcessMaker\Notifications\ErrorExecutionNotification;
use Throwable;

class ErrorHandling
{
Expand All @@ -35,29 +39,52 @@ class ErrorHandling
*/
public $defaultErrorHandling = [];

public $job;

public $exception;

public $metadata;

public function __construct(
public $element,
public $processRequestToken,
public ActivityInterface $element,
public ProcessRequestToken $processRequestToken,
) {
$this->bpmnErrorHandling = json_decode($element->getProperty('errorHandling'), true) ?? [];
}

public function handleRetries($job, $exception)
public function handleRetries(ShouldQueue $job, Throwable $exception)
{
$this->job = $job;
$this->exception = $exception;

return $this->handldRetryAttempts();
}

public function handleRetriesForScriptMicroservice(Throwable $exception, array $metadata)
{
$this->metadata = $metadata;
$this->exception = $exception;

return $this->handldRetryAttempts();
}

private function handldRetryAttempts()
{
$message = $exception->getMessage();
$finalAttempt = true;
$attemptNumber = $this->getAttemptNumber();
$message = $this->getMessage();

if ($this->retryAttempts() > 0) {
if ($job->attemptNum <= $this->retryAttempts()) {
Log::info('Retry the job process. Attempt ' . $job->attemptNum . ' of ' . $this->retryAttempts() . ', Wait time: ' . $this->retryWaitTime());
$this->requeue($job);
if ($attemptNumber <= $this->retryAttempts()) {
Log::info('Retry the job process. Attempt ' . $attemptNumber . ' of ' . $this->retryAttempts() . ', Wait time: ' . $this->retryWaitTime());
$this->requeue();

$finalAttempt = false;

return [$message, $finalAttempt];
}

$message = __('Job failed after :attempts total attempts', ['attempts' => $job->attemptNum]) . "\n" . $message;
$message = __('Job failed after :attempts total attempts', ['attempts' => $attemptNumber]) . "\n" . $message;

$this->sendExecutionErrorNotification($message);
} else {
Expand All @@ -67,26 +94,46 @@ public function handleRetries($job, $exception)
return [$message, $finalAttempt];
}

private function requeue($job)
private function requeue()
{
$class = get_class($job);
if ($job instanceof RunNayraServiceTask) {
if ($this->job instanceof RunNayraServiceTask) {
$newJob = new RunNayraServiceTask($this->processRequestToken);
$newJob->attemptNum = $job->attemptNum + 1;
$newJob->attemptNum = $this->attemptNumber + 1;
} else {
$newJob = new $class(
Process::findOrFail($job->definitionsId),
ProcessRequest::findOrFail($job->instanceId),
ProcessRequestToken::findOrFail($job->tokenId),
$job->data,
$job->attemptNum + 1
$jobClass = $this->getJobClass();
$newJob = new $jobClass(
$this->processRequestToken->process,
$this->processRequestToken->processRequest,
$this->processRequestToken,
$this->getData(),
$this->getAttemptNumber() + 1
);
}
$newJob->delay($this->retryWaitTime());
$newJob->onQueue('bpmn');
dispatch($newJob);
}

private function getData()
{
return $this->job ? $this->job->data : $this->metadata['data'];
}

private function getAttemptNumber()
{
return $this->job ? $this->job->attemptNum : $this->metadata['attemptNum'];
}

private function getMessage()
{
$this->exception->getMessage();
}

private function getJobClass()
{
return $this->job ? get_class($this->job) : $this->metadata['job_class'];
}

/**
* Send execution error notification.
*/
Expand Down Expand Up @@ -206,14 +253,4 @@ public function setDefaultsFromDataSourceConfig(array $config)
'retry_wait_time' => Arr::get($endpointConfig, 'retry_wait_time', 5),
];
}

public static function convertResponseToException($result)
{
if ($result['status'] === 'error') {
if (str_starts_with($result['message'], 'Command exceeded timeout of')) {
throw new ScriptTimeoutException($result['message']);
}
throw new ScriptException($result['message']);
}
}
}
1 change: 1 addition & 0 deletions ProcessMaker/Jobs/RunScriptTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function action(ProcessRequestToken $token = null, ScriptTaskInterface $e
'token_id' => $this->tokenId,
'data' => $data,
'attempts' => $this->attemptNum,
'job_class' => get_class($this),
],
];
$response = $script->runScript($data, $configuration, $token->getId(), $errorHandling->timeout(), 1, $metadata);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nolanpro here we are forcing this to be sync. This is not the idea, we should control this in the callback response.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@epdl in that case, should we remove ProcessMaker/Jobs/RunScriptTask.php and make this entirely synchronous from core's point of view? Do you see any point in using horizon/queues in core for scripts? Queueing one job that queues another job would make the process error prone

Expand Down
22 changes: 22 additions & 0 deletions ProcessMaker/Services/ScriptMicroserviceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use ProcessMaker\Events\ScriptResponseEvent;
use ProcessMaker\Exception\ScriptException;
use ProcessMaker\Exception\ScriptTimeoutException;
use ProcessMaker\Jobs\CompleteActivity;
use ProcessMaker\Jobs\ErrorHandling;
use ProcessMaker\Jobs\RunScriptTask;
use ProcessMaker\Models\Process as Definitions;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestToken;
Expand Down Expand Up @@ -41,5 +45,23 @@ public function handle(Request $request)
CompleteActivity::dispatch($definitions, $instance, $token, $response['output'])->onQueue('bpmn');
}
}

try {
$this->convertResponseToException($response);
} catch (ScriptException|ScriptTimeoutException $e) {
$element = $definitions->getElementInstanceById($token->element_id);
$errorHandling = new ErrorHandling($element, $token);
$errorHandling->handleRetriesForScriptMicroservice($e, $response['metadata']);
}
}

public function convertResponseToException($response)
{
if ($response['status'] === 'error') {
if (str_starts_with($response['message'], 'Command exceeded timeout of')) {
throw new ScriptTimeoutException($response['message']);
}
throw new ScriptException($response['message']);
}
}
}