<?php

namespace App\Commands;

use App\Services\Command;
use App\Services\Docker;
use Illuminate\Process\Pool;
use Illuminate\Support\Facades\Process;

class Logs extends Command
{
    protected $signature = 'logs';

    protected $description = 'View the logs of the docker environment';

    public function handle(): void
    {
        $this->info('Attaching to log output');

        $phpLogs = Docker::exec('tail -f /var/log/php/error.log');
        $nginxLogs = Docker::exec('tail -f /var/log/nginx/error.log');

        $pool = Process::pool(function (Pool $pool) use ($phpLogs, $nginxLogs) {
            $pool->forever()->command($phpLogs);
            $pool->forever()->command($nginxLogs);

        })->start();

        $running = $pool->running();
        while ($running->isNotEmpty()) {
            $running->each(function ($process) {
                echo $process->latestOutput();
                echo $process->latestErrorOutput();
            });
        }

    }

}
