strnoar / bqueuebundle
此包最新版本(1.3)没有可用的许可证信息。
Symfony BQueueBundle
1.3
2018-02-01 14:42 UTC
Requires
- doctrine/doctrine-bundle: *
- pda/pheanstalk: ^3.1
- symfony/symfony: >=2.8
README
BQueue 是一个为 Symfony >= 2.8 准备的 Beanstalkd 队列系统。安装、配置和使用它!
安装
$ composer require strnoar/bqueuebundle
将以下行添加到您的 AppKernel.php 文件中
new Strnoar\BQueueBundle\StrnoarBQueueBundle(),
配置
将以下行添加到您的 config.yml 文件中
strnoar_b_queue: adapter: beanstalkd # the value must be "sync" or "beanstalkd" (default: sync) host: 127.0.0.1 port: 11300 default: default # the default tube name tries: 1 # the number of time the manager try a worker who failed
使用
创建一个 Worker,这个类必须扩展 'Strnoar\BQueueBundle\Jobs\Jobs'
// MyBundle/Workers/ExampleWorker.php <?php namespace MyBundle\Workers; use Strnoar\BQueueBundle\Jobs\Jobs; class ExampleWorker implements JobsInterface { /** * @return mixed */ public function handle(Array $parameters) { // Access to the data you will pass to the parameters array value // when you dispatch the worker on queue // Do some stuff } }
现在,只需将其声明为服务
// MyBundle/Resources/config/services.yml my_bundle.exemple_worker: class: MyBundle\Workers\ExampleWorker
您可以通过容器 ID: 'bqueuebundle.job_manager' 访问工作管理器。
这是分配器
$this->get('bqueuebundle.job_manager') ->dispatch( // service is your worker service delaraction ID 'my_bundle.exemple_worker' // parameters must be an array, you can pass some value in this one ['my_key' => 'my_value'] );
如果您需要在 Worker 中注入依赖项,您可以使用方法并在服务声明中使用 'calls'
// MyBundle/Workers/ExampleWorker.php public function setDependencies(\Swift_Mailer $mailer) { $this->mailer = $mailer; return $this; }
现在,您只需执行 worker:listen 命令来执行队列中的 Worker
$ php bin/console worker:listen
您还可以指定管道和尝试次数
$ php bin/console worker:listen --tube=tube1 --tries=3
我建议使用 supervisord 来控制进程并自动化 worker:listen 命令的执行
待办事项
- 将失败的 Worker 存储到数据库中