royalcms/gearman

Royalcms gearman 包。

v5.0.0 2019-09-04 05:16 UTC

This package is auto-updated.

Last update: 2024-09-05 17:09:31 UTC


README

此包允许您将 gearman 作为本地队列后端服务添加。

# 安装

首先,您需要将其添加到您的 composer.json 中

其次,在 config/provider.php 中,您需要取消注释本地图队列服务提供者

//'��Royalcms\Component\Queue\QueueServiceProvider',

并将其替换为以下内容

'Royalcms\Component\Gearman\GearmanServiceProvider',

然后,在您的 config/queue.php 文件中,您可以添加以下内容

'default' => 'gearman',
'connections' => array(
    'gearman' => array(
        'driver' => 'gearman',
        'host'   => 'localserver.6min.local',
        'queue'  => 'default',
        'port'   => 4730,
        'timeout' => 1000 //milliseconds
    )
)

或者,如果您有多个 gearman 服务器

'default' => 'gearman',
'connections' => array(
    'gearman' => array(
        'driver' => 'gearman',
        'hosts'  => array(
            array('host' => 'localserver.6min.local', 'port' => 4730),
            array('host' => 'localserver2.6min.local', 'port' => 4730),
        ),
        'queue'  => 'default',
        'timeout' => 1000 //milliseconds
    )
)

然后,在您的代码中,您可以添加以下代码(这是添加作业到队列的本地方式)

Queue::push('SomeClass', array('message' => 'The data that should be available in the SomeClass@fire method'));

小贴士,您可以使用命名空间类,以及 Laravel 文档中用于调用自定义方法的任何内容都适用于此处。

示例

我在我的应用文件夹中添加了一个 "service" 文件夹,并在其中创建了一个名为 "SendMail.php" 的文件,类的代码如下

<?php

namespace TaskProcess\Services;

class SendMail {

    public function fire($job, $data)
    {
        //I send an email to my email address with subject "gearman test" and message whatever comes from gearman
        mail('pavel@taskprocess.com', 'gearman test', $data['message']);
    }

}

在我的路由文件中添加一个新路由

RC_Route::get('/gearman', function() {
    //in a loop I add 3 jobs to gearman with different content. The purpose is to see 3 different emails with 3 different contents
    foreach (array(1,2,3) as $row) {
        RC_Queue::push('TaskProcess\Services\SendMail', array('message' => 'Message №' . $row));
    }
});

最后,我在控制台中运行

php royalcms queue:listen

然后,我检查我的电子邮件中有什么

# 问题

如果您注意到任何错误,请打开一个问题或提交请求。

希望这个包能帮到您