GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - yiisoft/queue: Framework agnostic message queue

BranchesTags
Open more actions menu

Latest commit

 

History

761 Commits
761 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
config
config
 
 
docs
docs
 
 
runtime
runtime
 
 
src
src
 
 
stubs
stubs
 
 
tests
tests
 
 
.editorconfig
.editorconfig
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
.php-cs-fixer.dist.php
.php-cs-fixer.dist.php
 
 
.phpunit-watcher.yml
.phpunit-watcher.yml
 
 
AGENTS.md
AGENTS.md
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE.md
LICENSE.md
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
composer-dependency-analyser.php
composer-dependency-analyser.php
 
 
composer.json
composer.json
 
 
infection.json.dist
infection.json.dist
 
 
phpbench.json
phpbench.json
 
 
phpunit.xml.dist
phpunit.xml.dist
 
 
psalm.xml
psalm.xml
 
 
rector.php
rector.php
 
 

Repository files navigation

Yii

Yii Queue


Latest Stable Version Total Downloads Build status Code coverage Mutation testing badge Static analysis type-coverage

A framework-agnostic PHP queue library for running tasks asynchronously, best used with Yii3 applications.

Requirements

  • PHP 8.1 - 8.5.
  • PCNTL extension for signal handling (optional, recommended for production use).

Installation

The package could be installed with Composer:

composer require yiisoft/queue

Quick Start

1. Install an adapter

For production use, you should install an adapter package that matches your message broker (AMQP, Kafka, NATS, and others). See the adapter list and follow the adapter-specific documentation for installation and configuration details.

If you don't have an external broker — whether for development, testing, or because you want to design around QueueProducerInterface from day one and add a real broker later — you can run the queue in synchronous mode using SyncQueueProducer instead of AsyncQueueProducer. In this mode messages are processed immediately in the same process, so it won't provide true async execution, but the code stays the same when you switch to a real adapter.

2. Prepare a message and handler

Define a message class for the work to be done — a simple value object with typed properties:

use Yiisoft\Queue\Message\Message;

final class DownloadFileMessage extends Message
{
    public const TYPE = 'download-file';

    public function __construct(
        public readonly string $url,
        public readonly string $destinationPath,
    ) {}

    public static function fromPayload(string $type, bool|int|float|string|array|null $payload): static
    {
        if ($type !== self::TYPE) {
            throw new \InvalidArgumentException("Expected type \"" . self::TYPE . "\", got \"$type\".");
        }
        if (!is_array($payload)
            || !is_string($payload['url'] ?? null)
            || !is_string($payload['destinationPath'] ?? null)
        ) {
            throw new \InvalidArgumentException('Invalid data for ' . self::class . '.');
        }
        return new self($payload['url'], $payload['destinationPath']);
    }

    public function getType(): string
    {
        return self::TYPE;
    }

    public function getPayload(): array
    {
        return ['url' => $this->url, 'destinationPath' => $this->destinationPath];
    }
}

Then create a handler that processes it:

use Yiisoft\Queue\Message\Handler\HandlerInterface;
use Yiisoft\Queue\Message\MessageInterface;

final readonly class RemoteFileHandler implements HandlerInterface
{
    public function __construct(
        private FileDownloader $downloader,
        private FileProcessor $processor,
    ) {}

    public function handle(MessageInterface $message): void
    {
        assert($message instanceof DownloadFileMessage);
        $localPath = $this->downloader->download($message->url, $message->destinationPath);
        $this->processor->process($localPath);
    }
}

3. Configure the queue

Configuration with yiisoft/config

If you use yiisoft/app or yiisoft/app-api

Add queue configuration to your application $params config. In yiisoft/app/yiisoft/app-api templates it's typically the config/params.php file. If your project structure differs, put it into any params config file that is loaded by yiisoft/config.

Minimal configuration example:

return [
    'yiisoft/queue' => [
        'handlers' => [
            DownloadFileMessage::TYPE => RemoteFileHandler::class,
        ],
    ],
];

Advanced configuration with yiisoft/config

Manual configuration

For setting up all classes manually, see the Manual configuration guide.

4. Send (produce/push) a message to a queue

To send a message to the queue, get the queue instance and call push(). Typically the queue is injected as a dependency:

use Yiisoft\Queue\QueueProducerInterface;

final readonly class Foo
{
    public function __construct(private QueueProducerInterface $queue) {}

    public function bar(): void
    {
        $this->queue->push(new DownloadFileMessage(
            url: 'https://example.com/file-path.csv',
            destinationPath: '/tmp/file-path.csv',
        ));
    }
}

5. Handle queued messages

By default, Yii Framework uses yiisoft/yii-console to run CLI commands. If you installed yiisoft/app or yiisoft/app-api, you can run the queue worker with queue:consume:

./yii queue:consume --once # Handle all existing messages in the queue
./yii queue:consume [queueName] # Start a daemon listening for new messages permanently from the specified queue
./yii queue:consume --all [queueName [queueName2 [...]]] # Start a daemon listening for new messages permanently from all consumer-capable queues or specified list of queues (use with caution in production, recommended for dev only)

See Console commands for more details.

Producers use Yiisoft\Queue\QueueProducerInterface (push(), status(), getQueueName()); consumers use Yiisoft\Queue\QueueConsumerInterface (run(), listen()). See capability configuration for the strict role map used when named queues are configured.

In case you're running the queue in synchronous mode (no adapter), queue:consume logs an info message and exits. The messages are processed immediately when pushed.

Documentation

  • Guide
  • Internals

If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.

Versioning

This package follows semantic versioning.

The /stubs directory is intended for testing purposes only and must not be used in production code. Any changes there, including breaking ones, are always released as a patch version.

License

The Yii Queue is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

About

Framework agnostic message queue

www.yiiframework.com/

Topics

amqpbeanstalkhacktoberfestkafkanatsoptionalforframeworkannouncepulsarqueuerabbitmqredissqsvalkeyyii3

Resources

Readme
BSD-3-Clause license

Code of conduct

Code of conduct

Contributing

Contributing

Security policy

Security policy
Activity
Custom properties

Stars

60 stars

Watchers

16 watching

Forks

31 forks
Report repository

Releases

Sponsor this project

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.