krlove/async-service-call-bundle

用于异步调用服务方法的Symfony组件

安装次数: 52,824

依赖项: 0

建议者: 0

安全: 0

星标: 10

关注者: 3

分支: 5

开放问题: 2

类型:symfony-bundle

1.0.5 2017-12-10 15:40 UTC

This package is auto-updated.

Last update: 2024-08-29 04:20:07 UTC


README

此组件允许您在后台进程中异步执行服务方法

安装

使用composer下载

composer require krlove/async-service-call-bundle

AppKernel中启用组件

$bundles = [
   ...
   new Krlove\AsyncServiceCallBundle\KrloveAsyncServiceCallBundle(),
]

配置

选项

  • console_path - console脚本的路径。可以是绝对路径或相对于kernel.root_dir参数值的相对路径。默认为Symfony 2.*的app/console和Symfony 3.*的bin/console
  • php_path - php可执行文件的路径。如果配置中没有提供选项,则将使用Symfony\Component\Process\PhpExecutableFinder::find来设置。

示例

# config.yml
krlove_async_service_call:
    console_path: bin/console
    php_path: /usr/local/bin/php

用法

定义任何服务

<?php
    
namespace AppBundle\Service;
    
class AwesomeService
{
    public function doSomething($int, $string, $array)
    {
        // do something heavy
        sleep(10)
    }
}

注册服务

# services.yml
services:
    app.service.awesome:
        class: AppBundle\Service\AwesomeService
        public: true

确保您的服务已配置为public: true

异步执行doSomething方法

$this->get('krlove.async')
    ->call('app.service.awesome', 'doSomething', [1, 'string', ['array']);

上面的代码将通过在异步执行krlove:service:call命令来运行AppBundle\Service\AwesomeService::doSomething方法。

如果成功,将返回进程PID,失败则返回null