Skip to content

Commit 5f65a15

Browse files
caleelijulceslauhub
authored andcommitted
Run scripts using nayra service
1 parent 0513c6c commit 5f65a15

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

‎ProcessMaker/Console/Commands/BuildScriptExecutors.php‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use Exception;
66
use Illuminate\Console\Command;
7+
use Illuminate\Support\Facades\Cache;
78
use ProcessMaker\Events\BuildScriptExecutor;
89
use ProcessMaker\Exception\InvalidDockerImageException;
910
use ProcessMaker\Facades\Docker;
1011
use ProcessMaker\Models\ScriptExecutor;
12+
use UnexpectedValueException;
1113

1214
class BuildScriptExecutors extends Command
1315
{
@@ -157,6 +159,36 @@ public function buildExecutor()
157159
$command = Docker::command() .
158160
" build --build-arg SDK_DIR=./sdk -t {$image} -f {$packagePath}/Dockerfile.custom {$packagePath}";
159161

162+
$this->execCommand($command);
163+
164+
$isNayra = $scriptExecutor->language === 'nayra';
165+
if ($isNayra) {
166+
$instanceName = config('app.instance');
167+
$this->info('Stop existing nayra container');
168+
$this->execCommand(Docker::command() . " stop {$instanceName}_nayra 2>&1 || true");
169+
$this->execCommand(Docker::command() . " rm {$instanceName}_nayra 2>&1 || true");
170+
$this->info('Bring up the nayra container');
171+
$this->execCommand(Docker::command() . ' run -d --name ' . $instanceName . '_nayra ' . $image);
172+
$this->info('Get IP address of the nayra container');
173+
$ip = '';
174+
for ($i = 0; $i < 10; $i++) {
175+
$ip = exec(Docker::command() . " inspect --format '{{ .NetworkSettings.IPAddress }}' {$instanceName}_nayra");
176+
if ($ip) {
177+
$this->info('Nayra container IP: ' . $ip);
178+
Cache::forever('nayra_ips', [$ip]);
179+
$this->sendEvent(0, 'done');
180+
break;
181+
}
182+
sleep(1);
183+
}
184+
if (!$ip) {
185+
throw new UnexpectedValueException('Could not get IP address of the nayra container');
186+
}
187+
}
188+
}
189+
190+
private function execCommand(string $command)
191+
{
160192
if ($this->userId) {
161193
$this->runProc(
162194
$command,

‎ProcessMaker/ScriptRunners/Base.php‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace ProcessMaker\ScriptRunners;
44

5+
use Carbon\Carbon;
6+
use Illuminate\Support\Facades\Cache;
57
use Illuminate\Support\Facades\Log;
68
use ProcessMaker\GenerateAccessToken;
79
use ProcessMaker\Models\EnvironmentVariable;
810
use ProcessMaker\Models\ScriptDockerBindingFilesTrait;
911
use ProcessMaker\Models\ScriptDockerCopyingFilesTrait;
12+
use ProcessMaker\Models\ScriptDockerNayraTrait;
1013
use ProcessMaker\Models\ScriptExecutor;
1114
use ProcessMaker\Models\User;
1215
use RuntimeException;
@@ -15,6 +18,9 @@ abstract class Base
1518
{
1619
use ScriptDockerCopyingFilesTrait;
1720
use ScriptDockerBindingFilesTrait;
21+
use ScriptDockerNayraTrait;
22+
23+
const NAYRA_LANG = 'php-nayra';
1824

1925
private $tokenId = '';
2026

@@ -38,7 +44,7 @@ abstract public function config($code, array $dockerConfig);
3844
/**
3945
* Set the script executor
4046
*
41-
* @var \ProcessMaker\Models\User
47+
* @var \ProcessMaker\Models\ScriptExecutor
4248
*/
4349
private $scriptExecutor;
4450

@@ -70,13 +76,25 @@ public function run($code, array $data, array $config, $timeout, ?User $user)
7076
// Create tokens for the SDK if a user is set
7177
$token = null;
7278
if ($user) {
73-
$token = new GenerateAccessToken($user);
74-
$environmentVariables[] = 'API_TOKEN=' . $token->getToken();
79+
$expires = Carbon::now()->addWeek();
80+
$accessToken = Cache::remember('script-runner-' . $user->id, $expires, function () use ($user) {
81+
$user->removeOldRunScriptTokens();
82+
$token = new GenerateAccessToken($user);
83+
return $token->getToken();
84+
});
85+
$environmentVariables[] = 'API_TOKEN=' . $accessToken;
7586
$environmentVariables[] = 'API_HOST=' . config('app.docker_host_url') . '/api/1.0';
7687
$environmentVariables[] = 'APP_URL=' . config('app.docker_host_url');
7788
$environmentVariables[] = 'API_SSL_VERIFY=' . (config('app.api_ssl_verify') ? '1' : '0');
7889
}
7990

91+
// Nayra Executor
92+
$isNayra = $this->scriptExecutor->language === self::NAYRA_LANG;
93+
if ($isNayra) {
94+
$response = $this->handleNayraDocker($code, $data, $config, $timeout, $environmentVariables);
95+
return json_decode($response, true);
96+
}
97+
8098
if ($environmentVariables) {
8199
$parameters = '-e ' . implode(' -e ', $environmentVariables);
82100
} else {

0 commit comments

Comments
 (0)