<commit_before><?php namespace ShinyDeploy\Core;

use Noodlehaus\Config;

/**
 * Class WorkerManager
 *
 * Handles worker control stuff like startup, restart, eg.
 *
 */
class WorkerManager
{
    /**@var string $gearmanHost */
    protected $gearmanHost;

    /** @var int $gearmanPort */
    protected $gearmanPort;

    /** @var string $workerPath  */
    protected $workerPath;

    /** @var string $logPath */
    protected $logPath;

    /** @var string $pidPath */
    protected $pidPath;

    /** @var array $startupConfig */
    protected $startupConfig = [];

    /** @var string $processIdentifier */
    protected $processIdentifier;

    /** @var int $timeTillGhost */
    protected $timeTillGhost;

    /** @var array $pids Holds worker process ids. */
    protected $pids = [];

    public function __construct(Config $config)
    {
        $this->gearmanHost = $config->get('gearman.host');
        $this->gearmanPort = $config->get('gearman.port');
        $this->workerPath = $config->get('gearman.workerPath');
        $this->logPath = $config->get('gearman.logPath');
        $this->pidPath = $config->get('gearman.pidPath');
        $this->startupConfig = $config->get('gearman.workerScripts');
        $this->processIdentifier = $config->get(('gearman.processIdentifier'));
        $this->timeTillGhost = $config->get(('gearman.timeTillGhost'));

        $this->loadPids();
    }

    /**
     * Startup workers.
     *
     * @param string $typeFilter If given only workers of this type will be started.
     * @return bool
     */
    public function start($typeFilter = '')
    {
        foreach ($this->startupConfig as $workerType => $workerConfig) {
            // don't start workers of different type if filter is set
            if (!empty($typeFilter) && $typeFilter !== $workerType) {
                continue;
            }

            // don't start new workers if already running:
            if (!empty($this->pids[$workerType])) {
                continue;
            }

            // startup the workers:
            for ($i = 0; $i < $workerConfig['instances']; $i++) {
                $this->startupWorker($workerType);
            }
        }
        return true;
    }

    /**
     * Stop workers.
     *
     * @param string $typeFilter If given only workers of this type will be started.
     * @return bool
     */
    public function stop($typeFilter = '')
    {
        foreach ($this->startupConfig as $workerType => $workerConfig) {
            // don't stop workers of different type if filter is set
            if (!empty($typeFilter) && $typeFilter !== $workerType) {
                continue;
            }

            // skip if no worker running:
            if (empty($this->pids[$workerType])) {
                continue;
            }

            // stop the workers:
            foreach ($this->pids[$workerType] as $pid) {
                exec(escapeshellcmd('kill ' . $pid));
            }
        }
        $this->reloadPids();
        return true;
    }

    /**
     * Restart workers.
     *
     * @param string $workerId If given only workers of this type will be started.
     * @return bool<commit_msg>Cleanup PID files when starting/stopping workers<commit_after><?php namespace ShinyDeploy\Core;

use Noodlehaus\Config;

/**
 * Class WorkerManager
 *
 * Handles worker control stuff like startup, restart, eg.
 *
 */
class WorkerManager
{
    /**@var string $gearmanHost */
    protected $gearmanHost;

    /** @var int $gearmanPort */
    protected $gearmanPort;

    /** @var string $workerPath  */
    protected $workerPath;

    /** @var string $logPath */
    protected $logPath;

    /** @var string $pidPath */
    protected $pidPath;

    /** @var array $startupConfig */
    protected $startupConfig = [];

    /** @var string $processIdentifier */
    protected $processIdentifier;

    /** @var int $timeTillGhost */
    protected $timeTillGhost;

    /** @var array $pids Holds worker process ids. */
    protected $pids = [];

    public function __construct(Config $config)
    {
        $this->gearmanHost = $config->get('gearman.host');
        $this->gearmanPort = $config->get('gearman.port');
        $this->workerPath = $config->get('gearman.workerPath');
        $this->logPath = $config->get('gearman.logPath');
        $this->pidPath = $config->get('gearman.pidPath');
        $this->startupConfig = $config->get('gearman.workerScripts');
        $this->processIdentifier = $config->get(('gearman.processIdentifier'));
        $this->timeTillGhost = $config->get(('gearman.timeTillGhost'));

        $this->loadPids();
    }

    /**
     * Startup workers.
     *
     * @param string $typeFilter If given only workers of this type will be started.
     * @return bool
     */
    public function start($typeFilter = '')
    {
        $this->pidCleanup();
        foreach ($this->startupConfig as $workerType => $workerConfig) {
            // don't start workers of different type if filter is set
            if (!empty($typeFilter) && $typeFilter !== $workerType) {
                continue;
            }

            // don't start new workers if already running:
            if (!empty($this->pids[$workerType])) {
                continue;
            }

            // startup the workers:
            for ($i = 0; $i < $workerConfig['instances']; $i++) {
                $this->startupWorker($workerType);
            }
        }
        return true;
    }

    /**
     * Stop workers.
     *
     * @param string $typeFilter If given only workers of this type will be started.
     * @return bool
     */
    public function stop($typeFilter = '')
    {
        foreach ($this->startupConfig as $workerType => $workerConfig) {
            // don't stop workers of different type if filter is set
            if (!empty($typeFilter) && $typeFilter !== $workerType) {
                continue;
            }

            // skip if no worker running:
            if (empty($this->pids[$workerType])) {
                continue;
            }

            // stop the workers:
            foreach ($this->pids[$workerType] as $pid) {
                exec(escapeshellcmd('kill ' . $pid));
            }
        }
        $this->reloadPids();
        $this->pidCleanup();
        return true;
    }

    /**
     * Restart workers.
     *
     * @param string $workerId If given only workers of this type will be started.
     * @return bool
{'fraction_non_alphanumeric': 0.13785082958598233, 'fraction_numerical': 0.0003101256008683517, 'mean_word_length': 2.0182498830135702, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 57, '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': '5885601', 'n_tokens_mistral': 1859, 'n_tokens_neox': 1776, 'n_words': 458}