定制 glebsky/simple-queue 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

glebsky/simple-queue

Composer 安装命令:

composer require glebsky/simple-queue

包简介

Simple queues for your php application

README 文档

README

Simple Queue

Latest Stable Version Total Downloads Stable License PHP Version

Simple queues and simple queue handling for your PHP project

Ukrainian | English | Russian

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.php which works on PDO basis.

You can also use the ready-made Transport class Glebsky\SimpleQueue\Transports\PDOTransport which is based on PDO and works with SQL databases.

To create a table of queues in the Database, the migrate method 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 example folder

UA FLAG Stand With Ukraine

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 glebsky/simple-queue 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-26