laelaps / symfony-gearman-bundle
此包已被放弃,不再维护。未建议替代包。
将Gearman集成到Symfony中
1.2.0
2019-06-07 12:55 UTC
Requires
- php: >=5.3.0
- ext-gearman: *
- symfony/console: >=2.1
- symfony/framework-bundle: >=2.1
This package is not auto-updated.
Last update: 2023-09-15 13:03:26 UTC
README
Gearman包装器,以便您获得命令行工具并在Symfony中使用注解。
还支持Symfony网络分析工具栏集成。
安装
composer.json
{ "require": { "laelaps/symfony-gearman-bundle": "1.*@dev" } }
config.yml
我们可以为客户端配置一个服务器。因为只使用一个。如果您想通过多个Gearman服务器分担负载,则需要负载均衡器。
我们可以为工作者配置多个服务器。因为它们会在所有配置的Gearman服务器上查找工作。
laelaps_gearman: client_server: localhost:4730 worker_servers: - localhost:4730
app/AppKernel.php
<?php public function registerBundles() { $bundles = array( // ... new Laelaps\GearmanBundle\LaelapsGearmanBundle(), // ... ); }
工作者监督cron工具
有一个简单的监督bash脚本可用。有关说明,请参阅
示例
工作者
<?php // AcmeDemoBundle\Worker\ExampleWorker.php namespace AcmeDemoBundle\Worker; use GearmanJob; use Laelaps\GearmanBundle\Annotation as Gearman; use Laelaps\GearmanBundle\Worker; use Symfony\Component\Console\Output\OutputInterface; class ExampleWorker extends Worker { /** * @Gearman\PointOfEntry(name="example_job_name") * @param GearmanJob $job * @param Symfony\Component\Console\Output\OutputInterface $output * @return boolean returning false means job failure */ public function doExampleJob(GearmanJob $job, OutputInterface $output) { // do your job } }
运行工作者
Symfony样式注解
$ ./app/console gearman:worker:run AcmeBundle:ExampleWorker
请注意,这将查找Acme\Bundle\AcmeBundle\Worker\ExampleWorker
$ ./app/console gearman:worker:run ./src/AcmeDemoBundle/Worker/ExampleWorker.php
通配符也是可用的(不推荐但可能 - 结果是多个工作者使用单个进程)
$ ./app/console gearman:worker:run "./src/AcmeDemoBundle/Worker/*.php"
运行所有束中的所有工作者
$ ./app/console gearman:worker:run "./src/*/Worker/*.php"
从控制器调用工作
<?php class ExampleController { public function exampleAction() { // job name taken from PointOfEntry annotation $this->get('laelaps.gearman.client')->doBackground('example_job_name', $optionalWorkload = ''); } }
从命令行调用工作
$ ./app/console gearman:job:run example_job_name
$ ./app/console gearman:job:run example_job_name optional_workload_string
消费者(工作者的替代实现)
作为工作者实现的替代方案,还有一个消费者-处理器的实现。
使用以下命令在队列上放置工作
<?php $gearman->doBackground('queueName', serialize($workload));
编写一个像这样的处理器
<?php namespace AcmeDemoBundle\Worker; use Laelaps\GearmanBundle\Worker\HandlerInterface; use Psr\Log\LoggerInterface; class ConsumerHandler implements HandlerInterface { /** @var LoggerInterface */ protected $logger; /** * @param LoggerInterface $logger */ public function __construct(LoggerInterface $logger) { $this->logger = $logger; } /** * {@inheritdoc} */ public function handle($message) { try { $workload = unserialize($message); echo $workload; } catch (Exception $e) { $this->logger->error(sprintf("%s: %s", static::class, $e->getMessage())); return false; } return true; } }
并将此类添加到您的服务容器中,并添加一个标签
acme.worker.consumer_handler: class: AcmeDemoBundle\Worker\ConsumerHandler arguments: - "@logger" tags: - { name: laelaps.handler, queue_name: 'queueName'}
然后运行它
$ ./app/console gearman:consumer:queueName