nebo15/drunken-russian

PHP和MongoDB的任务(队列)管理器。100%无酒精。

1.0.7 2017-08-16 09:52 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:42:56 UTC


README

PHP和MongoDB的任务管理器。100%无酒精。

通过Composer安装

在项目根目录下运行

$ composer require nebo15/drunken-russian:dev-master

开始之前

创建一些有用的索引

$ vendor/bin/drunken --db=db_name init

向cron添加清除任务

$ vendor/bin/drunken --db=db_name clear

配置

如果您不想使用控制台选项,则在脚本调用的同一目录中创建配置文件 drunken.config.php。您可以在src目录中找到配置的示例。

可用字段

  • db - 数据库名称
  • workers-dir - 字符串或数组,存放worker文件的路径
  • log_path - drunken日志的路径
  • hipchat - hipchat集成
    • from - 发送者名称
    • token
    • room

您还可以使用选项 --config="path_to_config" 运行drunken。

示例

将任务添加到队列。

<?php

require_once __DIR__ . '/vendor/autoload.php';

$drunken = new \Drunken\Manager(
    (new MongoClient())->selectDB('db_name')
);

$drunken->add('file', [
    'file' => __DIR__ . 'drunken_lines_.txt',
    'message' => 'The test line'
]);

来自worker的返回数据

  • true - 任务成功完成
  • string - 错误字符串,将存储在错误字段中
  • string delay:\d+ - 延迟任务经过的秒数

如果worker返回其他内容,则任务将标记为“失败”。

<?php

namespace Drunken;

class FileWorker extends AbstractWorker
{
    public function doThisJob(array $data)
    {
        $result = file_put_contents($data['file'], sprintf("%s\n", $data['message']), FILE_APPEND);
        
        if ($result !== false) {
            return true;
        }
        
        return false;
    }
}

运行worker。

$ vendor/bin/drunken --db=db_name --workers-dir=/path/to/drunken_workers/ do