multiple commands

This commit is contained in:
2024-08-24 18:51:36 +03:00
parent d9e2c4954a
commit 1505aaa909
10 changed files with 100 additions and 50 deletions
+40 -18
View File
@@ -5,11 +5,8 @@ namespace Lucent\Http\Controller;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Artisan;
use Lucent\Channel\ChannelService;
use Lucent\Svelte\Svelte;
use function Lucent\Response\ok;
class BuildController extends Controller
{
@@ -20,49 +17,66 @@ class BuildController extends Controller
{
}
public function build()
public function build(Request $request)
{
$buildLogFile = storage_path("lucent/build.log");
if(file_exists($buildLogFile)){
$commandSignature = $request->route("signature");
$buildLogFile = $this->getLogFile($commandSignature);
$pidFile = $this->getPidFile($commandSignature);
if (file_exists($buildLogFile)) {
unlink($buildLogFile);
}
if (file_exists($pidFile)) {
unlink($pidFile);
}
exec("cd " . base_path() . " && php8.3 artisan {$this->channelService->channel->generateCommand} > " . $buildLogFile . " 2>&1 & echo $!", $op);
exec("cd " . base_path() . " && php8.3 artisan {$commandSignature} > " . $buildLogFile . " 2>&1 & echo $!", $op);
$pid = (int)$op[0];
return redirect($this->channelService->channel->lucentUrl . "/build-report");
file_put_contents($pidFile, $pid);
return redirect($this->channelService->channel->lucentUrl . "/command-report/" . $commandSignature);
}
public function report(): View
public function report(Request $request): View
{
$commandSignature = $request->route("signature");
$command = $this->channelService->channel->commands->firstWhere("signature", $commandSignature);
return $this->svelte->render(
layout: "channel",
view: "buildReport",
title: "Build Report",
title: $command->name,
data: [
"command" => $command,
]
);
}
public function reportSource()
public function reportSource(Request $request)
{
return response()->stream(function () {
$commandSignature = $request->route("signature");
return response()->stream(function () use ($commandSignature) {
while (true) {
// sleep(1); // 50ms
$data["date"] = date("Y-m-d H:i:s");
$data["logs"] = file_get_contents(storage_path("lucent/build.log"));
$data["logs"] = file_get_contents($this->getLogFile($commandSignature));
// $lines = explode("\n",$data["logs"]);
echo 'data: ' .json_encode($data);
echo 'data: ' . json_encode($data);
echo "\n\n";
ob_flush();
flush();
if(str_contains($data["logs"],"Finito")){
$pidFile = $this->getPidFile($commandSignature);
if (file_exists($pidFile)) {
$pid = file_get_contents($pidFile);
} else {
break;
}
if(str_contains($data["logs"],"Exception")){
if (empty($pid)) {
break;
}
if (!file_exists("/proc/$pid")) {
break;
}
@@ -79,5 +93,13 @@ 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");
}
}