美文网首页技术文PHP实战程序员
php-resque :基于Redis的后台任务系统

php-resque :基于Redis的后台任务系统

作者: codefine | 来源:发表于2016-10-22 17:38 被阅读2906次

为什么使用php-resque?

php-resque 是轻量级后台任务系统,基于Redis,功能设计简单,配置灵活。相比MQ系统大而全的MQ系统,这个显得小而美。

php-resque 角色划分

  • Job 定义任务,是负责具体的业务逻辑。
  • Queue 队列,负责Job存/取
  • Worker 从Queue中取Job来执行。 一般为PHP CLI模式下,后台守护方式运行。

使用

install

  • 如果下载慢, 可以配置 composer 国内镜像
composer config -g repo.packagist composer https://packagist.phpcomposer.com
  • 安装php-resque
composer require  "chrisboulton/php-resque 1.2"

编写Job

DemoJob.php

<?php
class DemoJob
{
    public function perform()
    {
        // Work work work
        //echo $this->args['name'];
    }
}

入队列操作

<?php

Resque::setBackend('localhost:6379');
$args = array(
      'name' => 'hanmeimei',
    );
Resque::enqueue('default', DemoJob::class, $args);

Worker代码

resque-worker.php

<?php
$redis_dsn = '127.0.0.1:6379';
putenv("REDIS_BACKEND=$redis_dsn");
// 引入队列的入口程序
$resque = realpath(dirname(__FILE__) . '/vendor/chrisboulton/php-resque/resque.php');
require_once $resque;

启动worker

php-resque 的环境变量有:

  • QUEUE – 这个是必要的,会决定 worker 要执行什么任务,重要的在前,例如 QUEUE=notify,mail,log 。也可以设定為 QUEUE=* 表示执行所有任务。

  • APP_INCLUDE – 可选,加载文件用的。可以设成 APP_INCLUDE=require.php ,在 require.php 中引入所有 Job 的 Class即可。

  • COUNT – 设定 worker 数量,预设是1 COUNT=5 。

  • REDIS_BACKEND – 设定 Redis 的 ip, port。如果没设定,预设是连 localhost:6379 。

  • LOGGING, VERBOSE – 设定 log, VERBOSE=1 即可。

  • VVERBOSE – 比较详细的 log, VVERBOSE=1 debug 的时候可以开出来看。

  • INTERVAL – worker 检查 queue 的间隔,预设是五秒 INTERVAL=5 。

  • PIDFILE – 如果你是开单 worker,可以指定 PIDFILE 把 pid 写入,例如 PIDFILE=/var/run/resque.pid 。

  • BACKGROUND 可以把 resque 丢到背景执行。或者使用 php resque.php &就可以了。

示例

QUEUE=counter php resque-worker.php

至此,php-resque的安装和使用已经完毕。

后面的章节是工具插件, 仅供参考。


界面 resque-web

监控 PHP-Resque 的运行状况

安装

gem install resque-web -v 0.0.8

运行

resque-web -p 40000

监控 supervisor

启动服务

/usr/bin/python /usr/bin/supervisord -c /etc/supervisor/supervisord.conf

监控项目配置
/etc/supervisor/conf.d/lumen_resque.conf

[program:worker_lumen_resque]
directory=/home/wwwroot/mysite
command=php resque-worker.php
environment=QUEUE='default'

优点:

  • 可以配置 程序异常退出后自动重启
  • 制定程序运行用户
  • 可以设置进程数
  • 自动重启
  • supervisord启动后,自动启动脚本
  • 分组管理

相关文章

  • php-resque :基于Redis的后台任务系统

    为什么使用php-resque? php-resque 是轻量级后台任务系统,基于Redis,功能设计简单,配置灵...

  • 利用redis和php-resque实现经典的后台任务

    一,由来 在PHP的页面编程过程中,我们总遇到这样一个问题,即是PHP是一个顺序执行的过程,只能在一个任务完成后接...

  • 变化

    Memcache/redis Redis 和 Memcached 都是基于内存的数据存储系统。Memcached是...

  • Redis学习之环境准备

    Redis学习之环境准备 Redis介绍 Redis是一个开源的,高性能的,基于内存的,基于键值对的缓存与存储系统...

  • Redis使用list队列做商品秒杀

    本代码是基于redis的list做秒杀系统: 基于Spring: Redis操作: 跑一下结果: 成功的线程nam...

  • Redis介绍、安装及基本操作

    Redis概述 1.1、Redis介绍 redis是一款基于内存的缓存系统或者数据库。支持持久化。 redis支持...

  • Redis 安装

    本文介绍基于 Linux 系统的 Redis 编译安装 一、安装 1、安装 gcc 环境 2、下载 redis 安...

  • 简单的社区系统搭建 nodeclub

    社区系统搭建成品 基于nodeclub搭建的社区系统。 系统需求环境 mongodb nodejs redis g...

  • 第 8 章(对象)

    Redis Object Redis 基于之前的那些数据结构创建了一个系统对象,这个系统包含字符串对象、列表对象、...

  • 分布式系统容错架构设计

    前言 hadoop、elasticsearch、redis cluster等系统都是基于分布式的系统架构,对于这些...

网友评论

    本文标题:php-resque :基于Redis的后台任务系统

    本文链接:https://www.haomeiwen.com/subject/ivjhuttx.html