From 5ded3191086cba9e3dee2b5898a782f1dd6f3a45 Mon Sep 17 00:00:00 2001 From: Moros Smith Date: Sun, 12 May 2024 02:04:56 -0400 Subject: [PATCH 1/3] compiler healthCheck - see that it exists! --- pgetinker/Compiler.php | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/pgetinker/Compiler.php b/pgetinker/Compiler.php index 130f107..426cdef 100644 --- a/pgetinker/Compiler.php +++ b/pgetinker/Compiler.php @@ -547,4 +547,53 @@ public function build() $this->cleanUp(); return false; } + + public function healthCheck() + { + $this->setWorkingDirectory("/tmp"); + + try + { + $compilerProcessResult = Process::env($this->environmentVariables) + ->path($this->workingDirectory) + ->timeout(intval(env("COMPILER_TIMEOUT", 10))) + ->command([ + "nsjail", + "--config", + base_path() . "/third_party/nsjail-emscripten.cfg", + "-B", + "{$this->workingDirectory}:/user", + "--", + "/opt/emsdk/upstream/emscripten/em++", + "-v", + ]) + ->run(); + + $this->compilerExitCode = $compilerProcessResult->exitCode(); + + $didTheThingSuccessfully = ($this->compilerExitCode == 0); + + $this->output = array_merge( + $this->output, + explode("\n", $compilerProcessResult->output()) + ); + + $this->errors = array_merge( + $this->errors, + explode("\n", $compilerProcessResult->errorOutput()) + ); + + if($this->compilerExitCode == 137) + { + $this->errors[] = "Compiler Killed (SIGTERM)"; + } + } + catch(Exception $e) + { + $this->errors[] = "compiler timed out on health check"; + $didTheThingSuccessfully = false; + } + + return $didTheThingSuccessfully; + } } From 029fae52268dd733b4be4b1e75f5b0b1cec1dc17 Mon Sep 17 00:00:00 2001 From: Moros Smith Date: Sun, 12 May 2024 02:05:13 -0400 Subject: [PATCH 2/3] test the compiler health check --- tests/Feature/CompilerTest.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/Feature/CompilerTest.php b/tests/Feature/CompilerTest.php index 5d670b5..7c47f9e 100644 --- a/tests/Feature/CompilerTest.php +++ b/tests/Feature/CompilerTest.php @@ -9,6 +9,19 @@ class CompilerTest extends TestCase { + public function test_compiler_exists(): void + { + if(Storage::disk("local")->exists(__FUNCTION__)) + Storage::disk("local")->deleteDirectory(__FUNCTION__); + + Storage::disk("local")->makeDirectory(__FUNCTION__); + $workingDirectory = Storage::disk("local")->path(__FUNCTION__); + $testSourceDirectory = __DIR__ . "/compiler-test-source"; + + $compiler = new Compiler(); + $this->assertTrue($compiler->healthCheck()); + Storage::disk("local")->deleteDirectory(__FUNCTION__); + } public function test_compiler_builds_hello_world(): void { @@ -24,9 +37,11 @@ public function test_compiler_builds_hello_world(): void $compiler->setCode(file_get_contents("{$testSourceDirectory}/hello-world.cpp")); $this->assertTrue($compiler->build()); + + Storage::disk("local")->deleteDirectory(__FUNCTION__); } - public function test_compiler_build_timeout(): void + public function test_compiler_build_memory_ice_killer(): void { if(Storage::disk("local")->exists(__FUNCTION__)) Storage::disk("local")->deleteDirectory(__FUNCTION__); @@ -40,6 +55,8 @@ public function test_compiler_build_timeout(): void $compiler->setCode(file_get_contents("{$testSourceDirectory}/ice-timeout.cpp")); $this->assertFalse($compiler->build()); + + Storage::disk("local")->deleteDirectory(__FUNCTION__); } public function test_compiler_builds_example(): void @@ -56,6 +73,8 @@ public function test_compiler_builds_example(): void $compiler->setCode(file_get_contents("{$testSourceDirectory}/example.cpp")); $this->assertTrue($compiler->build()); + + Storage::disk("local")->deleteDirectory(__FUNCTION__); } public function test_compiler_absolute_and_relative_include_trap(): void @@ -72,6 +91,8 @@ public function test_compiler_absolute_and_relative_include_trap(): void $compiler->setCode(file_get_contents("{$testSourceDirectory}/absolute-or-relative.cpp")); $this->assertFalse($compiler->build()); + + Storage::disk("local")->deleteDirectory(__FUNCTION__); } public function test_remote_includes(): void @@ -88,6 +109,8 @@ public function test_remote_includes(): void $compiler->setCode(file_get_contents("{$testSourceDirectory}/remote-includes.cpp")); $this->assertTrue($compiler->build()); + + Storage::disk("local")->deleteDirectory(__FUNCTION__); } } From 53ee5b031dd2e1c7b63dcaa82e4631118df0e5c6 Mon Sep 17 00:00:00 2001 From: Moros Smith Date: Sun, 12 May 2024 02:11:59 -0400 Subject: [PATCH 3/3] add health-check api endpoint --- app/Http/Controllers/CodeController.php | 21 ++++++++++++++++++--- routes/api.php | 1 + 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/CodeController.php b/app/Http/Controllers/CodeController.php index d9f1c24..504f816 100644 --- a/app/Http/Controllers/CodeController.php +++ b/app/Http/Controllers/CodeController.php @@ -3,14 +3,13 @@ namespace App\Http\Controllers; use App\Models\Code; -use Exception; + use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Process; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; -use PGEtinker\Compiler; +use PGEtinker\Compiler; use function PGEtinker\Utils\hashCode; class CodeController extends Controller @@ -78,6 +77,22 @@ function Share(Request $request) return response([ "statusCode" => 500, "message" => "some major server malfunction" ], 500)->header("Content-Type", "application/json"); } + function HealthCheck() + { + $compiler = new Compiler(); + if($compiler->healthCheck()) + { + return response([ + "statusCode" => 200, + "message" => "healthy" + ], 200); + } + + return response([ + "statusCode" => 400, + "message" => "unhealthy" + ], 400); + } function compileCode($code) { diff --git a/routes/api.php b/routes/api.php index e0ef33e..dcec33b 100644 --- a/routes/api.php +++ b/routes/api.php @@ -10,6 +10,7 @@ Route::post("/share", [CodeController::class, "Share" ]); Route::post("/compile", [CodeController::class, "Compile" ]); +Route::get("/health-check", [CodeController::class, "HealthCheck" ]); Route::get("model/{version}", function(Request $request, string $version) {