master

laravel/framework

Last updated at: 29/12/2023 09:23

BeanstalkdConnector.php

TLDR

The BeanstalkdConnector.php file is a part of the Illuminate\Queue\Connectors namespace in the Demo Projects project. It contains a class BeanstalkdConnector that implements the ConnectorInterface. This class is responsible for establishing a connection to the Beanstalkd queue and creating a Pheanstalk instance.

Methods

connect

This method establishes a queue connection by creating a new instance of BeanstalkdQueue and passing the required parameters such as the Pheanstalk instance, queue name, retry after time, block for time, and after commit callback (if provided).

pheanstalk

This protected method creates a new instance of Pheanstalk by passing the host, port, timeout, and socket factory interface.

<?php

namespace Illuminate\Queue\Connectors;

use Illuminate\Queue\BeanstalkdQueue;
use Pheanstalk\Contract\SocketFactoryInterface;
use Pheanstalk\Pheanstalk;
use Pheanstalk\Values\Timeout;

class BeanstalkdConnector implements ConnectorInterface
{
    /**
     * Establish a queue connection.
     *
     * @param  array  $config
     * @return \Illuminate\Contracts\Queue\Queue
     */
    public function connect(array $config)
    {
        return new BeanstalkdQueue(
            $this->pheanstalk($config),
            $config['queue'],
            $config['retry_after'] ?? Pheanstalk::DEFAULT_TTR,
            $config['block_for'] ?? 0,
            $config['after_commit'] ?? null
        );
    }

    /**
     * Create a Pheanstalk instance.
     *
     * @param  array  $config
     * @return \Pheanstalk\Pheanstalk
     */
    protected function pheanstalk(array $config)
    {
        return Pheanstalk::create(
            $config['host'],
            $config['port'] ?? SocketFactoryInterface::DEFAULT_PORT,
            isset($config['timeout']) ? new Timeout($config['timeout']) : null,
        );
    }
}