Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,21 @@ CUSTOM_EXECUTORS=false
CACHE_SETTING_DRIVER=cache_settings
CACHE_SETTING_PREFIX=settings:
AI_ENABLE_RAG_COLLECTIONS=false

SCRIPT_MICROSERVICE_ENABLED=true
SCRIPT_MICROSERVICE_BASE_URL=https://localhost:8010
SCRIPT_MICROSERVICE_CALLBACK="${APP_URL}/api/1.0/scripts/microservice/execution"
SCRIPT_MICROSERVICE_VERSION=

SCRIPT_MICROSERVICE_PUSHER_APP_ID=
SCRIPT_MICROSERVICE_PUSHER_APP_KEY=
SCRIPT_MICROSERVICE_PUSHER_APP_SECRET=
SCRIPT_MICROSERVICE_PUSHER_SCHEME=
SCRIPT_MICROSERVICE_PUSHER_HOST=
SCRIPT_MICROSERVICE_PUSHER_PORT=

KEYCLOAK_CLIENT_ID=
KEYCLOAK_CLIENT_SECRET=
KEYCLOAK_BASE_URL=
KEYCLOAK_USERNAME=
KEYCLOAK_PASSWORD=
172 changes: 172 additions & 0 deletions ProcessMaker/Console/Commands/TransitionExecutors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

namespace ProcessMaker\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Client\RequestException;
use ProcessMaker\Enums\ScriptExecutorType;
use ProcessMaker\Models\ScriptExecutor;
use ProcessMaker\Services\ScriptMicroserviceService;
use Illuminate\Support\Facades\Log;

class TransitionExecutors extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'processmaker:transition-executors
{uuid : The script executor UUID, or "all"}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Transition non-default script executor(s) to the Script Microservice';

public function __construct(private ScriptMicroserviceService $scriptMicroserviceService)
{
parent::__construct();
}

/**
* Execute the console command.
*
* Always talks to the microservice, even when SCRIPT_MICROSERVICE_ENABLED is false.
*/
public function handle(): int
{
$executors = $this->resolveExecutors($this->argument('uuid'));

if ($executors === null) {
return 1;
}

if ($executors->isEmpty()) {
$this->warn('No script executors found to transition.');

return 0;
}

$isAll = $this->argument('uuid') === 'all';
$remaining = $executors->count();
$processed = 0;

foreach ($executors as $executor) {
$remaining--;
$this->info("Transitioning executor {$executor->uuid} ({$executor->language}) to the microservice...");

try {
$response = $this->scriptMicroserviceService->updateCustomExecutor($executor);
Log::debug('Response', ['response' => $response]);
$status = strtolower((string) ($response['status'] ?? ''));

if (in_array($status, ['error', 'failed', 'failure'], true) && !isset($response['executor_id'])) {
throw new \RuntimeException(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) ?: 'Transition failed');
}
} catch (RequestException $e) {
$this->error("Transition failed for executor {$executor->uuid}");
$this->line($e->response?->body() ?: $e->getMessage());
if ($isAll && $remaining > 0) {
$this->warn("Stopping: {$remaining} remaining executor(s) were not processed.");
}

return 1;
} catch (\Throwable $e) {
$this->error("Transition failed for executor {$executor->uuid}");
$this->line($e->getMessage());
if ($isAll && $remaining > 0) {
$this->warn("Stopping: {$remaining} remaining executor(s) were not processed.");
}

return 1;
}

$this->info("Executor {$executor->uuid} transitioned successfully.");
$processed++;
}

if ($isAll) {
$this->info("All script executors transitioned successfully. ({$processed} processed)");
}

return 0;
}

/**
* Resolve executors that should be transitioned.
*
* Includes:
* - type = custom
* - type null (or unset) that are NOT the default/first executor for their language
*
* Excludes:
* - default package executors (first row per language)
*
* @return Collection<int, ScriptExecutor>|null Null when the request is invalid.
*/
private function resolveExecutors(string $uuid): ?Collection
{
if ($uuid === 'all') {
return ScriptExecutor::query()
->orderBy('id')
->get()
->filter(fn (ScriptExecutor $executor) => $this->shouldTransition($executor))
->values();
}

if (!$this->isValidUuid($uuid)) {
$this->error('Invalid uuid. Provide a script executor UUID or "all".');

return null;
}

$executor = ScriptExecutor::where('uuid', $uuid)->first();

if (!$executor) {
$this->error("Script executor [{$uuid}] not found.");

return null;
}

if (!$this->shouldTransition($executor)) {
$this->error("Script executor [{$uuid}] is a default/system executor and cannot be transitioned.");

return null;
}

return new Collection([$executor]);
}

/**
* Whether this executor should be migrated to the microservice.
*
* Custom executors always qualify. Others qualify only when they are not
* the default (first installed) executor for their language.
*/
private function shouldTransition(ScriptExecutor $executor): bool
{
if ($executor->type === ScriptExecutorType::Custom) {
return true;
}

$initial = ScriptExecutor::query()
->where('language', $executor->language)
->orderBy('created_at')
->orderBy('id')
->first();

return !$initial || (int) $initial->id !== (int) $executor->id;
}

private function isValidUuid(string $uuid): bool
{
return (bool) preg_match(
'/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i',
$uuid
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use ProcessMaker\Http\Controllers\Controller;
use ProcessMaker\Services\ScriptMicroserviceService;

class ScriptExecutorController extends Controller
{
Expand All @@ -13,6 +14,9 @@ public function index(Request $request)
abort(404);
}

return view('admin.script-executors.index');
return view('admin.script-executors.index',
[
'script_microservice_enabled' => config('script-runner-microservice.enabled'),
]);
}
}
56 changes: 35 additions & 21 deletions ProcessMaker/Http/Controllers/Api/ScriptExecutorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use ProcessMaker\Enums\ScriptExecutorType;
use ProcessMaker\Events\ScriptExecutorCreated;
use ProcessMaker\Events\ScriptExecutorDeleted;
use ProcessMaker\Events\ScriptExecutorUpdated;
Expand All @@ -15,6 +16,7 @@
use ProcessMaker\Jobs\BuildScriptExecutor;
use ProcessMaker\Models\Script;
use ProcessMaker\Models\ScriptExecutor;
use ProcessMaker\Services\ScriptMicroserviceService;

class ScriptExecutorController extends Controller
{
Expand All @@ -26,7 +28,7 @@ class ScriptExecutorController extends Controller
* @return ResponseFactory|Response
*
*
* @OA\Get(
* @OA\Get(
* path="/script-executors",
* summary="Returns all script executors that the user has access to",
* operationId="getScriptExecutors",
Expand Down Expand Up @@ -79,7 +81,7 @@ public function index(Request $request)
* @return ResponseFactory|Response
*
*
* @OA\Post(
* @OA\Post(
* path="/script-executors",
* summary="Create a script executor",
* operationId="createScriptExecutor",
Expand Down Expand Up @@ -109,21 +111,23 @@ public function index(Request $request)
* ),
* )
*/
public function store(Request $request)
public function store(Request $request, ScriptMicroserviceService $service)
{
$request->request->add(['type' => 'custom']);
$this->checkAuth($request);
$request->validate(ScriptExecutor::rules());

$scriptExecutor = ScriptExecutor::create(
$request->only((new ScriptExecutor())->getFillable())
);

ScriptExecutorCreated::dispatch($scriptExecutor->getAttributes());

BuildScriptExecutor::dispatch($scriptExecutor->id, $request->user()->id);
if (!config('script-runner-microservice.enabled')) {
ScriptExecutorCreated::dispatch($scriptExecutor->getAttributes());
BuildScriptExecutor::dispatch($scriptExecutor->id, $request->user()->id);
} else {
$service->createCustomExecutor($scriptExecutor);
Comment thread
cursor[bot] marked this conversation as resolved.
}

return ['status'=>'started', 'id' => $scriptExecutor->id];
return ['status' => 'started', 'uuid' => $scriptExecutor->uuid, 'id' => $scriptExecutor->id];
}

/**
Expand All @@ -135,7 +139,7 @@ public function store(Request $request)
* @return ResponseFactory|Response
*
*
* @OA\Put(
* @OA\Put(
* path="/script-executors/{script_executor}",
* summary="Update script executor",
* operationId="updateScriptExecutor",
Expand Down Expand Up @@ -173,7 +177,7 @@ public function store(Request $request)
* )
* )
*/
public function update(Request $request, ScriptExecutor $scriptExecutor)
public function update(Request $request, ScriptExecutor $scriptExecutor, ScriptMicroserviceService $service)
{
$this->checkAuth($request);
$request->validate(ScriptExecutor::rules());
Expand All @@ -184,13 +188,16 @@ public function update(Request $request, ScriptExecutor $scriptExecutor)
$request->only($scriptExecutor->getFillable())
);

if (!empty($scriptExecutor->getChanges())) {
ScriptExecutorUpdated::dispatch($scriptExecutor->id, $original, $scriptExecutor->getChanges());
if (config('script-runner-microservice.enabled') && $scriptExecutor->type == ScriptExecutorType::Custom) {
$service->updateCustomExecutor($scriptExecutor);
} else {
if (!empty($scriptExecutor->getChanges())) {
ScriptExecutorUpdated::dispatch($scriptExecutor->id, $original, $scriptExecutor->getChanges());
}
BuildScriptExecutor::dispatch($scriptExecutor->id, $request->user()->id);
}

BuildScriptExecutor::dispatch($scriptExecutor->id, $request->user()->id);

return ['status'=>'started'];
return ['status' => 'started', 'uuid' => $scriptExecutor->uuid];
}

/**
Expand All @@ -202,7 +209,7 @@ public function update(Request $request, ScriptExecutor $scriptExecutor)
* @return ResponseFactory|Response
*
*
* @OA\Delete(
* @OA\Delete(
* path="/script-executors/{script_executor}",
* summary="Delete a script executor",
* operationId="deleteScriptExecutor",
Expand Down Expand Up @@ -233,7 +240,7 @@ public function update(Request $request, ScriptExecutor $scriptExecutor)
* ),
* )
*/
public function delete(Request $request, ScriptExecutor $scriptExecutor)
public function delete(Request $request, ScriptExecutor $scriptExecutor, ScriptMicroserviceService $service)
{
if ($scriptExecutor->scripts()->count() > 0) {
throw ValidationException::withMessages(['delete' => __('Can not delete executor when it is assigned to scripts.')]);
Expand All @@ -254,9 +261,15 @@ public function delete(Request $request, ScriptExecutor $scriptExecutor)
}
}

$scriptExecutorUUID = $scriptExecutor->uuid;

ScriptExecutor::destroy($scriptExecutor->id);

ScriptExecutorDeleted::dispatch($scriptExecutor->getAttributes());
if (!config('script-runner-microservice.enabled')) {
ScriptExecutorDeleted::dispatch($scriptExecutor->getAttributes());
} else {
$service->deleteCustomExecutor($scriptExecutorUUID);
}
Comment thread
gusys marked this conversation as resolved.

return ['status' => 'done'];
}
Expand All @@ -280,7 +293,7 @@ private function checkAuth($request)
* @return ResponseFactory|Response
*
*
* @OA\Post(
* @OA\Post(
* path="/script-executors/cancel",
* summary="Cancel a script executor",
* operationId="cancelScriptExecutor",
Expand Down Expand Up @@ -327,7 +340,7 @@ public function cancel(Request $request)
* @return ResponseFactory|Response
*
*
* @OA\Get(
* @OA\Get(
* path="/script-executors/available-languages",
* summary="Returns all available languages",
* operationId="getAvailableLanguages",
Expand Down Expand Up @@ -360,7 +373,8 @@ public function availableLanguages()
{
$languages = [];
foreach (Script::scriptFormats() as $key => $config) {
if (in_array($key, Script::deprecatedLanguages)) {
// ToDo remove $key === 'php-nayra' validation when php-nayra include in deprecatedLanguages
if (in_array($key, Script::deprecatedLanguages) || $key === 'php-nayra') {
continue;
}
if (!array_key_exists('system', $config) || (array_key_exists('system', $config) && !$config['system'])) {
Expand Down
3 changes: 1 addition & 2 deletions ProcessMaker/Jobs/TestScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public function handle()
$response = $this->script->runScript($this->data, $this->configuration, '', null, 0, $metadata);
\Log::debug('Response from runScript: ' . print_r($response, true));

if (!config('script-runner-microservice.enabled') ||
$this->script->scriptExecutor && $this->script->scriptExecutor->type === ScriptExecutorType::Custom) {
if (!config('script-runner-microservice.enabled')) {
$this->sendResponse(200, $response);
}
} catch (Throwable $exception) {
Expand Down
Loading
Loading