定制 icephp/mini-queue 二次开发

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

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

icephp/mini-queue

Composer 安装命令:

composer require icephp/mini-queue

包简介

IcePHP/MiniQueue is a lightweight PHP library for managing job queues and processing tasks asynchronously. This library provides a simple and efficient way to handle tasks in the background, allowing your application to perform time-consuming operations without blocking the main execution flow

README 文档

README

Author: Michael Piper Contact: icephp@pipermichael.com

IcePHP/MiniQueue is a lightweight PHP library for managing job queues and processing tasks asynchronously. This library provides a simple and efficient way to handle tasks in the background, allowing your application to perform time-consuming operations without blocking the main execution flow.

composer require icephp/mini-queue

Files

queue.php

<?php
require_once __DIR__."/vendor/autoload.php";
use IcePHP\MiniQueue\MiniQueue;
$miniQueue = new MiniQueue([
    'driver' => 'pdo_sqlite',
    'path' => __DIR__ . '/../db.sqlite',
]);
$miniQueue->bootstrap();
return $miniQueue;

index.php

<?php
use IcePHP\MiniQueue\MiniQueue;
/**
 * @var MiniQueue
 */
$miniQueue = require_once __DIR__."/queue.php";
// Handle the /send-email route
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === '/emailreminder/index.php/send-email') {
    $requestBody = file_get_contents('php://input');
    $payload = json_decode($requestBody, true);
    $timezone = $payload['timezone']?? 'Africa/Lagos';
     $delay = new DateTime($scheduledDateTime, new DateTimeZone($timezone));
    $job = $miniQueue->queue([
            'delay' => $delay, 
            'type' => 'TaskStart',
            'data' => $payload
        ]);
} else {
    http_response_code(404);
    echo json_encode(['error' => 'Route not found', 'resp' => isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '']);
}
?>

worker.php

<?php
use IcePHP\MiniQueue\MiniQueue, IcePHP\MiniQueue\ORM\Job;
/**
 * @var MiniQueue
 */
$miniQueue = require_once __DIR__."/queue.php";
require __DIR__ . '/vendor/autoload.php';

class EmailSender
{
    public function __construct()
    {
       
    }

    private function sendEmail($payload)
    {
      // send mail here
      // mail()
    }

    public function sendScheduledEmail($emailData)
    {
        $adminEmails = $emailData['admin'];
        $recipientEmails = $emailData['recipient'];
        $subject = $emailData['subject'];
        $dueDateTime = $emailData['dueDateTime'];
        $hourstoDelivery = $emailData['hourstoDelivery'];
        $taskName = $emailData['taskName'];
        $interval =  $emailData['interval'];
        // ...
    }

    function generateEmailContent()
    {
        // implement email content here
    }
}

$miniQueue->log("Worker tick ". (new DateTime('now', new DateTimeZone(date_default_timezone_get()) ))->format('D, M, Y H:i:s'));
$miniQueue->process(type: 'TaskStart', callback:function (Job $job, callable $done) {
    $emailData = $job->getData();
    // Handle the /send-email route
    $emailSender = new EmailSender();
    $emailSender->sendScheduledEmail($emailData);
    $done();
});

Usage

  • Include the MiniQueue library in your project.
  • Use the provided classes and methods for managing job queues and processing tasks.
  • See examples in the code for sending scheduled emails and processing jobs.

Getting Started

Installation

To get started, include the MiniQueue library in your project:

$miniQueue = require_once __DIR__."/queue.php";
require __DIR__ . '/vendor/autoload.php';
use IcePHP\MiniQueue\MiniQueue, IcePHP\MiniQueue\ORM\Job;

Examples

Sending Scheduled Emails

$emailSender = new EmailSender();

$emailData = [
    // ...
];

$emailSender->sendScheduledEmail($emailData);

Running the Worker

You can run the worker using a cron job:

* * * * * /usr/local/bin/ea-php81 /home/****/public_html/****/worker.php

Make sure to adjust the path to the PHP executable (/usr/local/bin/ea-php81) and the path to your worker.php script accordingly.

Contributing

If you'd like to contribute to this project, please follow the standard GitHub fork and pull request workflow. Additionally, make sure to adhere to the code of conduct.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Special thanks to the contributors of IcePHP/MiniQueue for their valuable contributions.

Support

For any inquiries or support, please contact icephp@pipermichael.com.

icephp/mini-queue 适用场景与选型建议

icephp/mini-queue 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 10 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「queue」 「cronjob」 「scheduler」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 icephp/mini-queue 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 icephp/mini-queue 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-17