spiral/roadrunner-jobs
Composer 安装命令:
composer require spiral/roadrunner-jobs
包简介
RoadRunner Queues (Jobs) plugin API library
README 文档
README
RoadRunner Jobs Plugin
This repository contains the codebase PHP bridge using RoadRunner Jobs plugin.
Installation
To install application server and Jobs codebase
composer require spiral/roadrunner-jobs
You can use the convenient installer to download the latest available compatible version of RoadRunner assembly:
composer require spiral/roadrunner-cli --dev vendor/bin/rr get
Configuration
First you need to add at least one jobs adapter to your RoadRunner configuration. For example, such a configuration would be quite feasible to run:
rpc: listen: tcp://127.0.0.1:6001 server: command: php consumer.php relay: pipes jobs: consume: [ "local" ] pipelines: local: driver: memory config: priority: 10 prefetch: 10000
Note Read more about all available drivers on the documentation page.
After starting the server with this configuration, one driver named local will be available to you.
Usage
Producer
The following code will allow writing and reading an arbitrary value from the RoadRunner server.
<?php use Spiral\RoadRunner\Jobs\Jobs; use Spiral\Goridge\RPC\RPC; require __DIR__ . '/vendor/autoload.php'; // Jobs service $jobs = new Jobs(RPC::create('tcp://127.0.0.1:6001')); // Select "local" pipeline from jobs $queue = $jobs->connect('local'); // Create task prototype with default headers $task = $queue->create('ping', '{"site": "https://example.com"}') // Create task with "echo" name ->withHeader('attempts', 4) // Number of attempts to execute the task ->withHeader('retry-delay', 10); // Delay between attempts // Push "echo" task to the queue $task = $queue->dispatch($task); var_dump($task->getId() . ' has been queued');
Consumer
The Consumer processes tasks from RoadRunner server and responds based on the processing outcome:
ack- is used for positive acknowledgements.nack- is used for negative acknowledgements.requeue- is used for requeuing the task.
The behavior of the nack method depends on its implementation by the queue driver. It can accept an additional
parameter redelivery; if it is passed and set to true, the task will be requeued. However, not all drivers
support this functionality. If the redelivery parameter is not passed, set to false, or the queue driver's
implementation does not support it, the task will not be requeued.
$task->nack(message: $reason, redelivery: true);
The requeue method is implemented by RoadRunner and does not depend on the queue driver. It allows you to resend
the task to the end of the queue and add additional headers to the task.
$task->withHeader('attempts', (string) ($attempts + 1))->requeue($exception);
The nack and requeue methods have the ability to specify a delay for requeuing the task. To do this, call
the withDelay method and pass the desired value before invoking the nack or requeue methods.
$task->withDelay(10)->requeue($exception);
<?php use Spiral\RoadRunner\Jobs\Consumer; use Spiral\RoadRunner\Jobs\Task\ReceivedTaskInterface; require __DIR__ . '/vendor/autoload.php'; $consumer = new Spiral\RoadRunner\Jobs\Consumer(); /** @var Spiral\RoadRunner\Jobs\Task\ReceivedTaskInterface $task */ while ($task = $consumer->waitTask()) { try { $name = $task->getName(); // "ping" $queue = $task->getQueue(); // "local" $driver = $task->getDriver(); // "memory" $payload = $task->getPayload(); // {"site": "https://example.com"} // Process task $task->ack(); } catch (\Throwable $e) { $task->requeue($e); } }
License
The MIT License (MIT). Please see LICENSE for more information. Maintained
by Spiral Scout.
spiral/roadrunner-jobs 适用场景与选型建议
spiral/roadrunner-jobs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.74M 次下载、GitHub Stars 达 27, 最近一次更新时间为 2021 年 08 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 spiral/roadrunner-jobs 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spiral/roadrunner-jobs 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.74M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 27
- 点击次数: 28
- 依赖项目数: 17
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-20