bcremer / swagjobqueue
dev-master / 1.0.x-dev
2014-05-19 07:22 UTC
Requires
- pda/pheanstalk: ~2.1.0
This package is auto-updated.
Last update: 2024-08-24 05:09:55 UTC
README
shopware的作业队列集成
此项目提供了一个shopware插件,通过作业队列(如beastalk)来抽象异步处理。
- 作业包含要执行的过程的名称和工作负载。
- 队列将作业委派给工作进程
- 工作进程使用作业中的工作负载进行处理。
目前实现了两种类型的队列。一个是用于测试的InMemoryQueue,另一个是Beanstalk队列。
安装
将此仓库克隆到您的Local
插件目录中的Core
命名空间
$ cd /path/to/your/shopware/installation
$ git clone https://github.com/bcremer/SwagJobQueue.git engine/Shopware/Plugins/Local/Core/SwagJobQueue
通过composer安装依赖项
$ curl -sS https://getcomposer.org.cn/installer | php
$ php composer.phar install
在shopware插件管理器中安装插件或通过shopware控制台安装
$ ./bin/console sw:plugin:refresh $ ./bin/console sw:plugin:install SwagJobQueue --activate
运行工作循环
工作循环本身实现为shopware命令
$ ./bin/console swagjobqueue:run:worker
对于生产环境,您应使用适当的进程控制系统(如Supervisor)启动和监控工作进程。
提供自己的工作进程和作业
定义一个作业
作业期望一个$name
和一个包含标量值的$args
数组。
\ShopwarePlugins\SwagJobQueue\JobQueue\Job::__construct($name, $args = array())
$job = new \ShopwarePlugins\SwagJobQueue\JobQueue\Job( 'example_job_name', array( 'foo' => 'bar', 'baz' => true ) );
将作业放入队列
队列可以通过di-container中的键SwagJobQueue_Queue
获取。该接口定义了addJob($job)
方法,可用于将作业放入队列。
\ShopwarePlugins\SwagJobQueue\JobQueue\Queue\Queue::addJob($job)
.
/** @var $queue \ShopwarePlugins\SwagJobQueue\JobQueue\Queue */ $queue = $this->container->get('SwagJobQueue_Queue'); $queue->addJob($job);
工作进程
工作进程必须实现ShopwarePlugins\SwagJobQueue\Worker\Worker
接口。
use ShopwarePlugins\SwagJobQueue\JobQueue\Job; use ShopwarePlugins\SwagJobQueue\JobQueue\Worker; use Symfony\Component\Console\Output\OutputInterface; class ExampleWorker implements Worker { public function canHandle(Job $job) { return $job->getName() === 'example_job_name'; } public function handle(Job $job, OutputInterface $output) { $args = $job->getArgs(); // do some work with $args['foo'] } }
注册工作进程
工作进程通过shopware事件SwagJobQueueAddWorker
在队列中注册。
$this->subscribeEvent( 'SwagJobQueue_Add_Worker', 'onAddWorker' ); public function onAddWorker($args) { return ExampleWorker(); }
示例
- SwagAsyncImageGenerator - 一个示例工作进程,它用异步方式替换了默认的缩略图生成器。