Viewing File: /usr/local/cpanel/whostmgr/docroot/cgi/ncssl/source/src/Command/RunBackgroundHandlersCommand.php

<?php

namespace App\Command;

use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Process\Process;

#[AsCommand(
    name: 'app:run-background-handlers',
    description: 'Execute queued handlers',
)]
class RunBackgroundHandlersCommand extends Command
{
    public function __construct(
        private readonly LoggerInterface $runBackgroundHandlerLogger,
        #[Autowire(param: 'rpcHandler.backgroundJobsPath')]
        private readonly string $backgroundJobsPath,
    )
    {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->addArgument('dataFilePath', InputArgument::OPTIONAL, 'Path to jobs json file', $this->backgroundJobsPath)
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $this->runBackgroundHandlerLogger->debug('Command app:run-background-handlers was started!');

        $dataFilePath = $input->getArgument('dataFilePath');
        if (file_exists($dataFilePath)) {
            $dataFilePathTemp = implode('.', [$dataFilePath, uniqid('', true)]);
            rename($dataFilePath, $dataFilePathTemp);

            $lines = file($dataFilePathTemp, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
            foreach ($lines as $jsonData) {
                $this->runCommand($jsonData);
            }
            unlink($dataFilePathTemp);

            return Command::SUCCESS;
        }

        return Command::INVALID;
    }

    /**
     * @param string $jsonData handler arguments as json string
     */
    private function runCommand(string $jsonData): void
    {
        $consoleCommand = $this->getConsoleCommand($jsonData);

        $process = new Process($consoleCommand);
        $process->run();
        if (!$process->isSuccessful()) {
            $this->runBackgroundHandlerLogger->error('Error occurred while processing background command: ' . $jsonData, [
                'outputError' => $process->getErrorOutput(),
                'requestData' => $jsonData
            ]);
        }
    }

    /**
     * @param string $jsonData
     *
     * @return array
     */
    private function getConsoleCommand(string $jsonData): array
    {
        $b64data = base64_encode(trim($jsonData));
        $executablePath = $this->getExecutablePath();
        return [PHP_BINARY, $executablePath, 'app:rpc-handler', $b64data];
    }

    /**
     * @return string
     */
    private function getExecutablePath(): string
    {
        $trace = debug_backtrace();
        return end($trace)['file'];
    }
}
Back to Directory File Manager