Repository navigation
Expand file tree
/
Copy pathScriptMicroserviceRunner.php
More file actions
164 lines (135 loc) · 5.76 KB
/
Copy pathScriptMicroserviceRunner.php
File metadata and controls
164 lines (135 loc) · 5.76 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
<?php
namespace ProcessMaker\ScriptRunners;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use ProcessMaker\Enums\ScriptExecutorType;
use ProcessMaker\Exception\ConfigurationException;
use ProcessMaker\GenerateAccessToken;
use ProcessMaker\Jobs\ErrorHandling;
use ProcessMaker\Models\EnvironmentVariable;
use ProcessMaker\Models\Script;
use ProcessMaker\Models\User;
use ProcessMaker\Services\ScriptMicroserviceService;
use ProcessMaker\Services\SmartExtractConfiguration;
use stdClass;
class ScriptMicroserviceRunner
{
private string $tokenId = '';
private string $language;
private ScriptMicroserviceService $service;
public function __construct(protected Script $script)
{
$this->language = strtolower($script->language ?? $script->scriptExecutor->language);
$this->service = new ScriptMicroserviceService();
}
public function run($code, array $data, array $config, $timeout, $user, $sync, $metadata)
{
Log::debug('Language: ' . $this->language);
Log::debug('Sync: ' . $sync);
Log::debug('Metadata: ' . print_r($metadata, true));
Log::debug('Script type: ' . $this->script->scriptExecutor->type?->value);
$scriptRunner = $this->service->getScriptRunner(
$this->language,
$this->script->scriptExecutor->uuid,
$this->script->scriptExecutor->type === ScriptExecutorType::Custom
);
if (!$scriptRunner) {
throw new ConfigurationException('No exists script executor for this language: ' . $this->language);
}
$metadata = array_merge($this->getMetadata($user), $metadata);
$environmentVariables = $this->getEnvironmentVariables($user);
$payload = [
'version' => config('script-runner-microservice.version') ?? $this->getProcessMakerVersion(),
'language' => $scriptRunner['language'],
'metadata' => $metadata,
'data' => !empty($data) ? $this->sanitizeCss($data) : new stdClass(),
'config' => !empty($config) ? $config : new stdClass(),
'script' => base64_encode(str_replace("'", ''', $code)),
'secrets' => $environmentVariables,
'callback' => config('script-runner-microservice.callback'),
'callback_secure' => true,
'callback_token' => $environmentVariables['API_TOKEN'],
'debug' => true,
'timeout' => $timeout,
'sync' => $sync,
];
Log::debug('Payload: ' . print_r($payload, true));
$response = $this->service->sendScriptPayload($payload);
$response->throw();
$result = $response->json();
if ($sync) {
ErrorHandling::convertResponseToException($result);
}
return $result;
}
private function getEnvironmentVariables(User $user)
{
$variablesParameter = [];
EnvironmentVariable::chunk(50, function (Collection $variables) use (&$variablesParameter) {
foreach ($variables as $variable) {
// Fix variables that have spaces
$name = str_replace(' ', '_', $variable->name);
if ($name === SmartExtractConfiguration::API_HOST) {
continue;
}
$variablesParameter[$name] = $variable->value;
}
});
$smartExtractApiHost = app(SmartExtractConfiguration::class)->apiHost();
if ($smartExtractApiHost !== null) {
$variablesParameter[SmartExtractConfiguration::API_HOST] = $smartExtractApiHost;
}
// Add the url to the host
$variablesParameter['HOST_URL'] = config('app.docker_host_url');
// Create tokens for the SDK if a user is set
$token = null;
if ($user) {
$accessToken = Cache::remember('script-runner-' . $user->id, now()->addWeek(), function () use ($user) {
$user->removeOldRunScriptTokens();
$token = new GenerateAccessToken($user);
return $token->getToken();
});
$variablesParameter['API_TOKEN'] = $accessToken;
$variablesParameter['API_HOST'] = config('app.docker_host_url') . '/api/1.0';
$variablesParameter['APP_URL'] = config('app.docker_host_url');
$variablesParameter['API_SSL_VERIFY'] = (config('app.api_ssl_verify') ? '1' : '0');
$variablesParameter['SMART_EXTRACT_REQUEST_TIMEOUT'] = config('smart-extract.request_timeout');
}
return $variablesParameter;
}
public function setTokenId($tokenId)
{
$this->tokenId = $tokenId;
}
public function getProcessMakerVersion()
{
return Cache::remember('script-runner-microservice.processmaker-version', now()->addDay(), function () {
$composer_json_path = json_decode(file_get_contents(base_path() . '/composer.json'));
return $composer_json_path->version;
});
}
public function getMetadata($user)
{
return [
'script_id' => $this->script->id,
'executor_uuid' => $this->script->scriptExecutor->uuid,
'executor_type' => $this->script->scriptExecutor->type?->value,
'executor_version' => config('script-runner-microservice.version') ?? $this->getProcessMakerVersion(),
'instance' => config('app.url'),
'instance_uuid' => $this->service->getInstanceUuid(),
'user_id' => $user->id,
'user_email' => $user->email,
];
}
public function sanitizeCss($data)
{
if ($this->language !== 'javascript-ssr') {
return $data;
}
if (array_key_exists('css', $data)) {
$data['css'] = false;
}
return $data;
}
}