treehouselabs/worker-bundle

此包已被废弃且不再维护。作者建议使用symfony/messenger包。

为Symfony2项目添加worker功能,使用Beanstalkd作为消息队列

安装次数: 57,149

依赖者: 1

建议者: 1

安全性: 0

星标: 13

关注者: 7

分支: 7

开放问题: 4

类型:symfony-bundle

2.0.1 2019-11-13 10:19 UTC

This package is auto-updated.

Last update: 2020-02-13 08:48:31 UTC


README

此包不再维护。它仍然可以与Symfony版本^2.8|^3.0|^4.0兼容,并在此存档以避免破坏使用它的现有应用程序。然而,它将不会获得维护更新或安全修复。

如果您需要在项目中使用队列/worker功能,现在有更好的解决方案可供选择

以下为之前的README 👇

Worker bundle

Latest Version on Packagist Software License Build Status Coverage Status Quality Score

一个将worker功能添加到项目的Symfony bundle,使用Beanstalkd作为消息队列。

安装

在此过程中,我们假设您已经有一个Beanstalk服务器正在运行。

通过Composer安装

$ composer require treehouselabs/worker-bundle

启用bundle

# app/AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...

            new TreeHouse\WorkerBundle\TreeHouseWorkerBundle(),
        ];

        // ...
    }

    // ...
}

配置

定义一个队列,然后您就可以开始使用了

# app/config/config.yml

tree_house_worker:
  queue:
    server: localhost

如果正在使用PheanstalkBundle,此bundle也支持它

# app/config/config.yml

tree_house_worker:
  pheanstalk: leezy.pheanstalk

基本用法

此bundle创建了一个QueueManager服务,您可以使用它来管理任务。该管理器注册了用于执行特定任务的服务,称为executors。执行者从QueueManager接收一个任务并处理它。

定义executors

首先需要注册一个执行器,并标记为执行器

# src/AppBundle/Executor/HelloWorldExecutor.php

use TreeHouse\WorkerBundle\Executor\AbstractExecutor;

class HelloWorldExecutor extends AbstractExecutor
{
    public function getName()
    {
        return 'hello.world';
    }

    public function configurePayload(OptionsResolver $resolver)
    {
        $resolver->setRequired(0);
    }

    public function execute(array $payload)
    {
        $name = array_shift($payload);

        # process stuff here, in this example we just print something
        echo 'Hello, ' . $name;

        return true;
    }
}
# app/config/services.yml
app.executor.hello_world:
  class: AppBundle/Executor/HelloWorldExecutor
  tags:
    - { name: tree_house.worker.executor }

安排任务

现在您可以通过代码或使用worker:schedule命令添加任务

在应用程序的代码中

$queueManager = $container->get('tree_house.worker.queue_manager');
$queueManager->add('hello.world', ['Peter']);

使用命令

php app/console worker:schedule hello.world Peter

处理任务

worker现在可以通过控制台接收和处理这些任务

php app/console worker:run

# prints:
# Working hello.world with payload ["Peter"]
# Hello, Peter
# Completed job in 1ms with result: true

您可以通过将它们添加到crontab中、为它们创建一个Supervisor程序或使用您首选的方法来运行worker。

文档

  1. 消息队列 & 工作者
  2. QueueManager
  3. 执行者
  4. 处理任务

安全性

如果您发现任何安全相关的问题,请通过dev@treehouse.nl发送电子邮件,而不是使用问题跟踪器。

许可证

MIT许可证(MIT)。请参阅许可文件获取更多信息。

鸣谢