glebsky/simple-queue
Composer 安装命令:
composer require glebsky/simple-queue
包简介
Simple queues for your php application
README 文档
README
Simple queues and simple queue handling for your PHP project
Installation
The library is installed via composer.
composer require glebsky/simple-queue
Configuration
You need to implement the TransportInterface interface.
You can use MySql, Redis or any other storage that suits you.
The data that is present in Message and your class must be based on this data.
$id - int $status - int $created_at - timestamp $updated_at - timestamp $attempts - int $queue - string $job - string $body - string $priority - int $error - string
For example, you can refer to
example/DBTranspot.phpwhich works on PDO basis.
You can also use the ready-made
TransportclassGlebsky\SimpleQueue\Transports\PDOTransportwhich is based on PDO and works with SQL databases.To create a table of queues in the Database, the
migratemethod is present in this class
Usage
Create a task
You need to create your own Job class to implement the interface JobInterface
-
public $queueName- property in this class You can assign a queue to a particular job. If this property is not added, the queue will not be assigned. -
public $priority- property in this class You can set the priority in numbers, the higher the number, the higher the priority.
handle method In this class will be executed when processing the queue. This is where you put the code you need.
An example class can be found in
example/TestJob.php
Adding a task to the queue
The Queue class is used to add a task to the queue. We need to create an instance of our TransportInterface and
connect it to the Queue class. Then we create our task - TestJob and add the task to the queue using the dispatch
method
$transport = new DBTransport(); // create transport object $queue = new Queue($transport); // add $transport to queue $job = new TestJob('testmail@gmail.com','Subject','Message text'); // create job $job->queueName = 'example_queue'; // you can change queue name $job->priority = 3; // you can change priority $result = $queue->dispatch($job); // send job to queue
Queue Processing
The queue is processed using the Worker class.
$transport = new DBTransport(); $worker = new Worker($transport); $worker->run();
You can create your own PHP script where tasks will be handled, and you can run this script via CLI.
// Worker.php <?php require_once '../vendor/autoload.php'; require_once 'DBTransport.php'; use Glebsky\SimpleQueue\Example\DBTransport; use Glebsky\SimpleQueue\Worker; if (isset($argv[1])) { $queues = explode(',', $argv[1]); } else { $queues = []; } $transport = new DBTransport(); $worker = new Worker($transport); $worker->run($queues);
An example of running a script.
php Worker.php example_queue,test_queue
Where example_queue,test_queue are the comma-separated names of the queues to be processed. If you do not pass the
queue name parameter, the handler will work with unnamed queues.
It is also possible to process a specific task, in a place convenient for you in your application.
$transport = new DBTransport(); $message = $transport->fetchMessage(['queue_name1','queue_name2']) $worker->processJob($message);
Built-in PDOTransport class
If you plan to set up communication based on SQL databases, you can use the built-in class PDOTransport
Usage example:
<?php use Glebsky\SimpleQueue\Example\TestJob; use Glebsky\SimpleQueue\Queue; use Glebsky\SimpleQueue\Transports\PDOTransport; use Glebsky\SimpleQueue\Worker; require_once '../vendor/autoload.php'; require_once 'TestJob.php'; //credentials $host = 'localhost:3306'; $db_name = 'simple_queue'; $username = 'root'; $password = ''; $jobTableName = 'jobs'; //initialize PDO connection $transport = new PDOTransport($host, $db_name, $username, $password, $jobTableName); //check for migrations and existing 'jobs' table $transport->migrate(); //Create new queue and add new job $queue = new Queue($transport); $job = new TestJob('testmail@gmail.com', 'Test Subject', 'Test Message text'); //set properties for queue $job->priority = 3; //set queueName $job->queueName = 'email_queue'; // add job to queue $result = $queue->dispatch($job); //run worker to handle queue $worker = new Worker($transport); $worker->run(['email_queue']); //or u can handle single job $message = $transport->fetchMessage(['email_queue']); $result = $worker->processJob($message); // true or false
Tests
To run tests, you can use the command composer run-script test-simple-queues
Examples can be found in the
examplefolder
glebsky/simple-queue 适用场景与选型建议
glebsky/simple-queue 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 03 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「queue」 「php」 「database queue」 「simple-queue」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 glebsky/simple-queue 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 glebsky/simple-queue 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 glebsky/simple-queue 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
PHP AMQP Binding Library
A Laravel package to monitor queue jobs.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-26

