redcode/kue

Kue 是一个由 Redis 支持的优先级任务队列

0.1-alpha 2016-08-22 16:18 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:58:28 UTC


README

PHP 版本的 Kue (Node.js)

注意:目前处于开发中!

目标是:提供一种简单而强大的方式来使用 PHP 处理后台任务

安装

"kue/kue": "*"添加到您的composer.json文件中,然后

composer install

概览

用法

创建队列

// Connect to redis "localhost:6379"
$kue = Kue::createQueue();

带 Redis 配置创建

// Connect "redis_server:6379" and select db to "1"
$kue = Kue::createQueue(array('host' => 'redis_server', 'db' => 1));

Node 兼容模式

在此模式下,您可以使用 PHP 创建工作,并使用 Node.js 处理工作

$kue = Kue::createQueue();

$kue->originalMode(true);

原始模式将创建与 Node.js 相同的工作结构,默认模式由于一些原因进行了轻微的结构调整,以便在 PHP 中简单使用。

创建工作

$kue = Kue::createQueue();

$kue->create('email', array(
    'to' => 'hfcorriez@gmail.com',
    'subject' => 'Reset your password!',
    'body' => 'Your can reset your password in 5 minutes. Url: http://xxx/reset'
))->save();

带优先级创建工作

优先级将决定您的工作处理顺序

$kue = Kue::createQueue();

$kue->create('email', array(
    'to' => 'hfcorriez@gmail.com',
    'subject' => 'Reset your password!',
    'body' => 'Your can reset your password in 5 minutes. Url: http://xxx/reset'
))->priority('high')->save();

### 使用定时创建工作

定时将在指定时间触发工作,请参考以下示例

$kue = Kue::createQueue();

$kue->create('email', array(
    'to' => 'hfcorriez@gmail.com',
    'subject' => 'Reset your password!',
    'body' => 'Your can reset your password in 5 minutes. Url: http://xxx/reset'
))->timing('tomorrow')->save();

定时格式按照 PHP 日期和时间格式进行处理,您可以使用

  • 下周一
  • +1 天
  • 下个月最后一天
  • 2013-09-13 00:00:00
  • 等等..

带延迟时间创建工作

以下示例将在 3600 秒后延迟工作

$kue = Kue::createQueue();

$kue->create('email', array(
    'to' => 'hfcorriez@gmail.com',
    'subject' => 'Reset your password!',
    'body' => 'Your can reset your password in 5 minutes. Url: http://xxx/reset'
))->delay(3600)->save();

### 使用尝试创建工作

当工作失败时,以下示例将显示如何尝试

$kue = Kue::createQueue();

$kue->create('email', array(
    'to' => 'hfcorriez@gmail.com',
    'subject' => 'Reset your password!',
    'body' => 'Your can reset your password in 5 minutes. Url: http://xxx/reset'
))->attempts(5)->save();

处理工作

注意:$kue->process 是阻塞的

要处理工作,您必须编写一个脚本并以守护进程的方式运行

$kue = Kue::createQueue();

// Process the `email` type job
$kue->on('process:email', function($job){
    // Process logic
    $data = $job->data
    mail($data['to'], $data['subject'], $data['body']);
});

// Will blocking process to subscribe the queue
$kue->process();

我将在未来提供守护进程脚本和服务。

处理给定类型

如果您想编写一个脚本来处理给定类型。

$kue = Kue::createQueue();

// Process the `email` type job
$kue->on('process:email', function($job){
    // Process logic
    $data = $job->data
    mail($data['to'], $data['subject'], $data['body']);
});

// Process `email` type only
$kue->process('email');

或使用 Node.js 风格

$kue = Kue::createQueue();

// Process `email` type only
$kue->process('email', function($job){
   // Process logic
   $data = $job->data
   mail($data['to'], $data['subject'], $data['body']);
});

处理所有类型

$kue = Kue::createQueue();

// Process all types
$kue->process(function($job){
   // Process logic
   log($job->type . ' processed');
});

许可协议

(MIT 许可证)

版权所有 (c) 2012 hfcorriez <hfcorriez@gmail.com>

以下条件准许任何获得此软件及其相关文档副本(“软件”)的个人无限制地使用该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并准许向获得软件的个人提供软件副本,前提是

上述版权声明和许可声明应包含在软件的所有副本或主要部分中。

软件按“现状”提供,不提供任何形式的保证,明示或暗示,包括但不限于适销性、特定用途适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是基于合同、侵权或其他原因,无论是源于、因之或与此软件或其使用或其他交易有关。