Giter Site home page Giter Site logo

serve's Introduction

Serve 基于Swoole Server 编写的消息队列消费系统

已支持功能:

  • 支持数据库操作
  • 仅支持Redis 作为消息队列

环境要求:

运行如图:

调试模式 "php serve start"

image

守护进程模式 "php serve start -d "

image

Serve-Queue 是什么?

通过Swoole Server 实现消费端并命名为 “Serve-Queue”,Swoole Server API 都可以轻松实现;最新版Swoole 可在多进程worker/task, 添加协程更加高效处理任务。PS, 目前还未使用协程

核心特点

  • 命令行:快速实现消息中间件消费、支持守护进程、常驻内存;
  • 自动加载:基于 PSR-4 ,完全使用 Composer 构建;
  • 模块化:支持 Composer ,可以很方便的使用第三方库;

Install

  $> git clone https://github.com/twomiao/Serve.git
  $> cd Serve/
  $> composer install

Run

  $> cd Serve/
  $> chmod 777 ./serve
  $> php serve start

Serve-Queue [Options]

  • start: 调试模式运行 [ 额外打印所有信息到终端];
  • start -d: 守护进程运行;
  • reload: 平滑重启task进程 [Job::doJob];
  • stop: 停止运行Serve-Queue;
  • reload:all: 平滑重启Worker 和 Task 进程;

Job Code

<?php

namespace app\Job;

use app\Services\Order;
use Serve\Colors\Color;
use Serve\Colors\ColorText;
use Serve\Core\Log;
use Serve\Interfaces\IJob;

class Job implements IJob
{
    /**
     * @param array $data
     * @param \Serve\Interfaces\mysql $db
     * @return string|null 
     * null: 不执行after();
     * string: 执行after();
     */
    public function doJob(array $data, $db): ?string
    {
        $job = "task (".getmypid()."), succeeded ({$data['order_sn']}).";

        // 举栗子:带颜色的打印信息
        Color::println("2333, 平滑重启成功", ColorText::FG_LIGHT_RED);

        switch ($data['action']) {
            // 超时自动取消订单
            case Action::ORDER_CANCELLED:
                // 订单关闭逻辑
//                (new Order())->getOrder($data['order_sn'])->cancelled();
                return $job;
            case Action::MAILER_SEND:
                // 邮件发送逻辑
                break;
            default:
                // 其它操作
                break;
        }
        return null;
    }

    /**
     * @param $data
     * @return mixed
     * Swoole 文档地址:https://wiki.swoole.com/wiki/page/135.html
     * 此方法不支持平滑重启
     */
    public function after($data)
    {
        // 带颜色的输出 - 紫色
//        Color::println("哈哈哈哈哈",ColorText::FG_LIGHT_PURPLE);
        // 记录日志
        Log::debug($data);
    }
}

serve's People

Contributors

twomiao avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.