Skip to content

Commit e5b358e

Browse files
committed
Create the tenant if it does not exist
1 parent e3c6911 commit e5b358e

File tree

2 files changed

+405
-29
lines changed

2 files changed

+405
-29
lines changed

ProcessMaker/Services/ScriptMicroserviceService.php

Lines changed: 94 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ProcessMaker\Services;
44

5+
use Illuminate\Http\Client\RequestException;
56
use Illuminate\Http\Request;
67
use Illuminate\Support\Facades\App;
78
use Illuminate\Support\Facades\Cache;
@@ -21,18 +22,61 @@ class ScriptMicroserviceService
2122
{
2223
private $client;
2324

24-
public function __construct()
25+
private $tenantChecked = false;
26+
27+
private function client($includeToken = true)
2528
{
26-
$this->client = Http::withOptions([
27-
'verify' => !App::environment('local'),
28-
]);
29+
if (!$this->client) {
30+
$this->client = Http::withOptions([
31+
'verify' => !App::environment('local'),
32+
])->baseUrl(config('script-runner-microservice.base_url'))
33+
->accept('application/json')
34+
->contentType('application/json')
35+
->throw();
36+
}
37+
38+
if ($includeToken) {
39+
$this->client->withToken($this->getAccessToken());
40+
}
41+
42+
return $this->client;
43+
}
44+
45+
private function checkTenant()
46+
{
47+
if ($this->tenantChecked) {
48+
return;
49+
}
50+
51+
$instanceUuid = $this->getInstanceUuid();
52+
$url = '/tenants/' . $instanceUuid;
53+
$client = $this->client();
54+
55+
try {
56+
$response = $client->get($url);
57+
// Tenant exists, we're good
58+
} catch (RequestException $e) {
59+
// If we get a 404, create the tenant
60+
if ($e->response && $e->response->status() === 404) {
61+
Log::debug('Tenant not found, creating.', ['instanceUuid' => $instanceUuid]);
62+
$client->post('/tenants', [
63+
'name' => $instanceUuid,
64+
'id' => $instanceUuid,
65+
]);
66+
} else {
67+
// Re-throw if it's not a 404
68+
Log::error('Error checking tenant', ['error' => $e->getMessage()]);
69+
throw $e;
70+
}
71+
}
72+
73+
$this->tenantChecked = true;
2974
}
3075

3176
public function createCustomExecutor(ScriptExecutor $scriptExecutor)
3277
{
33-
Log::info('Creating custom script executor...');
34-
$url = config('script-runner-microservice.base_url') . '/custom/' . $this->getInstanceUuid() . '/scripts';
35-
Log::debug('Uri: ' . $url);
78+
$url = '/custom/' . $this->getInstanceUuid() . '/scripts';
79+
Log::debug('Creating custom script executor.', ['url' => $url]);
3680
$payload = [
3781
'id' => $scriptExecutor->uuid,
3882
'name' => $scriptExecutor->title,
@@ -43,17 +87,20 @@ public function createCustomExecutor(ScriptExecutor $scriptExecutor)
4387
];
4488
Log::debug('Payload: ', $payload);
4589

46-
$response = $this->client->withToken($this->getAccessToken())
47-
->post($url, $payload);
90+
$this->checkTenant();
91+
$response = $this->client()->post($url, $payload);
4892

49-
return $response->json();
93+
$jsonResponse = $response->json();
94+
Log::debug('Response', ['response' => $jsonResponse]);
95+
96+
return $jsonResponse;
5097
}
5198

5299
public function updateCustomExecutor(ScriptExecutor $scriptExecutor)
53100
{
54-
Log::info('Updating custom script executor...');
55-
$url = config('script-runner-microservice.base_url') . '/custom/scripts/' . $scriptExecutor->uuid;
56-
Log::debug('Uri: ' . $url);
101+
$this->checkTenant();
102+
$url = '/custom/scripts/' . $scriptExecutor->uuid;
103+
Log::debug('Updating custom script executor.', ['url' => $url]);
57104
$payload = [
58105
'name' => $scriptExecutor->title,
59106
'description' => $scriptExecutor->description,
@@ -63,22 +110,39 @@ public function updateCustomExecutor(ScriptExecutor $scriptExecutor)
63110
];
64111
Log::debug('Payload: ', $payload);
65112

66-
$response = $this->client->withToken($this->getAccessToken())
67-
->put($url, $payload);
113+
try {
114+
$response = $this->client()
115+
->put($url, $payload);
116+
117+
$jsonResponse = $response->json();
118+
Log::debug('Response', ['response' => $jsonResponse]);
68119

69-
return $response->json();
120+
return $jsonResponse;
121+
} catch (RequestException $e) {
122+
// If we get a 404, create the executor instead
123+
if ($e->response && $e->response->status() === 404) {
124+
Log::debug('Executor not found (404), creating instead...');
125+
126+
return $this->createCustomExecutor($scriptExecutor);
127+
} else {
128+
// Re-throw if it's not a 404
129+
throw $e;
130+
}
131+
}
70132
}
71133

72134
public function deleteCustomExecutor($scriptExecutorUUID)
73135
{
74-
Log::info('Deleting custom script executor...');
75-
$url = config('script-runner-microservice.base_url') . '/custom/scripts/' . $scriptExecutorUUID;
76-
Log::debug('Uri: ' . $url);
136+
$url = '/custom/scripts/' . $scriptExecutorUUID;
137+
Log::debug('Deleting custom script executor.', ['url' => $url]);
77138

78-
$response = $this->client->withToken($this->getAccessToken())
139+
$response = $this->client()
79140
->delete($url);
80141

81-
return $response->json();
142+
$jsonResponse = $response->json();
143+
Log::debug('Response', ['response' => $jsonResponse]);
144+
145+
return $jsonResponse;
82146
}
83147

84148
public function getAccessToken()
@@ -87,7 +151,7 @@ public function getAccessToken()
87151
return Cache::get('keycloak.access_token');
88152
}
89153

90-
$response = $this->client->asForm()->post(config('script-runner-microservice.keycloak.base_url') ?? '', [
154+
$response = $this->client(false)->asForm()->post(config('script-runner-microservice.keycloak.base_url') ?? '', [
91155
'grant_type' => 'password',
92156
'client_id' => config('script-runner-microservice.keycloak.client_id'),
93157
'client_secret' => config('script-runner-microservice.keycloak.client_secret'),
@@ -99,22 +163,24 @@ public function getAccessToken()
99163
Cache::put('keycloak.access_token', $response->json()['access_token'], $response->json()['expires_in'] - 60);
100164
}
101165

102-
return $response->json()['access_token'];
166+
$responseJson = $response->json();
167+
168+
return $responseJson['access_token'];
103169
}
104170

105171
public function getScriptRunner($language, $executorUuid, $custom = false)
106172
{
107173
$uri = !$custom ?
108-
config('script-runner-microservice.base_url') . '/scripts' :
109-
config('script-runner-microservice.base_url') . '/custom/' . $this->getInstanceUuid() . '/scripts';
174+
'/scripts' :
175+
'/custom/' . $this->getInstanceUuid() . '/scripts';
110176

111177
if (!$custom && Cache::has('script-runner-microservice.script-runner')) {
112178
return Cache::get('script-runner-microservice.script-runner.' . $language);
113179
} elseif ($custom && Cache::has('script-runner-microservice.custom-script-runner.' . $executorUuid)) {
114180
return Cache::get('script-runner-microservice.custom-script-runner.' . $executorUuid);
115181
}
116182

117-
$response = $this->client->withToken($this->getAccessToken())
183+
$response = $this->client()
118184
->get($uri)->collect();
119185

120186
$result = $response->filter(function ($item) use ($language, $executorUuid, $custom) {
@@ -134,14 +200,13 @@ public function getScriptRunner($language, $executorUuid, $custom = false)
134200

135201
public function sendScriptPayload($payload)
136202
{
137-
$uri = config('script-runner-microservice.base_url') . '/requests/create';
203+
$uri = '/requests/create';
138204
// Set a theoretical maximum timeout of 1 day (86400 seconds)
139205
// since the laravel client must have a timeout set.
140206
// The actual script timeout will be handled by the microservice.
141207
$clientTimeout = 86400;
142208

143-
return $this->client->timeout($clientTimeout)
144-
->withToken($this->getAccessToken())
209+
return $this->client()->timeout($clientTimeout)
145210
->post($uri, $payload);
146211
}
147212

0 commit comments

Comments
 (0)