#!/usr/bin/env php
<?php

use Bitrix\Main\Loader;
use Bsi\Queue\Queue;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;

set_time_limit(0);

define('NO_KEEP_STATISTIC', true);
define('NOT_CHECK_PERMISSIONS', true);
define('BX_NO_ACCELERATOR_RESET', true);
define('BX_CRONTAB', true);
define('STOP_STATISTICS', true);
define('NO_AGENT_STATISTIC', 'Y');
define('DisableEventsCheck', true);
define('NO_AGENT_CHECK', true);

$_SERVER['DOCUMENT_ROOT'] = realpath(dirname(__DIR__, 4));
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];

require $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/include/prolog_before.php';

@session_destroy();

Loader::includeModule('bsi.queue');

$container = Queue::getInstance()->getContainer();
$application = new Application();
foreach ($container->findTaggedServiceIds('console.command') as $id => $tags) {
    $commandClass = $container->findDefinition($id)->getClass();
    if (!is_subclass_of($commandClass, Command::class)) {
        throw new RuntimeException(sprintf('Invalid command "%s": class "%s" must implement interface "%s".', $id, $commandClass, Command::class));
    }

    /** @var Command $command */
    $command = $container->get($id);
    $application->add($command);
}
$application->run();
