From 1707ca5f3b16c274d5696b2479a53c2127486a43 Mon Sep 17 00:00:00 2001 From: Moros Smith Date: Sat, 4 May 2024 21:33:42 -0400 Subject: [PATCH 1/9] install deps for s3 bucket storage --- composer.json | 1 + composer.lock | 296 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 289 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 23db944..5cbe368 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ "laravel/framework": "^11.0", "laravel/sanctum": "^4.0", "laravel/tinker": "^2.9", + "league/flysystem-aws-s3-v3": "^3.0", "symfony/process": "^7.0" }, "require-dev": { diff --git a/composer.lock b/composer.lock index a739e0b..b0c9bde 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,157 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "98754fa2c3a24cebb2869e901a5b34b7", + "content-hash": "91d5eaed66ffca985042d731bef1cdb2", "packages": [ + { + "name": "aws/aws-crt-php", + "version": "v1.2.5", + "source": { + "type": "git", + "url": "https://github.com/awslabs/aws-crt-php.git", + "reference": "0ea1f04ec5aa9f049f97e012d1ed63b76834a31b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/awslabs/aws-crt-php/zipball/0ea1f04ec5aa9f049f97e012d1ed63b76834a31b", + "reference": "0ea1f04ec5aa9f049f97e012d1ed63b76834a31b", + "shasum": "" + }, + "require": { + "php": ">=5.5" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.35||^5.6.3||^9.5", + "yoast/phpunit-polyfills": "^1.0" + }, + "suggest": { + "ext-awscrt": "Make sure you install awscrt native extension to use any of the functionality." + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "AWS SDK Common Runtime Team", + "email": "aws-sdk-common-runtime@amazon.com" + } + ], + "description": "AWS Common Runtime for PHP", + "homepage": "https://github.com/awslabs/aws-crt-php", + "keywords": [ + "amazon", + "aws", + "crt", + "sdk" + ], + "support": { + "issues": "https://github.com/awslabs/aws-crt-php/issues", + "source": "https://github.com/awslabs/aws-crt-php/tree/v1.2.5" + }, + "time": "2024-04-19T21:30:56+00:00" + }, + { + "name": "aws/aws-sdk-php", + "version": "3.305.9", + "source": { + "type": "git", + "url": "https://github.com/aws/aws-sdk-php.git", + "reference": "a611af9a40a5d93f2f04427b322dbb6044e90327" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/a611af9a40a5d93f2f04427b322dbb6044e90327", + "reference": "a611af9a40a5d93f2f04427b322dbb6044e90327", + "shasum": "" + }, + "require": { + "aws/aws-crt-php": "^1.2.3", + "ext-json": "*", + "ext-pcre": "*", + "ext-simplexml": "*", + "guzzlehttp/guzzle": "^6.5.8 || ^7.4.5", + "guzzlehttp/promises": "^1.4.0 || ^2.0", + "guzzlehttp/psr7": "^1.9.1 || ^2.4.5", + "mtdowling/jmespath.php": "^2.6", + "php": ">=7.2.5", + "psr/http-message": "^1.0 || ^2.0" + }, + "require-dev": { + "andrewsville/php-token-reflection": "^1.4", + "aws/aws-php-sns-message-validator": "~1.0", + "behat/behat": "~3.0", + "composer/composer": "^1.10.22", + "dms/phpunit-arraysubset-asserts": "^0.4.0", + "doctrine/cache": "~1.4", + "ext-dom": "*", + "ext-openssl": "*", + "ext-pcntl": "*", + "ext-sockets": "*", + "nette/neon": "^2.3", + "paragonie/random_compat": ">= 2", + "phpunit/phpunit": "^5.6.3 || ^8.5 || ^9.5", + "psr/cache": "^1.0", + "psr/simple-cache": "^1.0", + "sebastian/comparator": "^1.2.3 || ^4.0", + "yoast/phpunit-polyfills": "^1.0" + }, + "suggest": { + "aws/aws-php-sns-message-validator": "To validate incoming SNS notifications", + "doctrine/cache": "To use the DoctrineCacheAdapter", + "ext-curl": "To send requests using cURL", + "ext-openssl": "Allows working with CloudFront private distributions and verifying received SNS messages", + "ext-sockets": "To use client-side monitoring" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0-dev" + } + }, + "autoload": { + "files": [ + "src/functions.php" + ], + "psr-4": { + "Aws\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Amazon Web Services", + "homepage": "http://aws.amazon.com" + } + ], + "description": "AWS SDK for PHP - Use Amazon Web Services in your PHP project", + "homepage": "http://aws.amazon.com/sdkforphp", + "keywords": [ + "amazon", + "aws", + "cloud", + "dynamodb", + "ec2", + "glacier", + "s3", + "sdk" + ], + "support": { + "forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80", + "issues": "https://github.com/aws/aws-sdk-php/issues", + "source": "https://github.com/aws/aws-sdk-php/tree/3.305.9" + }, + "time": "2024-05-03T18:09:03+00:00" + }, { "name": "brick/math", "version": "0.11.0", @@ -1775,6 +1924,71 @@ ], "time": "2024-04-07T19:17:50+00:00" }, + { + "name": "league/flysystem-aws-s3-v3", + "version": "3.27.0", + "source": { + "type": "git", + "url": "https://github.com/thephpleague/flysystem-aws-s3-v3.git", + "reference": "3e6ce2f972f1470db779f04d29c289dcd2c32837" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/thephpleague/flysystem-aws-s3-v3/zipball/3e6ce2f972f1470db779f04d29c289dcd2c32837", + "reference": "3e6ce2f972f1470db779f04d29c289dcd2c32837", + "shasum": "" + }, + "require": { + "aws/aws-sdk-php": "^3.295.10", + "league/flysystem": "^3.10.0", + "league/mime-type-detection": "^1.0.0", + "php": "^8.0.2" + }, + "conflict": { + "guzzlehttp/guzzle": "<7.0", + "guzzlehttp/ringphp": "<1.1.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "League\\Flysystem\\AwsS3V3\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Frank de Jonge", + "email": "info@frankdejonge.nl" + } + ], + "description": "AWS S3 filesystem adapter for Flysystem.", + "keywords": [ + "Flysystem", + "aws", + "file", + "files", + "filesystem", + "s3", + "storage" + ], + "support": { + "source": "https://github.com/thephpleague/flysystem-aws-s3-v3/tree/3.27.0" + }, + "funding": [ + { + "url": "https://ecologi.com/frankdejonge", + "type": "custom" + }, + { + "url": "https://github.com/frankdejonge", + "type": "github" + } + ], + "time": "2024-04-07T19:16:54+00:00" + }, { "name": "league/flysystem-local", "version": "3.25.1", @@ -1991,6 +2205,72 @@ ], "time": "2024-04-12T21:02:21+00:00" }, + { + "name": "mtdowling/jmespath.php", + "version": "2.7.0", + "source": { + "type": "git", + "url": "https://github.com/jmespath/jmespath.php.git", + "reference": "bbb69a935c2cbb0c03d7f481a238027430f6440b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/jmespath/jmespath.php/zipball/bbb69a935c2cbb0c03d7f481a238027430f6440b", + "reference": "bbb69a935c2cbb0c03d7f481a238027430f6440b", + "shasum": "" + }, + "require": { + "php": "^7.2.5 || ^8.0", + "symfony/polyfill-mbstring": "^1.17" + }, + "require-dev": { + "composer/xdebug-handler": "^3.0.3", + "phpunit/phpunit": "^8.5.33" + }, + "bin": [ + "bin/jp.php" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.7-dev" + } + }, + "autoload": { + "files": [ + "src/JmesPath.php" + ], + "psr-4": { + "JmesPath\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + }, + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Declaratively specify how to extract elements from a JSON document", + "keywords": [ + "json", + "jsonpath" + ], + "support": { + "issues": "https://github.com/jmespath/jmespath.php/issues", + "source": "https://github.com/jmespath/jmespath.php/tree/2.7.0" + }, + "time": "2023-08-25T10:54:48+00:00" + }, { "name": "nesbot/carbon", "version": "3.3.0", @@ -3416,16 +3696,16 @@ }, { "name": "symfony/deprecation-contracts", - "version": "v3.4.0", + "version": "v3.5.0", "source": { "type": "git", "url": "https://github.com/symfony/deprecation-contracts.git", - "reference": "7c3aff79d10325257a001fcf92d991f24fc967cf" + "reference": "0e0d29ce1f20deffb4ab1b016a7257c4f1e789a1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/7c3aff79d10325257a001fcf92d991f24fc967cf", - "reference": "7c3aff79d10325257a001fcf92d991f24fc967cf", + "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/0e0d29ce1f20deffb4ab1b016a7257c4f1e789a1", + "reference": "0e0d29ce1f20deffb4ab1b016a7257c4f1e789a1", "shasum": "" }, "require": { @@ -3434,7 +3714,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "3.4-dev" + "dev-main": "3.5-dev" }, "thanks": { "name": "symfony/contracts", @@ -3463,7 +3743,7 @@ "description": "A generic function and convention to trigger deprecation notices", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/deprecation-contracts/tree/v3.4.0" + "source": "https://github.com/symfony/deprecation-contracts/tree/v3.5.0" }, "funding": [ { @@ -3479,7 +3759,7 @@ "type": "tidelift" } ], - "time": "2023-05-23T14:45:45+00:00" + "time": "2024-04-18T09:32:20+00:00" }, { "name": "symfony/error-handler", From 4c943b3599f604d31ec3a84b548843e3b7ff2f75 Mon Sep 17 00:00:00 2001 From: Moros Smith Date: Sat, 4 May 2024 22:43:31 -0400 Subject: [PATCH 2/9] throw when FS fails --- config/filesystems.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/filesystems.php b/config/filesystems.php index 90c5568..6c340ae 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -43,7 +43,7 @@ "private" => 0755, ], ], - 'throw' => false, + 'throw' => true, ], 'public' => [ @@ -63,7 +63,7 @@ 'url' => env('AWS_URL'), 'endpoint' => env('AWS_ENDPOINT'), 'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false), - 'throw' => false, + 'throw' => true, ], ], From 74928f1b67650fd83ac8cd90def0de41e8d9ec0f Mon Sep 17 00:00:00 2001 From: Moros Smith Date: Sat, 4 May 2024 23:35:41 -0400 Subject: [PATCH 3/9] remove unused filter code --- app/Http/Controllers/CodeController.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/app/Http/Controllers/CodeController.php b/app/Http/Controllers/CodeController.php index e5f2340..8461da6 100644 --- a/app/Http/Controllers/CodeController.php +++ b/app/Http/Controllers/CodeController.php @@ -556,22 +556,8 @@ function compileCode($code) function filterOutput($text) { - $text = str_replace("/opt/emsdk/upstream/emscripten/cache/sysroot", "/***", $text); - return $text; - // $text = explode("\n", $text); - - // for($i = 0 $i < count($text)) - - // // $text = array_filter(explode("\n", $text), function($value) - // // { - // // return (strpos($value, "undefined symbol") !== false) || - // // (strpos($value, "duplicate symbol") !== false) || - // // (strpos($value, "pgetinker.cpp") === 0); - // // }); - - // return implode("\n", $text); } } From 99de3e26ef985269d19a6b5f092209c77b05a830 Mon Sep 17 00:00:00 2001 From: Moros Smith Date: Sat, 4 May 2024 23:36:47 -0400 Subject: [PATCH 4/9] use s3 bucket for cache, if configured --- app/Http/Controllers/CodeController.php | 27 +++++++++++++++---------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/CodeController.php b/app/Http/Controllers/CodeController.php index 8461da6..dede5e4 100644 --- a/app/Http/Controllers/CodeController.php +++ b/app/Http/Controllers/CodeController.php @@ -153,6 +153,7 @@ function hashCode($code) function compileCode($code) { + $cacheDisk = (!empty(env("AWS_BUCKET"))) ? Storage::disk("s3") : Storage::disk("local"); if($code == null) { Log::debug("Compile: missing required code parameters"); @@ -176,21 +177,22 @@ function compileCode($code) if(env("COMPILER_CACHING", false)) { - if(Storage::directoryMissing("compilerCache")) + + if(!$cacheDisk->exists("compilerCache")) { - Storage::createDirectory("compilerCache"); + $cacheDisk->makeDirectory("compilerCache"); } - if(Storage::directoryMissing(("remoteIncludeCache"))) + if(!$cacheDisk->exists("remoteIncludeCache")) { - Storage::createDirectory("remoteIncludeCache"); + $cacheDisk->makeDirectory("remoteIncludeCache"); } - if(Storage::fileExists("compilerCache/{$hashedCode}")) + if($cacheDisk->exists("compilerCache/{$hashedCode}")) { Log::debug("Compile: cache hit", ["hashedCode" => $hashedCode]); - $html = Storage::read("compilerCache/{$hashedCode}"); + $html = $cacheDisk->get("compilerCache/{$hashedCode}"); return [ "statusCode" => 200, @@ -303,10 +305,13 @@ function compileCode($code) if(env("COMPILER_CACHING", false)) { // if we have a cached version of the url's contents, don't pull it - if(Storage::fileExists("remoteIncludeCache/{$hashedUrl}")) + if($cacheDisk->exists("remoteIncludeCache/{$hashedUrl}")) { $log->info("remote include cache hit"); - Storage::copy("remoteIncludeCache/{$hashedUrl}", "{$directoryName}/{$potentialFilename}"); + Storage::put( + "{$directoryName}/{$potentialFilename}", + $cacheDisk->get("remoteIncludeCache/{$hashedUrl}") + ); $linesOfCode[$i] = '#include "' . $potentialFilename .'"'; continue; } @@ -367,8 +372,8 @@ function compileCode($code) if(env("COMPILER_CACHING", false)) { - $log->info("copying remote file to cache"); - Storage::copy("{$directoryName}/{$potentialFilename}", "remoteIncludeCache/{$hashedUrl}"); + $log->info("caching remotely included source file: $potentialFilename"); + $cacheDisk->put("remoteIncludeCache/{$hashedUrl}", $response->body()); } $linesOfCode[$i] = '#include "' . $potentialFilename .'"'; @@ -542,7 +547,7 @@ function compileCode($code) if(env("COMPILER_CACHING", false)) { - Storage::move("{$directoryName}/pgetinker.html", "compilerCache/{$hashedCode}"); + $cacheDisk->put("compilerCache/{$hashedCode}", $html); } Storage::deleteDirectory($directoryName); From 596114027a339edb7ee6542e9dfe8fcadd0d5485 Mon Sep 17 00:00:00 2001 From: Moros Smith Date: Sat, 4 May 2024 23:55:31 -0400 Subject: [PATCH 5/9] fix typo that could have affected test results --- tests/Feature/CodeControllerHashTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/CodeControllerHashTest.php b/tests/Feature/CodeControllerHashTest.php index 00e66db..8c74546 100644 --- a/tests/Feature/CodeControllerHashTest.php +++ b/tests/Feature/CodeControllerHashTest.php @@ -34,7 +34,7 @@ public function test_comment_hash(): void public function test_string_literal_hash(): void { $c = new CodeController(); - $this->assertFalse($c->hashCode('"this is a string') == $c->hashCode('"this is a longer string"')); + $this->assertFalse($c->hashCode('"this is a string"') == $c->hashCode('"this is a longer string"')); } public function test_one_line_macro(): void From 9aaad91ee00df416082d8d57b172ddaedc6bf1b6 Mon Sep 17 00:00:00 2001 From: Moros Smith Date: Sun, 5 May 2024 09:47:28 -0400 Subject: [PATCH 6/9] revampe compileCode to use local disk for compilation and remote disk for persistence --- app/Http/Controllers/CodeController.php | 161 +++++++++++++++--------- 1 file changed, 101 insertions(+), 60 deletions(-) diff --git a/app/Http/Controllers/CodeController.php b/app/Http/Controllers/CodeController.php index dede5e4..76ef9dc 100644 --- a/app/Http/Controllers/CodeController.php +++ b/app/Http/Controllers/CodeController.php @@ -153,7 +153,6 @@ function hashCode($code) function compileCode($code) { - $cacheDisk = (!empty(env("AWS_BUCKET"))) ? Storage::disk("s3") : Storage::disk("local"); if($code == null) { Log::debug("Compile: missing required code parameters"); @@ -173,26 +172,33 @@ function compileCode($code) ]; } + $localDisk = Storage::disk("local"); + $remoteDisk = (!empty(env("AWS_BUCKET"))) ? Storage::disk("s3") : Storage::disk("local"); + $hashedCode = $this->hashCode($code); if(env("COMPILER_CACHING", false)) { - - if(!$cacheDisk->exists("compilerCache")) + if(!$remoteDisk->exists("compilerCache")) { - $cacheDisk->makeDirectory("compilerCache"); + $remoteDisk->makeDirectory("compilerCache"); } - if(!$cacheDisk->exists("remoteIncludeCache")) + if(!$remoteDisk->exists("remoteIncludeCache")) { - $cacheDisk->makeDirectory("remoteIncludeCache"); + $remoteDisk->makeDirectory("remoteIncludeCache"); } - if($cacheDisk->exists("compilerCache/{$hashedCode}")) + if(!$remoteDisk->exists("workspaces")) + { + $remoteDisk->makeDirectory("workspaces"); + } + + if($remoteDisk->exists("compilerCache/{$hashedCode}")) { Log::debug("Compile: cache hit", ["hashedCode" => $hashedCode]); - $html = $cacheDisk->get("compilerCache/{$hashedCode}"); + $html = $remoteDisk->get("compilerCache/{$hashedCode}"); return [ "statusCode" => 200, @@ -200,21 +206,21 @@ function compileCode($code) "html" => $html, ]; } - + Log::debug("Compile: cache miss", ["hashedCode" => $hashedCode]); } - if(Storage::directoryMissing("workspaces")) + if(!$localDisk->exists("workspaces")) { - Storage::createDirectory("workspaces"); + $localDisk->makeDirectory("workspaces"); } $directoryName = "workspaces/" . Str::uuid(); - Storage::createDirectory($directoryName); + $localDisk->makeDirectory($directoryName); $log = new Logger("compiler"); - $logHandler = new StreamHandler(Storage::path($directoryName) . "/compiler.log"); + $logHandler = new StreamHandler($localDisk->path($directoryName) . "/compiler.log"); $logHandler->setFormatter(new LineFormatter(null, null, true, true)); $log->pushHandler($logHandler); @@ -305,12 +311,12 @@ function compileCode($code) if(env("COMPILER_CACHING", false)) { // if we have a cached version of the url's contents, don't pull it - if($cacheDisk->exists("remoteIncludeCache/{$hashedUrl}")) + if($remoteDisk->exists("remoteIncludeCache/{$hashedUrl}")) { $log->info("remote include cache hit"); - Storage::put( + $localDisk->put( "{$directoryName}/{$potentialFilename}", - $cacheDisk->get("remoteIncludeCache/{$hashedUrl}") + $remoteDisk->get("remoteIncludeCache/{$hashedUrl}") ); $linesOfCode[$i] = '#include "' . $potentialFilename .'"'; continue; @@ -368,12 +374,12 @@ function compileCode($code) } $log->info("writing remote file to: {$directoryName}/{$potentialFilename}"); - Storage::put("{$directoryName}/{$potentialFilename}", $response->body()); + $localDisk->put("{$directoryName}/{$potentialFilename}", $response->body()); if(env("COMPILER_CACHING", false)) { $log->info("caching remotely included source file: $potentialFilename"); - $cacheDisk->put("remoteIncludeCache/{$hashedUrl}", $response->body()); + $remoteDisk->put("remoteIncludeCache/{$hashedUrl}", $response->body()); } $linesOfCode[$i] = '#include "' . $potentialFilename .'"'; @@ -395,13 +401,13 @@ function compileCode($code) } $version = "v0.01"; - $workspaceDirectory = Storage::path($directoryName); + $workspaceDirectory = $localDisk->path($directoryName); $thirdPartyDirectory = base_path() . "/third_party"; $compilerEnvironment = env("COMPILER_ENVIRONMENT", "local"); $log->info("writing linesOfCode to {$directoryName}/pgetinker.cpp"); - Storage::put("{$directoryName}/pgetinker.cpp", implode("\n", $linesOfCode)); + $localDisk->put("{$directoryName}/pgetinker.cpp", implode("\n", $linesOfCode)); $environmentVariables = []; $compilerCommand = null; @@ -492,6 +498,8 @@ function compileCode($code) ->command($compilerCommand)->run(); $log->info("compiler exited with code: " . $compilerProcessResult->exitCode()); + + $response = null; if($compilerProcessResult->exitCode() !== 0) { @@ -505,58 +513,91 @@ function compileCode($code) "stdout" => $compilerProcessResult->output(), "stderr" => $compilerProcessResult->errorOutput(), ]); - - return $response; } - $log->info("invoking the linker"); - $linkerProcessResult = Process::env($environmentVariables) - ->path($workspaceDirectory) - ->timeout(10) - ->command($linkerCommand)->run(); - - if($linkerProcessResult->exitCode() !== 0) + // if we're here and $response is still null, the compile stage succeeded, invoke linker + if($response == null) { - $response = [ - "statusCode" => 400, - "stdout" => $this->filterOutput($linkerProcessResult->output()), - "stderr" => $this->filterOutput($linkerProcessResult->errorOutput()), - ]; - - $log->error("linking failed", [ - "stdout" => $linkerProcessResult->output(), - "stderr" => $linkerProcessResult->errorOutput(), - ]); - - return $response; + $log->info("invoking the linker"); + $linkerProcessResult = Process::env($environmentVariables) + ->path($workspaceDirectory) + ->timeout(10) + ->command($linkerCommand)->run(); + + if($linkerProcessResult->exitCode() !== 0) + { + $response = [ + "statusCode" => 400, + "stdout" => $this->filterOutput($linkerProcessResult->output()), + "stderr" => $this->filterOutput($linkerProcessResult->errorOutput()), + ]; + + $log->error("linking failed", [ + "stdout" => $linkerProcessResult->output(), + "stderr" => $linkerProcessResult->errorOutput(), + ]); + } + } + + if($response == null) + { + if(!$localDisk->exists("{$directoryName}/pgetinker.html")) + { + $response = [ + "statusCode" => 42069, + "message" => "something really bad up happened in order for this to occur. contact the administrator", + ]; + + Log::debug("Compile: failed beyond the linker stage", $response); + } } - if(Storage::fileMissing("{$directoryName}/pgetinker.html")) + if($response == null) { + // if we've made it here, SUCCESS! + $html = $localDisk->get("{$directoryName}/pgetinker.html"); + + if(env("COMPILER_CACHING", false)) + { + $remoteDisk->put("compilerCache/{$hashedCode}", $html); + } + + $localDisk->deleteDirectory($directoryName); + $response = [ - "statusCode" => 42069, - "message" => "something really bad up happened in order for this to occur. contact the administrator", + "statusCode" => 200, + "hash" => $hashedCode, + "html" => $html, ]; - - Log::debug("Compile: failed beyond the linker stage", $response); - return $response; } - - // if we've made it here, SUCCESS! - $html = Storage::read("{$directoryName}/pgetinker.html"); - - if(env("COMPILER_CACHING", false)) + + if($response["statusCode"] != 200) { - $cacheDisk->put("compilerCache/{$hashedCode}", $html); + // it's possible that the local disk and remote disk + // are the same + if(get_class($localDisk) != get_class($remoteDisk)) + { + // get the local files + $files = $localDisk->files($directoryName); + + // create the remote directory + $remoteDisk->makeDirectory($directoryName); + + for($i = 0; $i < count($files); $i++) + { + // copy the file from the localDisk to the remoteDisk + $remoteDisk->put( + $files[$i], + $localDisk->get($files[$i]) + ); + } + + // remove the local files + $localDisk->deleteDirectory($directoryName); + } } - Storage::deleteDirectory($directoryName); - - return [ - "statusCode" => 200, - "hash" => $hashedCode, - "html" => $html, - ]; + return $response; } function filterOutput($text) From bcbf012fcbbd35dda25dceb1c1639b1decd6ac1e Mon Sep 17 00:00:00 2001 From: Moros Smith Date: Sun, 5 May 2024 09:55:16 -0400 Subject: [PATCH 7/9] stderr in order to display the error to the user --- app/Http/Controllers/CodeController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/CodeController.php b/app/Http/Controllers/CodeController.php index 76ef9dc..aa8ad80 100644 --- a/app/Http/Controllers/CodeController.php +++ b/app/Http/Controllers/CodeController.php @@ -545,7 +545,7 @@ function compileCode($code) { $response = [ "statusCode" => 42069, - "message" => "something really bad up happened in order for this to occur. contact the administrator", + "stderr" => "something really bad happened in order for this to occur. contact the administrator", ]; Log::debug("Compile: failed beyond the linker stage", $response); From 8812fca471b9a2b5b471d60f740357fd14a5617c Mon Sep 17 00:00:00 2001 From: Moros Smith Date: Sun, 5 May 2024 09:55:25 -0400 Subject: [PATCH 8/9] helpful log messages --- app/Http/Controllers/CodeController.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Http/Controllers/CodeController.php b/app/Http/Controllers/CodeController.php index aa8ad80..dbd7540 100644 --- a/app/Http/Controllers/CodeController.php +++ b/app/Http/Controllers/CodeController.php @@ -569,6 +569,8 @@ function compileCode($code) "hash" => $hashedCode, "html" => $html, ]; + + Log::info("Compile: finished successfully"); } if($response["statusCode"] != 200) @@ -577,6 +579,7 @@ function compileCode($code) // are the same if(get_class($localDisk) != get_class($remoteDisk)) { + Log::info("uploading files to remote disk."); // get the local files $files = $localDisk->files($directoryName); @@ -595,6 +598,7 @@ function compileCode($code) // remove the local files $localDisk->deleteDirectory($directoryName); } + Log::info("Compile: finished disgracefully"); } return $response; From 9f23001bad97617fedde93e0a427bc645b4959f1 Mon Sep 17 00:00:00 2001 From: Moros Smith Date: Sun, 5 May 2024 10:13:45 -0400 Subject: [PATCH 9/9] add database logger --- composer.json | 3 +- composer.lock | 125 +++++++++++++++++- config/logging.php | 6 + ..._05_05_140817_create_laravel_log_table.php | 20 +++ 4 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 database/migrations/2024_05_05_140817_create_laravel_log_table.php diff --git a/composer.json b/composer.json index 5cbe368..1c0ddd0 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ "laravel/sanctum": "^4.0", "laravel/tinker": "^2.9", "league/flysystem-aws-s3-v3": "^3.0", - "symfony/process": "^7.0" + "symfony/process": "^7.0", + "yoeriboven/laravel-log-db": "^1.2" }, "require-dev": { "fakerphp/faker": "^1.23", diff --git a/composer.lock b/composer.lock index b0c9bde..458d5b7 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "91d5eaed66ffca985042d731bef1cdb2", + "content-hash": "fd486d3b4f91443f151c7196d8aceabc", "packages": [ { "name": "aws/aws-crt-php", @@ -3462,6 +3462,66 @@ ], "time": "2023-11-08T05:53:05+00:00" }, + { + "name": "spatie/laravel-package-tools", + "version": "1.16.4", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-package-tools.git", + "reference": "ddf678e78d7f8b17e5cdd99c0c3413a4a6592e53" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-package-tools/zipball/ddf678e78d7f8b17e5cdd99c0c3413a4a6592e53", + "reference": "ddf678e78d7f8b17e5cdd99c0c3413a4a6592e53", + "shasum": "" + }, + "require": { + "illuminate/contracts": "^9.28|^10.0|^11.0", + "php": "^8.0" + }, + "require-dev": { + "mockery/mockery": "^1.5", + "orchestra/testbench": "^7.7|^8.0", + "pestphp/pest": "^1.22", + "phpunit/phpunit": "^9.5.24", + "spatie/pest-plugin-test-time": "^1.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\LaravelPackageTools\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "role": "Developer" + } + ], + "description": "Tools for creating Laravel packages", + "homepage": "https://github.com/spatie/laravel-package-tools", + "keywords": [ + "laravel-package-tools", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/laravel-package-tools/issues", + "source": "https://github.com/spatie/laravel-package-tools/tree/1.16.4" + }, + "funding": [ + { + "url": "https://github.com/spatie", + "type": "github" + } + ], + "time": "2024-03-20T07:29:11+00:00" + }, { "name": "symfony/clock", "version": "v7.0.5", @@ -6027,6 +6087,69 @@ "source": "https://github.com/webmozarts/assert/tree/1.11.0" }, "time": "2022-06-03T18:03:27+00:00" + }, + { + "name": "yoeriboven/laravel-log-db", + "version": "1.2", + "source": { + "type": "git", + "url": "https://github.com/yoeriboven/laravel-log-db.git", + "reference": "0cfe64b03254eb441865af90109372a9c1485448" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/yoeriboven/laravel-log-db/zipball/0cfe64b03254eb441865af90109372a9c1485448", + "reference": "0cfe64b03254eb441865af90109372a9c1485448", + "shasum": "" + }, + "require": { + "php": "^8.1", + "spatie/laravel-package-tools": "^1.9.2" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.39", + "nunomaduro/collision": "^6.0", + "orchestra/testbench": "^7.0", + "pestphp/pest": "^1.21", + "pestphp/pest-plugin-laravel": "^1.1", + "phpunit/phpunit": "^9.5" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Yoeriboven\\LaravelLogDb\\DatabaseLoggerServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Yoeriboven\\LaravelLogDb\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Yoeri Boven", + "email": "yoeriboven@msn.com", + "role": "Developer" + } + ], + "description": "A database driver for logging with Laravel", + "homepage": "https://github.com/yoeriboven/laravel-log-db", + "keywords": [ + "laravel", + "laravel-log-db", + "yoeriboven" + ], + "support": { + "issues": "https://github.com/yoeriboven/laravel-log-db/issues", + "source": "https://github.com/yoeriboven/laravel-log-db/tree/1.2" + }, + "time": "2024-01-13T13:33:00+00:00" } ], "packages-dev": [ diff --git a/config/logging.php b/config/logging.php index d526b64..85826a1 100644 --- a/config/logging.php +++ b/config/logging.php @@ -4,6 +4,7 @@ use Monolog\Handler\StreamHandler; use Monolog\Handler\SyslogUdpHandler; use Monolog\Processor\PsrLogMessageProcessor; +use Yoeriboven\LaravelLogDb\DatabaseLogger; return [ @@ -126,6 +127,11 @@ 'emergency' => [ 'path' => storage_path('logs/laravel.log'), ], + + 'db' => [ + 'driver' => 'custom', + 'via' => DatabaseLogger::class, + ], ], diff --git a/database/migrations/2024_05_05_140817_create_laravel_log_table.php b/database/migrations/2024_05_05_140817_create_laravel_log_table.php new file mode 100644 index 0000000..f19e2a7 --- /dev/null +++ b/database/migrations/2024_05_05_140817_create_laravel_log_table.php @@ -0,0 +1,20 @@ +id(); + $table->string('level_name'); + $table->unsignedSmallInteger('level'); + $table->string('message'); + $table->dateTime('logged_at'); + $table->json('context'); + $table->json('extra'); + }); + } +};