SwagJobQueue

Created: 2014-05-19 06:13
Updated: 2018-08-23 03:23
License: mit
php

README.md

SwagJobQueue

A job queue integration for shopware

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.

Installation

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

Run worker loop

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.

Provide own Workers and Jobs

Define a job

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
    )
);

Put job in queue

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);

Worker

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']
    }
}

Register worker

The workers are registered in the queue via the shopware event SwagJobQueueAddWorker.

$this->subscribeEvent(
    'SwagJobQueue_Add_Worker',
    'onAddWorker'
);

public function onAddWorker($args)
{
    return ExampleWorker();
}

Examples

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more