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

81 lines
2.4 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;
2024-08-27 17:42:06 +03:00
use Lucent\Command\CommandRepo;
use Lucent\Command\CommandService;
2023-10-04 23:48:12 +03:00
use Lucent\Svelte\Svelte;
class BuildController extends Controller
{
public function __construct(
2024-08-27 17:42:06 +03:00
private readonly Svelte $svelte,
private readonly ChannelService $channelService,
private readonly CommandService $commandService,
private readonly CommandRepo $commandRepo,
2023-10-04 23:48:12 +03:00
)
{
}
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");
2024-08-27 17:42:06 +03:00
$this->commandService->run($commandSignature);
2023-10-05 13:03:17 +03:00
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) {
2024-08-27 17:42:06 +03:00
$commandLogItem = $this->commandRepo->findBySignature($commandSignature);
2023-10-05 13:03:17 +03:00
$data["date"] = date("Y-m-d H:i:s");
2024-08-27 17:42:06 +03:00
$data["logs"] = $commandLogItem->logs ?? "";
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-27 17:42:06 +03:00
logger($this->commandService->commandIsRunning($commandLogItem->pid));
if (!$this->commandService->commandIsRunning($commandLogItem->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',
]);
}
}