grom/silex-resque

该软件包已被废弃且不再维护。没有建议的替代软件包。
关于该软件包最新版本(dev-master)没有可用的许可证信息。

dev-master 2013-07-02 13:54 UTC

This package is auto-updated.

Last update: 2022-02-01 12:38:37 UTC


README

此软件包提供将 php-resque 软件包与您的 Silex 项目集成改进的解决方案。它包含一个用于维护工作进程的命令行工具,以及一个抽象的工作类,使得 Silex 应用程序对每个工作进程都可用。

安装

php composer.phar require grom/silex-resque:dev-master

如果您已经设置了控制台,只需将 Grom\SilexResque\Command\ResqueWorkerCommand($app) 命令添加到其中。如果没有,这里有一个简单的方法来创建一个

php composer.phar require knplabs/console-service-provider:dev-master

然后创建一个名为 bin/console 的文件,使其可执行,然后填充如下内容

#!/usr/bin/env php
<?php
$loader = require __DIR__.'/../vendor/autoload.php';

$app = new Silex\Application;

/* Your bootstrap script, shared with the web app
 * Should "return $app" and configure all services.
 * Or, you know, configure your services here instead.
 */
$app = require __DIR__.'/src/bootstrap.php'; 

// Register console provider
$app->register(new Knp\Provider\ConsoleServiceProvider(), array(
    'console.name'              => 'My Project Console',
    'console.version'           => '1.0.0-alpha,
    'console.project_directory' => __DIR__,
));

// Add the Resque worker command
$app['console']->add(new \Grom\SilexResque\Command\ResqueWorkerCommand($app));

// ...add any other commands you want

$app['console']->run();

要启动一个工作进程,请运行 bin/console resque:worker default(其中 default 是队列的名称。)

创建工作

创建一个继承自 Grom\SilexResque\BaseJob 的工作类,并填写所需的 execute 函数

<?php
namespace MyVendor\MyProject\Job;

use Grom\SilexResque\BaseJob;
use Silex\Application;

class TestJob extends BaseJob
{
    protected function execute(array $args, Application $app)
    {
        // do stuff here, e.g.
        print_r($args);
    }
}

现在从您的 Silex 应用程序中排队一个工作

$app->get('/queuetest', function () {
    $token = Resque::enqueue('default', 'MyVendor\MyProject\Job\TestJob', array(
        'arg1' => 1,
        'stuff' => 'things',
    ), true);
    
    return print_r($token, true);
});

令牌可用来稍后获取工作信息

$status = new Resque_Job_Status($token);
echo $status->get(); // Outputs the status

监控工作

由于 PHP-Resque 与原始 Ruby 版本兼容,您可以使用其 webapp 来管理监控队列,重试失败的工作等。使用 gem install resque 安装它,然后使用 resque-web -p 5678 运行它。完成操作后,您可以使用 resque-web --kill 杀死其服务器。