<commit_before><?php

declare(strict_types=1);

namespace Enqueue\Gearman;

use Interop\Queue\Consumer;
use Interop\Queue\Message;
use Interop\Queue\Queue;

class GearmanConsumer implements Consumer
{
    /**
     * @var \GearmanWorker
     */
    private $worker;

    /**
     * @var GearmanDestination
     */
    private $destination;

    /**
     * @var GearmanContext
     */
    private $context;

    public function __construct(GearmanContext $context, GearmanDestination $destination)
    {
        $this->context = $context;
        $this->destination = $destination;

        $this->worker = $context->createWorker();
    }

    /**
     * @return GearmanDestination
     */
    public function getQueue(): Queue
    {
        return $this->destination;
    }

    /**
     * @return GearmanMessage
     */
    public function receive(int $timeout = 0): ?Message
    {
        set_error_handler(function ($severity, $message, $file, $line) {
            throw new \ErrorException($message, 0, $severity, $file, $line);
        });

        $this->worker->setTimeout($timeout);

        try {
            $message = null;

            $this->worker->addFunction($this->destination->getName(), function (\GearmanJob $job) use (&$message) {
                $message = GearmanMessage::jsonUnserialize($job->workload());
            });

            $this->worker->work();
        } finally {
            restore_error_handler();
        }

        return $message;
    }

    /**
     * @return GearmanMessage
     */
    public function receiveNoWait(): ?Message
    {
        return $this->receive(100);
    }

    /**
     * @param GearmanMessage $message
     */
    public function acknowledge(Message $message): void
    {
    }

    /**
     * @param GearmanMessage $message
     */
    public function reject(Message $message, bool $requeue = false): void
    {
        if ($requeue) {
            $this->context->createProducer()->send($this->destination, $message);
        }
    }

    public function getWorker(): \GearmanWorker
    {
        return $this->worker;
    }
}
<commit_msg>register worker callback only once, move to constructor<commit_after><?php

declare(strict_types=1);

namespace Enqueue\Gearman;

use Interop\Queue\Consumer;
use Interop\Queue\Message;
use Interop\Queue\Queue;

class GearmanConsumer implements Consumer
{
    /**
     * @var \GearmanWorker
     */
    private $worker;

    /**
     * @var GearmanDestination
     */
    private $destination;

    /**
     * @var GearmanContext
     */
    private $context;

    /**
     * Message content.
     *
     * @var mixed
     */
    private $message;

    public function __construct(GearmanContext $context, GearmanDestination $destination)
    {
        $this->context = $context;
        $this->destination = $destination;

        $this->worker = $context->createWorker();

        $this->worker->addFunction($this->destination->getName(), function (\GearmanJob $job) {
            $this->message = GearmanMessage::jsonUnserialize($job->workload());
        });
    }

    /**
     * @return GearmanDestination
     */
    public function getQueue(): Queue
    {
        return $this->destination;
    }

    /**
     * @return GearmanMessage
     */
    public function receive(int $timeout = 0): ?Message
    {
        set_error_handler(function ($severity, $message, $file, $line) {
            throw new \ErrorException($message, 0, $severity, $file, $line);
        });

        $this->worker->setTimeout($timeout);

        try {
            $this->message = null;

            $this->worker->work();
        } finally {
            restore_error_handler();
        }

        return $this->message;
    }

    /**
     * @return GearmanMessage
     */
    public function receiveNoWait(): ?Message
    {
        return $this->receive(100);
    }

    /**
     * @param GearmanMessage $message
     */
    public function acknowledge(Message $message): void
    {
    }

    /**
     * @param GearmanMessage $message
     */
    public function reject(Message $message, bool $requeue = false): void
    {
        if ($requeue) {
            $this->context->createProducer()->send($this->destination, $message);
        }
    }

    public function getWorker(): \GearmanWorker
    {
        return $this->worker;
    }
}

{'fraction_non_alphanumeric': 0.13504155124653738, 'fraction_numerical': 0.002770083102493075, 'mean_word_length': 2.135311143270622, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 46, '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': '2467135', 'n_tokens_mistral': 1279, 'n_tokens_neox': 1216, 'n_words': 251}