Files
lucent-laravel/src/Http/Controller/BuildController.php
T

106 lines
3.1 KiB
PHP
Raw Normal View History

2023-10-04 23:48:12 +03:00
<?php
namespace Lucent\Http\Controller;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Lucent\Channel\ChannelService;
use Lucent\Svelte\Svelte;
class BuildController extends Controller
{
public function __construct(
public readonly Svelte $svelte,
public readonly ChannelService $channelService,
)
{
}
2024-08-24 18:51:36 +03:00
public function build(Request $request)
2023-10-04 23:48:12 +03:00
{
2024-08-24 18:51:36 +03:00
$commandSignature = $request->route("signature");
$buildLogFile = $this->getLogFile($commandSignature);
$pidFile = $this->getPidFile($commandSignature);
if (file_exists($buildLogFile)) {
2023-10-05 13:03:17 +03:00
unlink($buildLogFile);
}
2024-08-24 18:51:36 +03:00
if (file_exists($pidFile)) {
unlink($pidFile);
}
2023-10-05 13:03:17 +03:00
2024-08-24 18:51:36 +03:00
exec("cd " . base_path() . " && php8.3 artisan {$commandSignature} > " . $buildLogFile . " 2>&1 & echo $!", $op);
2023-10-04 23:48:12 +03:00
$pid = (int)$op[0];
2024-08-24 18:51:36 +03:00
file_put_contents($pidFile, $pid);
return redirect($this->channelService->channel->lucentUrl . "/command-report/" . $commandSignature);
2023-10-04 23:48:12 +03:00
}
2024-08-24 18:51:36 +03:00
public function report(Request $request): View
2023-10-04 23:48:12 +03:00
{
2024-08-24 18:51:36 +03:00
$commandSignature = $request->route("signature");
$command = $this->channelService->channel->commands->firstWhere("signature", $commandSignature);
2023-10-04 23:48:12 +03:00
return $this->svelte->render(
layout: "channel",
view: "buildReport",
2024-08-24 18:51:36 +03:00
title: $command->name,
data: [
"command" => $command,
]
2023-10-04 23:48:12 +03:00
);
}
2024-08-24 18:51:36 +03:00
public function reportSource(Request $request)
2023-10-04 23:48:12 +03:00
{
2024-08-24 18:51:36 +03:00
$commandSignature = $request->route("signature");
return response()->stream(function () use ($commandSignature) {
2023-10-04 23:48:12 +03:00
while (true) {
2023-10-05 13:03:17 +03:00
$data["date"] = date("Y-m-d H:i:s");
2024-08-24 18:51:36 +03:00
$data["logs"] = file_get_contents($this->getLogFile($commandSignature));
2023-10-16 18:30:00 +03:00
// $lines = explode("\n",$data["logs"]);
2023-10-05 13:03:17 +03:00
2024-08-24 18:51:36 +03:00
echo 'data: ' . json_encode($data);
2023-10-04 23:48:12 +03:00
echo "\n\n";
ob_flush();
flush();
2024-08-24 18:51:36 +03:00
$pidFile = $this->getPidFile($commandSignature);
if (file_exists($pidFile)) {
$pid = file_get_contents($pidFile);
} else {
break;
}
if (empty($pid)) {
2023-10-05 13:03:17 +03:00
break;
}
2024-08-24 18:51:36 +03:00
if (!file_exists("/proc/$pid")) {
2023-11-17 20:21:45 +02:00
break;
}
2023-10-04 23:48:12 +03:00
// Break the loop if the client aborted the connection (closed the page)
2023-10-05 13:03:17 +03:00
if (connection_aborted()) {
break;
}
sleep(1); // 50ms
2023-10-04 23:48:12 +03:00
}
}, 200, [
'Cache-Control' => 'no-cache',
'X-Accel-Buffering' => 'no',
'Content-Type' => 'text/event-stream',
]);
}
2024-08-24 18:51:36 +03:00
private function getLogFile(string $signature): string
{
return storage_path("lucent/$signature.log");
}
2023-10-04 23:48:12 +03:00
2024-08-24 18:51:36 +03:00
private function getPidFile(string $signature): string
{
return storage_path("lucent/$signature.pid.log");
}
2023-10-04 23:48:12 +03:00
}