dubture/async-bundle

为Symfony2提供易于使用的异步后端工作抽象

安装: 21

依赖者: 0

建议者: 0

安全: 0

星标: 3

关注者: 4

分支: 0

开放问题: 0

类型:symfony-bundle

0.0.1 2015-04-18 07:30 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:37:18 UTC


README

Build Status Scrutinizer Code Quality

此Symfony扩展提供了一种将昂贵逻辑发送到后台工作进程的高级方法。

配置

  1. 使用 composer require "dubture/async-bundle" 安装此扩展包
  2. 将必要的扩展包添加到您的Kernel
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(

        // register the async bundle
        new Dubture\AsyncBundle\DubtureAsyncBundle(),

        // register the dependencies of the async bundle
        new JMS\DiExtraBundle\JMSDiExtraBundle($this),
        new JMS\AopBundle\JMSAopBundle(),

        // your application bundles here...

    );

    return $bundles;
}
  1. 配置要使用的后端
# app/config/config.yml
dubture_async:
  backend: rabbitmq # one of rabbitmq|resque|sonata|runtime

用法

考虑以下服务

<services>
    <service class="Acme\Bundle\MediaTranscodingService" id="media_transcoder">
    </service>
</services>
class MediaTranscodingService
{
    public function transcodeFile($sourcePath)
    {
            // ... do some heavy-lifting, e.g. media transcoding
    }
}

如果您想将此方法委托给后台工作进程,这需要您做的一切

use Dubture\AsyncBundle\Annotation\Async;

class MediaTranscodingService
{
    /**
     * @Async
     */
    public function transcodeFile($sourcePath)
    {
            // ... do some heavy-lifting, e.g. media transcoding
    }
}

现在对 transcodeFile 的任何调用都将被拦截并委托给后台工作进程。

带有 @Async 注解的方法需要遵循以下约定

  1. 声明异步方法的类必须是服务
  2. 方法参数必须是可序列化的(无资源,例如,doctrine连接)
  3. 方法不应该返回任何内容(任何返回值都将丢失)

如果您需要在后台工作进程内部发生某些事情时做出反应,您可以在完成后简单地派发事件。

后台工作进程实现依赖于以下扩展包之一

请参阅 Resources/docs 了解特定后端的文档。