assegaiphp/beanstalkd

Beanstalkd queue integration for AssegaiPHP framework, enabling job production and consumption using the Pheanstalk library.

1.0.0 2025-07-15 22:17 UTC

This package is auto-updated.

Last update: 2025-07-15 22:25:26 UTC


README

Assegai Logo

A progressive PHP framework for building efficient and scalable web applications.

AssegaiPHP Beanstalkd Queue Integration

License AssegaiPHP

This package adds Beanstalkd queue support to the AssegaiPHP framework using the Pheanstalk PHP client.

📦 Installation

Install via Composer:

composer require assegaiphp/beanstalkd

Or use the Assegai CLI:

assegai add beanstalkd

⚙️ Configuration

Add a Beanstalk driver and connection to your config/queues.php file:

<?php

return [
  'drivers' => [
    'beanstalk' => Assegai\Beanstalkd\BeanstalkdQueue::class,
  ],
  'connections' => [
    'beanstalk' => [
      'notifications' => [
        'host' => 'localhost',
        'port' => 11300,
        'connection_timeout' => 10,
        'receive_timeout' => 10,
      ],
    ],
  ],
];

💡 The format is: 'driverName.queueName', e.g., 'beanstalk.notifications'.

✨ Usage

Producing Jobs

Inject a queue instance in your service using #[InjectQueue]:

use Assegai\Core\Queues\Attributes\InjectQueue;
use Assegai\Core\Queues\Interfaces\QueueInterface;

readonly class NotificationsService
{
  public function __construct(
    #[InjectQueue('beanstalk.notifications')] private QueueInterface $queue
  ) {}

  public function send(array $payload): void
  {
    $this->queue->add($payload);
  }
}

Consuming Jobs

Create a queue consumer class with #[Processor] and extend WorkerHost:

use Assegai\Core\Queues\Attributes\Processor;
use Assegai\Core\Queues\WorkerHost;
use Assegai\Core\Queues\QueueProcessResult;
use Assegai\Core\Queues\Interfaces\QueueProcessResultInterface;

#[Processor('beanstalk.notifications')]
class NotificationsConsumer extends WorkerHost
{
  public function process(callable $callback): QueueProcessResultInterface
  {
    $job = $callback();
    $data = $job->data;

    echo "Dispatching notification: {$data->message}" . PHP_EOL;

    return new QueueProcessResult(data: ['status' => 'sent'], job: $job);
  }
}

⚠️ Do not use #[Injectable] on consumers. The process() method must accept a callable and return a QueueProcessResultInterface.

Running the Worker

Start the queue worker using:

assegai queue:work

This will continuously listen for jobs from the configured Beanstalk tube.

🧪 Testing

You can simulate jobs by calling the service from a controller or CLI command and watch the consumer terminal for output.

📚 Resources

Support

Assegai is an MIT-licensed open source project. It can grow thanks to sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Assegai is MIT licensed.