diff --git a/front/js/svelte/records/elements/ReferenceInlineButtons.svelte b/front/js/svelte/records/elements/ReferenceInlineButtons.svelte index 80911d8..1240402 100644 --- a/front/js/svelte/records/elements/ReferenceInlineButtons.svelte +++ b/front/js/svelte/records/elements/ReferenceInlineButtons.svelte @@ -67,7 +67,7 @@ class=" button" on:click={(e) => createInlineReference(e, schema.name)} - >{schema.name} + >{schema.label} {/each} @@ -78,7 +78,7 @@ {/each} diff --git a/src/Channel/ChannelService.php b/src/Channel/ChannelService.php index e514be7..75fa258 100644 --- a/src/Channel/ChannelService.php +++ b/src/Channel/ChannelService.php @@ -31,7 +31,7 @@ final class ChannelService $schemasCollection = (new Collection($schemasArray["schemas"] ?? []))->map([$schemaService, 'fromArray']); $userCommands = []; - foreach (config("lucent.commands") as $signature => $desc) { + foreach (config("lucent.commands") ?? [] as $signature => $desc) { $userCommands[] = new UserCommand($desc, $signature); } diff --git a/src/Command/CommandRepo.php b/src/Command/CommandRepo.php new file mode 100644 index 0000000..ad87ba6 --- /dev/null +++ b/src/Command/CommandRepo.php @@ -0,0 +1,40 @@ +where("signature", $signature)->first(); + if (empty($row)) { + return null; + } + return CommandLogItem::fromDB($row); + } + + + public function upsertCommand(CommandLogItem $commandLogItem): void + { + $foundCommandLogItem = $this->findBySignature($commandLogItem->signature); + if (empty($foundCommandLogItem)) { + DB::table("command_logs")->insert(toArray($commandLogItem)); + return; + } + + DB::table("command_logs")->where("signature", $commandLogItem->signature)->update(toArray($commandLogItem)); + } + + public function appendToLogs(string $signature, string $line): void + { + $res = DB::update( + 'update command_logs set logs = logs || ? where signature = ?', + [$line, $signature] + ); + + } +} \ No newline at end of file diff --git a/src/Command/CommandService.php b/src/Command/CommandService.php new file mode 100644 index 0000000..5434235 --- /dev/null +++ b/src/Command/CommandService.php @@ -0,0 +1,60 @@ +commandRepo->findBySignature($signature); + + if (empty($commandLogItem)) { + $commandLogItem = new CommandLogItem( + id: Id::new(), + signature: $signature, + pid: null, + logs: "" + ); + } elseif ($this->commandIsRunning($commandLogItem->pid)) { + throw new LucentException('Command is already running'); + } + + $commandLogItem->pid = $this->runCommand($signature); + $commandLogItem->logs = ""; + $this->commandRepo->upsertCommand($commandLogItem); + return $commandLogItem; + } + + public function logWriter(string $signature): callable + { + return function (string $line) use($signature) { + $this->commandRepo->appendToLogs($signature, $line.PHP_EOL); + }; + } + + private function runCommand(string $signature): int + { + $phpBinaryFinder = new PhpExecutableFinder(); + $phpBinaryPath = $phpBinaryFinder->find(); + $pid = (int)shell_exec("cd " . base_path() . " && $phpBinaryPath artisan {$signature} > /dev/null 2>&1 & echo $!"); + return $pid; + } + + public function commandIsRunning(int $pid): bool + { + return file_exists("/proc/$pid"); + } +} \ No newline at end of file diff --git a/src/Command/Data/CommandLogItem.php b/src/Command/Data/CommandLogItem.php new file mode 100644 index 0000000..20ce650 --- /dev/null +++ b/src/Command/Data/CommandLogItem.php @@ -0,0 +1,27 @@ +id, + signature: $data->signature, + pid: $data->pid, + logs: $data->logs, + ); + } +} \ No newline at end of file diff --git a/src/Database/migrations/2014_10_12_000000_create_users_table.php b/src/Database/migrations/0000_create_users_table.php similarity index 95% rename from src/Database/migrations/2014_10_12_000000_create_users_table.php rename to src/Database/migrations/0000_create_users_table.php index 1508f94..3e241a4 100644 --- a/src/Database/migrations/2014_10_12_000000_create_users_table.php +++ b/src/Database/migrations/0000_create_users_table.php @@ -6,7 +6,7 @@ use Illuminate\Support\Facades\Schema; return new class extends Migration { - protected $connection = 'lucentDb'; + protected $connection = 'lucentdb'; /** * Run the migrations. diff --git a/src/Database/migrations/2022_11_16_192907_create_sessions_table.php b/src/Database/migrations/0001_create_sessions_table.php similarity index 95% rename from src/Database/migrations/2022_11_16_192907_create_sessions_table.php rename to src/Database/migrations/0001_create_sessions_table.php index ed8c04b..ad4fe4d 100644 --- a/src/Database/migrations/2022_11_16_192907_create_sessions_table.php +++ b/src/Database/migrations/0001_create_sessions_table.php @@ -6,7 +6,7 @@ use Illuminate\Support\Facades\Schema; return new class extends Migration { - protected $connection = 'lucentDb'; + protected $connection = 'lucentdb'; /** * Run the migrations. diff --git a/src/Database/migrations/2023_01_20_162322_records.php b/src/Database/migrations/0002_records.php similarity index 96% rename from src/Database/migrations/2023_01_20_162322_records.php rename to src/Database/migrations/0002_records.php index e70a613..07d2cda 100644 --- a/src/Database/migrations/2023_01_20_162322_records.php +++ b/src/Database/migrations/0002_records.php @@ -6,7 +6,7 @@ use Illuminate\Support\Facades\Schema; return new class extends Migration { - protected $connection = 'lucentDb'; + protected $connection = 'lucentdb'; /** * Run the migrations. * diff --git a/src/Database/migrations/2023_03_02_152122_revisions.php b/src/Database/migrations/0003_revisions.php similarity index 95% rename from src/Database/migrations/2023_03_02_152122_revisions.php rename to src/Database/migrations/0003_revisions.php index e5982f3..88522f7 100644 --- a/src/Database/migrations/2023_03_02_152122_revisions.php +++ b/src/Database/migrations/0003_revisions.php @@ -6,7 +6,7 @@ use Illuminate\Support\Facades\Schema; return new class extends Migration { - protected $connection = 'lucentDb'; + protected $connection = 'lucentdb'; /** * Run the migrations. * diff --git a/src/Database/migrations/2023_10_22_152122_generatedIndexColumn.php b/src/Database/migrations/0004_generatedIndexColumn.php similarity index 94% rename from src/Database/migrations/2023_10_22_152122_generatedIndexColumn.php rename to src/Database/migrations/0004_generatedIndexColumn.php index 02fe933..11de904 100644 --- a/src/Database/migrations/2023_10_22_152122_generatedIndexColumn.php +++ b/src/Database/migrations/0004_generatedIndexColumn.php @@ -6,7 +6,7 @@ use Illuminate\Support\Facades\Schema; return new class extends Migration { - protected $connection = 'lucentDb'; + protected $connection = 'lucentdb'; /** * Run the migrations. * diff --git a/src/Database/migrations/2023_10_23_152122_RecordsUpdatedIndex.php b/src/Database/migrations/0005_RecordsUpdatedIndex.php similarity index 95% rename from src/Database/migrations/2023_10_23_152122_RecordsUpdatedIndex.php rename to src/Database/migrations/0005_RecordsUpdatedIndex.php index 6f7c23f..cd73c55 100644 --- a/src/Database/migrations/2023_10_23_152122_RecordsUpdatedIndex.php +++ b/src/Database/migrations/0005_RecordsUpdatedIndex.php @@ -6,7 +6,7 @@ use Illuminate\Support\Facades\Schema; return new class extends Migration { - protected $connection = 'lucentDb'; + protected $connection = 'lucentdb'; /** * Run the migrations. diff --git a/src/Database/migrations/0006_CommandLogs.php b/src/Database/migrations/0006_CommandLogs.php new file mode 100644 index 0000000..d92a602 --- /dev/null +++ b/src/Database/migrations/0006_CommandLogs.php @@ -0,0 +1,35 @@ +uuid('id')->primary(); + $table->string('signature'); + $table->integer('pid')->nullable(); + $table->text('logs'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('command_logs'); + } +}; diff --git a/src/Http/Controller/BuildController.php b/src/Http/Controller/BuildController.php index b17e2f7..cc8c2ef 100644 --- a/src/Http/Controller/BuildController.php +++ b/src/Http/Controller/BuildController.php @@ -6,13 +6,17 @@ use App\Http\Controllers\Controller; use Illuminate\Contracts\View\View; use Illuminate\Http\Request; use Lucent\Channel\ChannelService; +use Lucent\Command\CommandRepo; +use Lucent\Command\CommandService; use Lucent\Svelte\Svelte; class BuildController extends Controller { public function __construct( - public readonly Svelte $svelte, - public readonly ChannelService $channelService, + private readonly Svelte $svelte, + private readonly ChannelService $channelService, + private readonly CommandService $commandService, + private readonly CommandRepo $commandRepo, ) { } @@ -20,19 +24,8 @@ class BuildController extends Controller public function build(Request $request) { $commandSignature = $request->route("signature"); - $buildLogFile = $this->getLogFile($commandSignature); - $pidFile = $this->getPidFile($commandSignature); - if (file_exists($buildLogFile)) { - unlink($buildLogFile); - } - if (file_exists($pidFile)) { - unlink($pidFile); - } + $this->commandService->run($commandSignature); - exec("cd " . base_path() . " && php8.3 artisan {$commandSignature} > " . $buildLogFile . " 2>&1 & echo $!", $op); - $pid = (int)$op[0]; - file_put_contents($pidFile, $pid); - return redirect($this->channelService->channel->lucentUrl . "/command-report/" . $commandSignature); } public function report(Request $request): View @@ -55,8 +48,10 @@ class BuildController extends Controller $commandSignature = $request->route("signature"); return response()->stream(function () use ($commandSignature) { while (true) { + + $commandLogItem = $this->commandRepo->findBySignature($commandSignature); $data["date"] = date("Y-m-d H:i:s"); - $data["logs"] = file_get_contents($this->getLogFile($commandSignature)); + $data["logs"] = $commandLogItem->logs ?? ""; // $lines = explode("\n",$data["logs"]); echo 'data: ' . json_encode($data); @@ -64,19 +59,8 @@ class BuildController extends Controller ob_flush(); flush(); - - $pidFile = $this->getPidFile($commandSignature); - if (file_exists($pidFile)) { - $pid = file_get_contents($pidFile); - } else { - break; - } - - if (empty($pid)) { - break; - } - - if (!file_exists("/proc/$pid")) { + logger($this->commandService->commandIsRunning($commandLogItem->pid)); + if (!$this->commandService->commandIsRunning($commandLogItem->pid)) { break; } @@ -93,13 +77,4 @@ class BuildController extends Controller ]); } - private function getLogFile(string $signature): string - { - return storage_path("lucent/$signature.log"); - } - - private function getPidFile(string $signature): string - { - return storage_path("lucent/$signature.pid.log"); - } } diff --git a/src/LucentServiceProvider.php b/src/LucentServiceProvider.php index 39f7bc8..510bbff 100644 --- a/src/LucentServiceProvider.php +++ b/src/LucentServiceProvider.php @@ -33,8 +33,11 @@ class LucentServiceProvider extends ServiceProvider return new ImageManager(['driver' => 'imagick']); }); + + $this->app->bind(DatabaseGraph::class, function () { - return match (config("lucent.database")) { + $dbConnection = config("lucent.database"); + return match (config("database.connections.$dbConnection.driver")) { "sqlite" => new SqliteDatabaseGraph(), "pgsql" => new PgsqlDatabaseGraph(), }; diff --git a/src/StaticGenerator/StaticGenerator.php b/src/StaticGenerator/StaticGenerator.php index 33fb73a..e15bf89 100644 --- a/src/StaticGenerator/StaticGenerator.php +++ b/src/StaticGenerator/StaticGenerator.php @@ -3,51 +3,51 @@ use Carbon\Carbon; use Illuminate\Contracts\Filesystem\Filesystem; -use Illuminate\Contracts\View\View; -use Illuminate\Support\Facades\File; -use Illuminate\Support\Facades\Storage; +use Lucent\Command\CommandService; use Throwable; class StaticGenerator { public function __construct( - public Writer $writer, - public Filesystem $filesystem + public Filesystem $filesystem, + public CommandService $commandService, ) { } - public function run(callable $callback): void + public function run(string $signature, callable $callback): void { - - echo "Start ".Carbon::now()->format("Y-m-d H:i:s").PHP_EOL; + $logWriter = $this->commandService->logWriter($signature); + $logWriter("Start " . Carbon::now()->format("Y-m-d H:i:s")); $this->removeBuildDirectory(); - echo "Removing previous data".PHP_EOL; + $logWriter("Removing previous data"); try { - $callback($this->writer); - }catch (Throwable $th){ - echo "Finished with errors".Carbon::now()->format("Y-m-d H:i:s")." ".$th->getMessage().PHP_EOL; + $callback(new Writer($logWriter)); + } catch (Throwable $th) { + $logWriter("Finished with errors" . Carbon::now()->format("Y-m-d H:i:s") . " " . $th->getMessage()); } $this->copyBuildDirectory(); - echo "Finished ".Carbon::now()->format("Y-m-d H:i:s").PHP_EOL; + $logWriter("Finished " . Carbon::now()->format("Y-m-d H:i:s")); } - private function removeBuildDirectory() :void{ - if(!file_exists(storage_path("lucent/build"))){ + private function removeBuildDirectory(): void + { + if (!file_exists(storage_path("lucent/build"))) { return; } - $cmd = "rm -rf ".storage_path("lucent/build"); + $cmd = "rm -rf " . storage_path("lucent/build"); exec($cmd); } - private function copyBuildDirectory() :void{ - if(file_exists(storage_path("lucent/live"))){ - $cmd = "rm -rf ".storage_path("lucent/live"); + private function copyBuildDirectory(): void + { + if (file_exists(storage_path("lucent/live"))) { + $cmd = "rm -rf " . storage_path("lucent/live"); exec($cmd); } - $cmd = sprintf("cp -R %s %s",storage_path("lucent/build"),storage_path("lucent/live")); + $cmd = sprintf("cp -R %s %s", storage_path("lucent/build"), storage_path("lucent/live")); exec($cmd); } diff --git a/src/StaticGenerator/Writer.php b/src/StaticGenerator/Writer.php index ecf4e08..832b12e 100644 --- a/src/StaticGenerator/Writer.php +++ b/src/StaticGenerator/Writer.php @@ -1,13 +1,10 @@ logWriter; + $logWriter("Path: $path"); }