This project provides a shopware plugin that abstracts asynchronous processing via job queues like beastalk.
- Jobs contain the name and the workload of the process to be done.
- The Queue delegates jobs to the workers
- Worker does the processing using the workload from the jobs.
Currently two types of queues are implemented. A InMemoryQueue for testing puposes and a Beanstalk queue.
Clone this repo into the Core
namespace in your Local
plugin directory:
$ cd /path/to/your/shopware/installation
$ git clone https://github.com/bcremer/SwagJobQueue.git engine/Shopware/Plugins/Local/Core/SwagJobQueue
Install dependencies via composer:
$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar install
Install Plugin in the shopware plugin manager or via the shopware console:
$ ./bin/console sw:plugin:refresh
$ ./bin/console sw:plugin:install SwagJobQueue --activate
The worker loop itself is implemented as a shopware command
$ ./bin/console swagjobqueue:run:worker
For production you should start and monitor the worker process using a proper pocess control system like Supervisor.
A job expects a $name
and an $args
array containing scalar values.
\ShopwarePlugins\SwagJobQueue\JobQueue\Job::__construct($name, $args = array())
$job = new \ShopwarePlugins\SwagJobQueue\JobQueue\Job(
'example_job_name',
array(
'foo' => 'bar',
'baz' => true
)
);
The queue can be obtained via the key SwagJobQueue_Queue
from the di-container.
The interface defines the method addJob($job)
that can be used to put a job into the queue.
\ShopwarePlugins\SwagJobQueue\JobQueue\Queue\Queue::addJob($job)
.
/** @var $queue \ShopwarePlugins\SwagJobQueue\JobQueue\Queue */
$queue = $this->container->get('SwagJobQueue_Queue');
$queue->addJob($job);
The worker has to implement the ShopwarePlugins\SwagJobQueue\Worker\Worker
interface.
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']
}
}
The workers are registered in the queue via the shopware event SwagJobQueueAddWorker
.
$this->subscribeEvent(
'SwagJobQueue_Add_Worker',
'onAddWorker'
);
public function onAddWorker($args)
{
return ExampleWorker();
}
- SwagAsyncImageGenerator - A example worker that replaces the default thumbnail generator with a async one.