<commit_before><?php

namespace SlmQueue\Strategy;

use SlmQueue\Worker\WorkerEvent;
use Zend\EventManager\EventManagerInterface;

class InterruptStrategy extends AbstractStrategy
{
    /**
     * @var bool
     */
    protected $interrupted = false;

    /**
     * @param array $options
     */
    public function __construct(array $options = null)
    {
        parent::__construct($options);

        // Conditional because of lack of pcntl_signal on windows
        if (function_exists('pcntl_signal')) {
            declare(ticks = 1);
            pcntl_signal(SIGTERM, [$this, 'onPCNTLSignal']);
            pcntl_signal(SIGINT, [$this, 'onPCNTLSignal']);
        }
    }

    /**
     * {@inheritDoc}
     */
    public function attach(EventManagerInterface $events, $priority = 1)
    {
        $this->listeners[] = $events->attach(
            WorkerEvent::EVENT_PROCESS_IDLE,
            [$this, 'onStopConditionCheck'],
            $priority
        );
        $this->listeners[] = $events->attach(
            WorkerEvent::EVENT_PROCESS_QUEUE,
            [$this, 'onStopConditionCheck'],
            -1000
        );
        $this->listeners[] = $events->attach(
            WorkerEvent::EVENT_PROCESS_STATE,
            [$this, 'onReportQueueState'],
            $priority
        );
    }

    /**
     * Checks for the stop condition of this strategy
     *
     * @param WorkerEvent $event
     * @return string
     */
    public function onStopConditionCheck(WorkerEvent $event)
    {
        if ($this->interrupted) {
            $event->exitWorkerLoop();

            $this->state = sprintf("interrupt by an external signal on '%s'", $event->getName());
        }
    }

    /**
     * Handle the signal
     *
     * @param int $signo
     */
    public function onPCNTLSignal($signo)
    {
        switch ($signo) {
            case SIGTERM:
            case SIGINT:
                $this->interrupted = true;
                break;
        }<commit_msg>move tick statement to someplace 'within' the worker loop<commit_after><?php

namespace SlmQueue\Strategy;

use SlmQueue\Worker\WorkerEvent;
use Zend\EventManager\EventManagerInterface;

class InterruptStrategy extends AbstractStrategy
{
    /**
     * @var bool
     */
    protected $interrupted = false;

    /**
     * @param array $options
     */
    public function __construct(array $options = null)
    {
        parent::__construct($options);

        // Conditional because of lack of pcntl_signal on windows
        if (function_exists('pcntl_signal')) {
            pcntl_signal(SIGTERM, [$this, 'onPCNTLSignal']);
            pcntl_signal(SIGINT, [$this, 'onPCNTLSignal']);
        }
    }

    /**
     * {@inheritDoc}
     */
    public function attach(EventManagerInterface $events, $priority = 1)
    {
        $this->listeners[] = $events->attach(
            WorkerEvent::EVENT_PROCESS_IDLE,
            [$this, 'onStopConditionCheck'],
            $priority
        );
        $this->listeners[] = $events->attach(
            WorkerEvent::EVENT_PROCESS_QUEUE,
            [$this, 'onStopConditionCheck'],
            -1000
        );
        $this->listeners[] = $events->attach(
            WorkerEvent::EVENT_PROCESS_STATE,
            [$this, 'onReportQueueState'],
            $priority
        );
    }

    /**
     * Checks for the stop condition of this strategy
     *
     * @param WorkerEvent $event
     * @return string
     */
    public function onStopConditionCheck(WorkerEvent $event)
    {
        declare(ticks = 1);

        if ($this->interrupted) {
            $event->exitWorkerLoop();

            $this->state = sprintf("interrupt by an external signal on '%s'", $event->getName());
        }
    }

    /**
     * Handle the signal
     *
     * @param int $signo
     */
    public function onPCNTLSignal($signo)
    {
        switch ($signo) {
            case SIGTERM:
            case SIGINT:
                $this->interrupted = true;
                break;
        }
{'fraction_non_alphanumeric': 0.12003000750187547, 'fraction_numerical': 0.003000750187546887, 'mean_word_length': 1.9368575624082232, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '2345084', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1122, 'n_words': 238}