Skip to content

Commit fd2a8c4

Browse files
authored
Merge pull request #61 from open-runtimes/fix-active-runtime-id
Clarify active runtime ID usage in the executor
2 parents 27ed18a + cbfb68d commit fd2a8c4

File tree

2 files changed

+49
-52
lines changed

2 files changed

+49
-52
lines changed

app/http.php

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
275275
try {
276276
$orchestration->remove($container->getId(), true);
277277

278-
$activeRuntimeId = $container->getLabels()['openruntimes-runtime-id'];
278+
$activeRuntimeId = $container->getName();
279279

280280
if (!$activeRuntimes->exists($activeRuntimeId)) {
281281
$activeRuntimes->del($activeRuntimeId);
@@ -303,21 +303,21 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
303303
->action(function (string $runtimeId, string $timeoutStr, Response $response, Log $log) {
304304
$timeout = \intval($timeoutStr);
305305

306-
$runtimeId = System::getHostname() . '-' . $runtimeId; // Used in Docker (name)
306+
$runtimeName = System::getHostname() . '-' . $runtimeId;
307307

308308
$response->sendHeader('Content-Type', 'text/event-stream');
309309
$response->sendHeader('Cache-Control', 'no-cache');
310310

311311
// Wait for runtime
312312
for ($i = 0; $i < 10; $i++) {
313313
$output = '';
314-
$code = Console::execute('docker container inspect ' . \escapeshellarg($runtimeId), '', $output);
314+
$code = Console::execute('docker container inspect ' . \escapeshellarg($runtimeName), '', $output);
315315
if ($code === 0) {
316316
break;
317317
}
318318

319319
if ($i === 9) {
320-
$runtimeIdTokens = explode("-", $runtimeId);
320+
$runtimeIdTokens = explode("-", $runtimeName);
321321
$executorId = $runtimeIdTokens[0];
322322
$functionId = $runtimeIdTokens[1];
323323
$deploymentId = $runtimeIdTokens[2];
@@ -357,7 +357,7 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
357357
});
358358

359359
$output = '';
360-
Console::execute('docker exec ' . \escapeshellarg($runtimeId) . ' tail -F /var/tmp/logs.txt', '', $output, $timeout, function (string $outputChunk, mixed $process) use (&$logsChunk, &$logsProcess) {
360+
Console::execute('docker exec ' . \escapeshellarg($runtimeName) . ' tail -F /var/tmp/logs.txt', '', $output, $timeout, function (string $outputChunk, mixed $process) use (&$logsChunk, &$logsProcess) {
361361
$logsProcess = $process;
362362

363363
if (!empty($outputChunk)) {
@@ -390,15 +390,14 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
390390
->inject('response')
391391
->inject('log')
392392
->action(function (string $runtimeId, string $image, string $entrypoint, string $source, string $destination, array $variables, string $runtimeEntrypoint, string $command, int $timeout, bool $remove, int $cpus, int $memory, string $version, Orchestration $orchestration, Table $activeRuntimes, Response $response, Log $log) {
393-
$activeRuntimeId = $runtimeId; // Used with Swoole table (key)
394-
$runtimeId = System::getHostname() . '-' . $runtimeId; // Used in Docker (name)
393+
$runtimeName = System::getHostname() . '-' . $runtimeId;
395394

396395
$runtimeHostname = \uniqid();
397396

398-
$log->addTag('runtimeId', $activeRuntimeId);
397+
$log->addTag('runtimeId', $runtimeName);
399398

400-
if ($activeRuntimes->exists($activeRuntimeId)) {
401-
if ($activeRuntimes->get($activeRuntimeId)['status'] == 'pending') {
399+
if ($activeRuntimes->exists($runtimeName)) {
400+
if ($activeRuntimes->get($runtimeName)['status'] == 'pending') {
402401
throw new \Exception('A runtime with the same ID is already being created. Attempt a execution soon.', 500);
403402
}
404403

@@ -411,8 +410,8 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
411410

412411
$secret = \bin2hex(\random_bytes(16));
413412

414-
$activeRuntimes->set($activeRuntimeId, [
415-
'name' => $activeRuntimeId,
413+
$activeRuntimes->set($runtimeName, [
414+
'name' => $runtimeName,
416415
'hostname' => $runtimeHostname,
417416
'created' => $startTime,
418417
'updated' => $startTime,
@@ -423,7 +422,7 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
423422
/**
424423
* Temporary file paths in the executor
425424
*/
426-
$tmpFolder = "tmp/$runtimeId/";
425+
$tmpFolder = "tmp/$runtimeName/";
427426
$tmpSource = "/{$tmpFolder}src/code.tar.gz";
428427
$tmpBuild = "/{$tmpFolder}builds/code.tar.gz";
429428

@@ -486,13 +485,13 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
486485
/** Keep the container alive if we have commands to be executed */
487486
$containerId = $orchestration->run(
488487
image: $image,
489-
name: $runtimeId,
488+
name: $runtimeName,
490489
hostname: $runtimeHostname,
491490
vars: $variables,
492491
command: $runtimeEntrypointCommands,
493492
labels: [
494493
'openruntimes-executor' => System::getHostname(),
495-
'openruntimes-runtime-id' => $activeRuntimeId
494+
'openruntimes-runtime-id' => $runtimeId
496495
],
497496
volumes: [
498497
\dirname($tmpSource) . ':/tmp:rw',
@@ -516,7 +515,7 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
516515
];
517516

518517
$status = $orchestration->execute(
519-
name: $runtimeId,
518+
name: $runtimeName,
520519
command: $commands,
521520
output: $output,
522521
timeout: $timeout
@@ -563,8 +562,8 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
563562
'duration' => $duration,
564563
]);
565564

566-
$activeRuntimes->set($activeRuntimeId, [
567-
'name' => $activeRuntimeId,
565+
$activeRuntimes->set($runtimeName, [
566+
'name' => $runtimeName,
568567
'hostname' => $runtimeHostname,
569568
'created' => $startTime,
570569
'updated' => \microtime(true),
@@ -578,7 +577,7 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
578577
try {
579578
$logs = '';
580579
$status = $orchestration->execute(
581-
name: $runtimeId,
580+
name: $runtimeName,
582581
command: [ 'sh', '-c', 'cat /var/tmp/logs.txt' ],
583582
output: $logs,
584583
timeout: 15
@@ -599,11 +598,11 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
599598

600599
// Silently try to kill container
601600
try {
602-
$orchestration->remove($activeRuntimeId, true);
601+
$orchestration->remove($runtimeName, true);
603602
} catch (Throwable $th) {
604603
}
605604

606-
$activeRuntimes->del($activeRuntimeId);
605+
$activeRuntimes->del($runtimeName);
607606

608607
throw new Exception($error, 500);
609608
}
@@ -616,11 +615,11 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
616615

617616
// Silently try to kill container
618617
try {
619-
$orchestration->remove($activeRuntimeId, true);
618+
$orchestration->remove($runtimeName, true);
620619
} catch (Throwable $th) {
621620
}
622621

623-
$activeRuntimes->del($activeRuntimeId);
622+
$activeRuntimes->del($runtimeName);
624623
}
625624

626625
$response
@@ -651,15 +650,15 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
651650
->inject('response')
652651
->inject('log')
653652
->action(function (string $runtimeId, Table $activeRuntimes, Response $response, Log $log) {
654-
$activeRuntimeId = $runtimeId; // Used with Swoole table (key)
653+
$runtimeName = System::getHostname() . '-' . $runtimeId;
655654

656-
$log->addTag('runtimeId', $activeRuntimeId);
655+
$log->addTag('runtimeId', $runtimeName);
657656

658-
if (!$activeRuntimes->exists($activeRuntimeId)) {
657+
if (!$activeRuntimes->exists($runtimeName)) {
659658
throw new Exception('Runtime not found', 404);
660659
}
661660

662-
$runtime = $activeRuntimes->get($activeRuntimeId);
661+
$runtime = $activeRuntimes->get($runtimeName);
663662

664663
$response
665664
->setStatusCode(Response::STATUS_CODE_OK)
@@ -674,17 +673,16 @@ function removeAllRuntimes(Table $activeRuntimes, Orchestration $orchestration):
674673
->inject('response')
675674
->inject('log')
676675
->action(function (string $runtimeId, Orchestration $orchestration, Table $activeRuntimes, Response $response, Log $log) {
677-
$activeRuntimeId = $runtimeId; // Used with Swoole table (key)
678-
$runtimeId = System::getHostname() . '-' . $runtimeId; // Used in Docker (name)
676+
$runtimeName = System::getHostname() . '-' . $runtimeId;
679677

680-
$log->addTag('runtimeId', $activeRuntimeId);
678+
$log->addTag('runtimeId', $runtimeName);
681679

682-
if (!$activeRuntimes->exists($activeRuntimeId)) {
680+
if (!$activeRuntimes->exists($runtimeName)) {
683681
throw new Exception('Runtime not found', 404);
684682
}
685683

686-
$orchestration->remove($runtimeId, true);
687-
$activeRuntimes->del($activeRuntimeId);
684+
$orchestration->remove($runtimeName, true);
685+
$activeRuntimes->del($runtimeName);
688686

689687
$response
690688
->setStatusCode(Response::STATUS_CODE_OK)
@@ -718,10 +716,9 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr
718716
$payload = '';
719717
}
720718

721-
$activeRuntimeId = $runtimeId; // Used with Swoole table (key)
722-
$runtimeId = System::getHostname() . '-' . $runtimeId; // Used in Docker (name)
719+
$runtimeName = System::getHostname() . '-' . $runtimeId;
723720

724-
$log->addTag('runtimeId', $activeRuntimeId);
721+
$log->addTag('runtimeId', $runtimeName);
725722

726723
$variables = \array_merge($variables, [
727724
'INERNAL_EXECUTOR_HOSTNAME' => System::getHostname()
@@ -730,21 +727,21 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr
730727
$coldStartDuration = 0;
731728

732729
// Prepare runtime
733-
if (!$activeRuntimes->exists($activeRuntimeId)) {
730+
if (!$activeRuntimes->exists($runtimeName)) {
734731
if (empty($image) || empty($source) || empty($entrypoint)) {
735732
throw new Exception('Runtime not found. Please start it first or provide runtime-related parameters.', 401);
736733
}
737734

738735
// Prepare request to executor
739-
$sendCreateRuntimeRequest = function () use ($activeRuntimeId, $image, $source, $entrypoint, $variables, $cpus, $memory, $version, $runtimeEntrypoint) {
736+
$sendCreateRuntimeRequest = function () use ($runtimeId, $image, $source, $entrypoint, $variables, $cpus, $memory, $version, $runtimeEntrypoint) {
740737
$statusCode = 0;
741738
$errNo = -1;
742739
$executorResponse = '';
743740

744741
$ch = \curl_init();
745742

746743
$body = \json_encode([
747-
'runtimeId' => $activeRuntimeId,
744+
'runtimeId' => $runtimeId,
748745
'image' => $image,
749746
'source' => $source,
750747
'entrypoint' => $entrypoint,
@@ -815,13 +812,13 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr
815812
}
816813

817814
// Update swoole table
818-
$runtime = $activeRuntimes->get($activeRuntimeId) ?? [];
815+
$runtime = $activeRuntimes->get($runtimeName) ?? [];
819816
$runtime['updated'] = \time();
820-
$activeRuntimes->set($activeRuntimeId, $runtime);
817+
$activeRuntimes->set($runtimeName, $runtime);
821818

822819
// Ensure runtime started
823820
for ($i = 0; $i < 10; $i++) {
824-
if ($activeRuntimes->get($activeRuntimeId)['status'] !== 'pending') {
821+
if ($activeRuntimes->get($runtimeName)['status'] !== 'pending') {
825822
break;
826823
}
827824

@@ -833,7 +830,7 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr
833830
}
834831

835832
// Ensure we have secret
836-
$runtime = $activeRuntimes->get($activeRuntimeId);
833+
$runtime = $activeRuntimes->get($runtimeName);
837834
$hostname = $runtime['hostname'];
838835
$secret = $runtime['key'];
839836
if (empty($secret)) {
@@ -1061,9 +1058,9 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr
10611058
}
10621059

10631060
// Update swoole table
1064-
$runtime = $activeRuntimes->get($activeRuntimeId);
1061+
$runtime = $activeRuntimes->get($runtimeName);
10651062
$runtime['updated'] = \microtime(true);
1066-
$activeRuntimes->set($activeRuntimeId, $runtime);
1063+
$activeRuntimes->set($runtimeName, $runtime);
10671064

10681065
// Finish request
10691066
$response
@@ -1208,17 +1205,17 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr
12081205
Timer::tick($interval * 1000, function () use ($orchestration, $activeRuntimes) {
12091206
Console::info("Running maintenance task ...");
12101207
// Stop idling runtimes
1211-
foreach ($activeRuntimes as $activeRuntimeId => $runtime) {
1208+
foreach ($activeRuntimes as $runtimeName => $runtime) {
12121209
$inactiveThreshold = \time() - \intval(Http::getEnv('OPR_EXECUTOR_INACTIVE_TRESHOLD', '60'));
12131210
if ($runtime['updated'] < $inactiveThreshold) {
1214-
go(function () use ($activeRuntimeId, $runtime, $orchestration, $activeRuntimes) {
1211+
go(function () use ($runtimeName, $runtime, $orchestration, $activeRuntimes) {
12151212
try {
12161213
$orchestration->remove($runtime['name'], true);
12171214
Console::success("Successfully removed {$runtime['name']}");
12181215
} catch (\Throwable $th) {
12191216
Console::error('Inactive Runtime deletion failed: ' . $th->getMessage());
12201217
} finally {
1221-
$activeRuntimes->del($activeRuntimeId);
1218+
$activeRuntimes->del($runtimeName);
12221219
}
12231220
});
12241221
}
@@ -1232,8 +1229,8 @@ function (string $runtimeId, ?string $payload, string $path, string $method, arr
12321229
if (\str_starts_with($entry, $prefix)) {
12331230
$isActive = false;
12341231

1235-
foreach ($activeRuntimes as $activeRuntimeId => $runtime) {
1236-
if (\str_ends_with($entry, $activeRuntimeId)) {
1232+
foreach ($activeRuntimes as $runtimeName => $runtime) {
1233+
if (\str_ends_with($entry, $runtimeName)) {
12371234
$isActive = true;
12381235
break;
12391236
}

tests/ExecutorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ public function testBuild(): array
152152
$response = $this->client->call(Client::METHOD_GET, '/runtimes', [], []);
153153
$this->assertEquals(200, $response['headers']['status-code']);
154154
$this->assertEquals(1, count($response['body']));
155-
$this->assertEquals('test-build', $response['body'][0]['name']);
155+
$this->assertStringEndsWith('test-build', $response['body'][0]['name']);
156156

157157
/** Get runtime */
158158
$response = $this->client->call(Client::METHOD_GET, '/runtimes/test-build', [], []);
159159
$this->assertEquals(200, $response['headers']['status-code']);
160-
$this->assertEquals('test-build', $response['body']['name']);
160+
$this->assertStringEndsWith('test-build', $response['body']['name']);
161161

162162
/** Delete runtime */
163163
$response = $this->client->call(Client::METHOD_DELETE, '/runtimes/test-build', [], []);

0 commit comments

Comments
 (0)