Skip to content

Commit

Permalink
Merge pull request #63 from Moros1138/add-api-and-compiler-health-check
Browse files Browse the repository at this point in the history
Add api and compiler health check
  • Loading branch information
Moros1138 authored May 12, 2024
2 parents 367dab9 + 53ee5b0 commit ce78fee
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
21 changes: 18 additions & 3 deletions app/Http/Controllers/CodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down
49 changes: 49 additions & 0 deletions pgetinker/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
25 changes: 24 additions & 1 deletion tests/Feature/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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__);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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__);
}

}

0 comments on commit ce78fee

Please sign in to comment.