<commit_before><?php

namespace Markup\JobQueueBundle\Service;

use Psr\Log\LoggerInterface;
use Symfony\Component\Finder\Finder;

class SupervisordConfigFileWriter
{
    /**
     * @todo move to configuration?
     * The name of this file must be present in the app/config location
     */
    const DEFAULT_CONFIG_FILE = 'supervisord_queues.conf.dist';

    private $supervisordUser;
    private $supervisordConfigPath;

    public function __construct(
        LoggerInterface $logger,
        JobManager $jobby,
        $kernelPath,
        $resquePrefix,
        $redisHost,
        $redisPort,
        $redisDB
    ) {
        $this->logger = $logger;
        $this->jobby = $jobby;
        $this->kernelPath = $kernelPath;
        $this->resquePrefix = $resquePrefix;
        $this->redisHost = $redisHost;
        $this->redisPort = $redisPort;
        $this->redisDB = $redisDB;
    }

    /**
     * The user who will execute the supervisord queue
     */
    public function setSupervisordUser($supervisordUser)
    {
        $this->supervisordUser = $supervisordUser;
    }

    /**
     * Sets the path to write the configuration file to
     */
    public function setSupervisordConfigPath($supervisordConfigPath)
    {
        $this->supervisordConfigPath = $supervisordConfigPath;
    }

    /**
     * Writes the supervisord config file
     */
    public function writeConfig($uniqueEnvironment, $server)
    {
        if (!$this->supervisordUser || !$this->supervisordConfigPath) {
            throw new \Exception('You must configure the supervisord config writer before writing a configuration file');
        }

        $supervisorUser = $this->supervisordUser;
        $supervisordConfigPath = $this->supervisordConfigPath;
        $defaultConfigFile = self::DEFAULT_CONFIG_FILE;
        $phpBin = 'php';
        $kernelPath = realpath($this->kernelPath);
        $absoluteReleasePath = realpath($kernelPath . '/..');

        $logger = $this->logger;

        $resquePrefix = $this->resquePrefix;
        $redisHost = $this->redisHost;
        $redisPort = $this->redisPort;
        $redisDB = $this->redisDB;

        $supervisordConfigFilePath = sprintf('%s/queue_%s_%s_%s_generated.conf', $supervisordConfigPath, $resquePrefix, $uniqueEnvironment, $server);

        $logger->info(sprintf('writing queue configuration to %s, clearing previous file', $supervisordConfigFilePath));
        file_put_contents($supervisordConfigFilePath, '');

        //copy the default supervisord_queues.conf.dist file to the destination path
        // $finder = new Finder();
        // $finder->name($defaultConfigFile)->in($kernelPath)->path('config')->depth(1);
        // $results = iterator_to_array($finder);
        // if (count($results) == 0) {
        //     throw new \Exception(sprintf('Default config file %s not present in project, cannot write config file', $defaultConfigFile));
        // }
        // $file = current($results);

        // copy($file->getRealPath(), $supervisordConfigFilePath);
        // write the default sceduled worker config:

        $conf = [];
        $conf[] = sprintf("[program:markup_job_queue_%s_%s_%s_scheduled]", $resquePrefix, $uniqueEnvironment, $server);
        $conf[] = sprintf("command=%s %s/vendor/bcc/resque-bundle/BCC/ResqueBundle/bin/resque-scheduler", $phpBin, $absoluteReleasePath);
        $conf[] = sprintf("user=%s", $supervisorUser);
        $conf[] = "autostart=false";
        $conf[] = "autorestart=true";
        $conf[] = sprintf("directory=%s", $absoluteReleasePath);
        $conf[] = "stopsignal=QUIT";
        $conf[] = sprintf("stderr_logfile=%s/logs/resque-scheduledworker.stderror.log", $kernelPath);
        $conf[] = sprintf("stdout_logfile=%s/logs/resque-scheduledworker.stdout.log", $kernelPath);

        $envConfig = sprintf(
            "environment = APP_INCLUDE='%s/vendor/autoload.php',VERBOSE='1',PREFIX='%s',REDIS_BACKEND='%s:%s',REDIS_BACKEND_DB='%s',RESQUE_PHP='%s/vendor/chrisboulton/php-resque/lib/Resque.php'",
            $absoluteReleasePath,
            $resquePrefix,
            $redisHost,
            $redisPort,
            $redisDB,
            $absoluteReleasePath
        );
        $conf[] = $envConfig;
        file_put_contents($supervisordConfigFilePath, implode("\n", $conf), FILE_APPEND);

        //stream "#{try_sudo} chmod 777 #{supervisor_config_file_path}"
        $logger->info('amending supervisor queue config');

        $queues = $this->jobby->getQueueConfigurations($server);

        // write a configuration entry for each queue
        $queueGroups = [];
        foreach ($queues as $queue) {
            $queueName = sprintf('%s-%s', $queue->getName(), $server);
            $queueGroup = sprintf("markup_job_queue_%s_%s_%s", $resquePrefix, $uniqueEnvironment, $queue->getName());
            $queueGroups[] = $queueGroup;
            $conf = [];
            $conf[] = "\n";
            $conf[] = sprintf("[program:%s]", $queueGroup);
            $conf[] = sprintf("command=%s %s/vendor/bcc/resque-bundle/BCC/ResqueBundle/bin/resque", $phpBin, $absoluteReleasePath);
            $conf[] = sprintf("user=%s", $supervisorUser);
            $conf[] = "autostart=false";
            $conf[] = "autorestart=true";
            $conf[] = sprintf("directory=%s", $absoluteReleasePath);
            $conf[] = "stopsignal=QUIT";
            $conf[] = sprintf("stderr_logfile=%s/logs/resque-worker.error.log", $kernelPath);
            $conf[] = sprintf("stdout_logfile=%s/logs/resque-worker.out.log", $kernelPath);
            $envConfig = sprintf(
                "environment = APP_INCLUDE='%s/vendor/autoload.php',VERBOSE='1',QUEUE='%s',PREFIX='%s',COUNT='%s',INTERVAL='20',REDIS_BACKEND='%s:%s',REDIS_BACKEND_DB='%s'",
                $absoluteReleasePath,<commit_msg>Update SupervisordConfigFileWriter.php<commit_after><?php

namespace Markup\JobQueueBundle\Service;

use Psr\Log\LoggerInterface;
use Symfony\Component\Finder\Finder;

class SupervisordConfigFileWriter
{
    /**
     * @todo move to configuration?
     * The name of this file must be present in the app/config location
     */
    const DEFAULT_CONFIG_FILE = 'supervisord_queues.conf.dist';

    private $supervisordUser;
    private $supervisordConfigPath;

    public function __construct(
        LoggerInterface $logger,
        JobManager $jobby,
        $kernelPath,
        $resquePrefix,
        $redisHost,
        $redisPort,
        $redisDB
    ) {
        $this->logger = $logger;
        $this->jobby = $jobby;
        $this->kernelPath = $kernelPath;
        $this->resquePrefix = $resquePrefix;
        $this->redisHost = $redisHost;
        $this->redisPort = $redisPort;
        $this->redisDB = $redisDB;
    }

    /**
     * The user who will execute the supervisord queue
     */
    public function setSupervisordUser($supervisordUser)
    {
        $this->supervisordUser = $supervisordUser;
    }

    /**
     * Sets the path to write the configuration file to
     */
    public function setSupervisordConfigPath($supervisordConfigPath)
    {
        $this->supervisordConfigPath = $supervisordConfigPath;
    }

    /**
     * Writes the supervisord config file
     */
    public function writeConfig($uniqueEnvironment, $server)
    {
        if (!$this->supervisordUser || !$this->supervisordConfigPath) {
            throw new \Exception('You must configure the supervisord config writer before writing a configuration file');
        }

        $supervisorUser = $this->supervisordUser;
        $supervisordConfigPath = $this->supervisordConfigPath;
        $defaultConfigFile = self::DEFAULT_CONFIG_FILE;
        $phpBin = 'php';
        $kernelPath = realpath($this->kernelPath);
        $absoluteReleasePath = realpath($kernelPath . '/..');

        $logger = $this->logger;

        $resquePrefix = $this->resquePrefix;
        $redisHost = $this->redisHost;
        $redisPort = $this->redisPort;
        $redisDB = $this->redisDB;

        $supervisordConfigFilePath = sprintf('%s/queue_%s_%s_%s_generated.conf', $supervisordConfigPath, $resquePrefix, $uniqueEnvironment, $server);

        $logger->info(sprintf('writing queue configuration to %s, clearing previous file', $supervisordConfigFilePath));
        file_put_contents($supervisordConfigFilePath, '');

        //copy the default supervisord_queues.conf.dist file to the destination path
        // $finder = new Finder();
        // $finder->name($defaultConfigFile)->in($kernelPath)->path('config')->depth(1);
        // $results = iterator_to_array($finder);
        // if (count($results) == 0) {
        //     throw new \Exception(sprintf('Default config file %s not present in project, cannot write config file', $defaultConfigFile));
        // }
        // $file = current($results);

        // copy($file->getRealPath(), $supervisordConfigFilePath);
        // write the default sceduled worker config:

        $conf = [];
        $conf[] = sprintf("[program:markup_job_queue_%s_%s_%s_scheduled]", $resquePrefix, $uniqueEnvironment, $server);
        $conf[] = sprintf("command=%s %s/vendor/bcc/resque-bundle/BCC/ResqueBundle/bin/resque-scheduler", $phpBin, $absoluteReleasePath);
        $conf[] = sprintf("user=%s", $supervisorUser);
        $conf[] = "autostart=false";
        $conf[] = "autorestart=true";
        $conf[] = sprintf("directory=%s", $absoluteReleasePath);
        $conf[] = "stopsignal=QUIT";
        $conf[] = sprintf("stderr_logfile=%s/logs/resque-scheduledworker.stderror.log", $kernelPath);
        $conf[] = sprintf("stdout_logfile=%s/logs/resque-scheduledworker.stdout.log", $kernelPath);

        $envConfig = sprintf(
            "environment = APP_INCLUDE='%s/vendor/autoload.php',VERBOSE='1',PREFIX='%s',REDIS_BACKEND='%s:%s',REDIS_BACKEND_DB='%s',RESQUE_PHP='%s/vendor/chrisboulton/php-resque/lib/Resque.php'",
            $absoluteReleasePath,
            $resquePrefix,
            $redisHost,
            $redisPort,
            $redisDB,
            $absoluteReleasePath
        );
        $conf[] = $envConfig;
        file_put_contents($supervisordConfigFilePath, implode("\n", $conf), FILE_APPEND);

        //stream "#{try_sudo} chmod 777 #{supervisor_config_file_path}"
        $logger->info('amending supervisor queue config');

        $queues = $this->jobby->getQueueConfigurations($server);

        // write a configuration entry for each queue
        $queueGroups = [];
        foreach ($queues as $queue) {
            $queueName = sprintf('%s-%s', $queue->getName(), $server);
            $queueGroup = sprintf("markup_job_queue_%s_%s_%s", $resquePrefix, $uniqueEnvironment, $queueName);
            $queueGroups[] = $queueGroup;
            $conf = [];
            $conf[] = "\n";
            $conf[] = sprintf("[program:%s]", $queueGroup);
            $conf[] = sprintf("command=%s %s/vendor/bcc/resque-bundle/BCC/ResqueBundle/bin/resque", $phpBin, $absoluteReleasePath);
            $conf[] = sprintf("user=%s", $supervisorUser);
            $conf[] = "autostart=false";
            $conf[] = "autorestart=true";
            $conf[] = sprintf("directory=%s", $absoluteReleasePath);
            $conf[] = "stopsignal=QUIT";
            $conf[] = sprintf("stderr_logfile=%s/logs/resque-worker.error.log", $kernelPath);
            $conf[] = sprintf("stdout_logfile=%s/logs/resque-worker.out.log", $kernelPath);
            $envConfig = sprintf(
                "environment = APP_INCLUDE='%s/vendor/autoload.php',VERBOSE='1',QUEUE='%s',PREFIX='%s',COUNT='%s',INTERVAL='20',REDIS_BACKEND='%s:%s',REDIS_BACKEND_DB='%s'",
                $absoluteReleasePath,
{'fraction_non_alphanumeric': 0.14741824440619622, 'fraction_numerical': 0.001549053356282272, 'mean_word_length': 2.9075319435104237, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2750843', 'n_tokens_mistral': 3532, 'n_tokens_neox': 3521, 'n_words': 612}