-
Notifications
You must be signed in to change notification settings - Fork 227
FOUR-15875 Optimize Script Tasks Execution with Nayra Engine #6942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
88086b4
Merge pull request #6914 from ProcessMaker/FOUR-15871
caleeli 932374f
Add Nayra service as language
caleeli 4fe8577
Add support Nayra to run scripts
caleeli 2de84d5
Run scripts using nayra service
caleeli d9e7027
Add support for php-nayra to script editor
caleeli 6fa08cf
Create PHP-Nayra executor
caleeli c90dd09
Add missing down method
caleeli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
namespace ProcessMaker\Models; | ||
|
||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\Log; | ||
use ProcessMaker\Exception\ScriptException; | ||
use ProcessMaker\Facades\Docker; | ||
|
||
/** | ||
* Execute a docker container copying files to interchange information. | ||
*/ | ||
trait ScriptDockerNayraTrait | ||
{ | ||
|
||
/** | ||
* Execute the script task using Nayra Docker. | ||
* | ||
* @return string|bool | ||
*/ | ||
public function handleNayraDocker(string $code, array $data, array $config, $timeout, array $environmentVariables) | ||
{ | ||
$envVariables = []; | ||
foreach ($environmentVariables as $line) { | ||
list($key, $value) = explode('=', $line, 2); | ||
$envVariables[$key] = $value; | ||
} | ||
$params = [ | ||
'name' => uniqid('script_', true), | ||
'script' => $code, | ||
'data' => $data, | ||
'config' => $config, | ||
'envVariables' => $envVariables, | ||
'timeout' => $timeout, | ||
]; | ||
$body = json_encode($params); | ||
$servers = Cache::get('nayra_ips'); | ||
if (!$servers) { | ||
$url = config('app.nayra_rest_api_host') . '/run_script'; | ||
} else { | ||
$index = array_rand($servers); | ||
$url = 'http://' . $servers[$index] . ':8080/run_script'; | ||
$this->ensureNayraServerIsRunning('http://' . $servers[$index] . ':8080'); | ||
} | ||
$ch = curl_init($url); | ||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, [ | ||
'Content-Type: application/json', | ||
'Content-Length: ' . strlen($body), | ||
]); | ||
$result = curl_exec($ch); | ||
curl_close($ch); | ||
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
if ($httpStatus !== 200) { | ||
Log::error('Error executing script with Nayra Docker', [ | ||
'url' => $url, | ||
'httpStatus' => $httpStatus, | ||
'result' => $result, | ||
]); | ||
throw new ScriptException($result); | ||
} | ||
return $result; | ||
} | ||
|
||
/** | ||
* Ensure that the Nayra server is running. | ||
* | ||
* @param string $url URL of the Nayra server | ||
* @return void | ||
* @throws ScriptException If cannot connect to Nayra Service | ||
*/ | ||
private function ensureNayraServerIsRunning(string $url) | ||
{ | ||
$header = @get_headers($url); | ||
if (!$header) { | ||
$this->bringUpNayra($url); | ||
} | ||
} | ||
|
||
/** | ||
* Bring up Nayra and check the provided URL. | ||
* | ||
* @param string $url The URL to check | ||
* @return void | ||
*/ | ||
private function bringUpNayra(string $url) | ||
{ | ||
$docker = Docker::command(); | ||
$instanceName = config('app.instance'); | ||
$image = $this->scriptExecutor->dockerImageName(); | ||
exec($docker . " stop {$instanceName}_nayra 2>&1 || true"); | ||
exec($docker . " rm {$instanceName}_nayra 2>&1 || true"); | ||
exec($docker . ' run -d --name ' . $instanceName . '_nayra ' . $image . ' &', $output, $status); | ||
if ($status) { | ||
Log::error('Error starting Nayra Docker', [ | ||
'output' => $output, | ||
'status' => $status, | ||
]); | ||
throw new ScriptException('Error starting Nayra Docker'); | ||
} | ||
$this->waitContainerNetwork($docker, $instanceName); | ||
$this->nayraServiceIsRunning($url); | ||
} | ||
|
||
/** | ||
* Waits for the container network to be ready. | ||
* | ||
* @param Docker $docker The Docker instance. | ||
* @param string $instanceName The name of the container instance. | ||
* @return string The IP of the container. | ||
*/ | ||
private function waitContainerNetwork($docker, $instanceName): string | ||
{ | ||
$ip = ''; | ||
for ($i = 0; $i < 30; $i++) { | ||
$ip = exec($docker . " inspect --format '{{ .NetworkSettings.IPAddress }}' {$instanceName}_nayra"); | ||
if ($ip) { | ||
Cache::forever('nayra_ips', [$ip]); | ||
return $ip; | ||
} | ||
sleep(1); | ||
} | ||
throw new ScriptException('Could not get address of the nayra container'); | ||
} | ||
|
||
/** | ||
* Checks if the Nayra service is running. | ||
* | ||
* @param string $url The URL of the Nayra service. | ||
* @return bool Returns true if the Nayra service is running, false otherwise. | ||
*/ | ||
private function nayraServiceIsRunning($url): bool | ||
{ | ||
for ($i = 0; $i < 30; $i++) { | ||
$status = @get_headers($url); | ||
if ($status) { | ||
return true; | ||
} | ||
sleep(1); | ||
} | ||
throw new ScriptException('Could not connect to the nayra container'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
FROM processmaker4dev/nayra-engine:next | ||
# The tag :next is temporal until the official release of the engine |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
upgrades/2024_06_12_150527_create_nayra_script_executor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Log; | ||
use ProcessMaker\Models\Script; | ||
use ProcessMaker\Models\ScriptExecutor; | ||
use ProcessMaker\ScriptRunners\Base; | ||
use ProcessMaker\Upgrades\UpgradeMigration as Upgrade; | ||
|
||
class CreateNayraScriptExecutor extends Upgrade | ||
{ | ||
/** | ||
* Run the upgrade migration. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
$exists = ScriptExecutor::where('language', Base::NAYRA_LANG)->exists(); | ||
if (!$exists) { | ||
$scriptExecutor = new ScriptExecutor(); | ||
$scriptExecutor->language = Base::NAYRA_LANG; | ||
$scriptExecutor->title = 'Nayra (µService)'; | ||
$scriptExecutor->save(); | ||
} | ||
} | ||
|
||
/** | ||
* Reverse the upgrade migration. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
try { | ||
$existsScriptsUsingNayra = Script::where('language', Base::NAYRA_LANG)->exists(); | ||
if (!$existsScriptsUsingNayra) { | ||
ScriptExecutor::where('language', Base::NAYRA_LANG)->delete(); | ||
} else { | ||
Log::error('There are scripts using Nayra, so the Nayra script executor cannot be deleted.'); | ||
} | ||
} catch (Exception $e) { | ||
Log::error('Cannot delete Nayra script executor: ' . $e->getMessage()); | ||
} | ||
} | ||
} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing "down" method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing method was added