commands and logs to the database

This commit is contained in:
2024-08-27 17:42:06 +03:00
parent 74d2fcc4fa
commit ae65ca47f6
16 changed files with 210 additions and 72 deletions
+12 -37
View File
@@ -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");
}
}