Developed with love by KnpLabs Hire us for your project!
22

worker-bundle

by Riverline

The WorkerBundle add abstraction to queue providers and allow to create Workers to consume queue workload.

README

Build Status

What is Riverline\WorkerBundle

Riverline\WorkerBundle add abstraction to queue providers and allow to create Workers to consume queue workload.

Requirements

  • PHP 5.3
  • Symfony 2.x

Installation

Riverline\WorkerBundle is compatible with composer and any prs-0 autoloader

Configuration

riverline_worker:
    providers:
        predis:
            class: Riverline\WorkerBundle\Provider\PRedis
            arguments:
                - { host: redis.example.com }
        sqs: #v1
            class: Riverline\WorkerBundle\Provider\AwsSQS
            arguments:
                - { key: xxxxxx, secret: xxxxx }
                - sqs.eu-west-1.amazonaws.com
        sqs: #v3
            class: Riverline\WorkerBundle\Provider\AwsSQSv3
            arguments:
                -
                    version: "latest"
                    region: "us-west-2"
                    credentials:
                        key: "xxxxxx"
                        secret: "xxxxxx"
        gearman:
            class: Riverline\WorkerBundle\Provider\Gearman
            arguments:
                - [ gearman1.example.com, gearman2.examplet.com ]
        amqp: ## WIP
            class: Riverline\WorkerBundle\Provider\AMQP
        semaphore:
            class: Riverline\WorkerBundle\Provider\Semaphore
        activemq:
            class: Riverline\WorkerBundle\Provider\ActiveMQ
            arguments:
                - tcp://localhost:61613
                - login
                - passcode
                - false        # Boolean indicates if message is persistent
                - false        # Boolean indicates if broker statistics plugin is enabled http://activemq.apache.org/statisticsplugin.html
                - true         # Boolean indicates if sync mode is enabled

    queues:
        queue1:
            name: ThisIsMyQueue
            provider: predis
        queue2:
            name: https://eu-west-1.queue.amazonaws.com/xxxxxx/xxxx
            provider: sqs

Usage

You can access any configured provider or queue through the Symfony Container

<?php

$provider = $this->get('riverline_worker.provider.predis');
$provider->put('ThisIsMyQueue', 'Hello World');

$queue = $this->get('riverline_worker.queue.queue1');
echo $queue->count()." item(s) in the queue";

You can easily create Workers

<?php

// src/Acme/DemoBundle/Command/DemoWorkerCommand.php

namespace Acme\DemoBundle\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Riverline\WorkerBundle\Command\Worker;
use Riverline\WorkerBundle\Command\WorkerControlCodes;

class DemoWorkerCommand extends Worker
{
    protected function configureWorker()
    {
        $this
            // Queue name from the configuration
            ->setQueueName('queue1')

            // Inhered Command methods
            ->setName('demo-worker')
            ->setDescription('Test a worker')
        ;
    }

    protected function executeWorker(InputInterface $input, OutputInterface $output, $workload)
    {
        $output->writeln($workload);

        // Stop worker and dot not process other workloads
        if ($someReasonToStopAndExit)
        {
            return WorkerControlCodes::STOP_EXECUTION;
        }

        // else continue
        return WorkerControlCodes::CAN_CONTINUE;
    }
}

Then you can launch your worker like any other command

$ php app/console demo-worker
Hello World

You can pass options.

$ php app/console --worker-wait-timeout=60 --worker-limit=10 --memory-limit=128 --worker-exit-on-exception

This command wait 60 seconds for a workload from the queue, will process a maximum of 10 workloads or exit when usaed memory exceed 128Mb and exit if the executeWorker() throw an exception.

Copyright (c) 2004-2012 Romain Cambien

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
  • Merge pull request #19 from crosello/feature/setup_activemq_sync
    By web-flow, 5 months ago
  • ActiveMQ setup sync in configuration
    By christophe.rosello, 5 months ago
  • Merge pull request #18 from crosello/hotfix/fix_activemq_disable_unbuffered_frames
    By web-flow, 5 months ago
  • Add prefetch size in subscription headers
    By christophe.rosello, 5 months ago
  • Ack message after received/processed
    By christophe.rosello, 5 months ago
  • Merge branch 'develop'
    By rcambien, 6 months ago
  • Add Gearman test
    By rcambien, 6 months ago
  • Merge pull request #15 from Riverline/develop
    By web-flow, 6 months ago
  • Merge pull request #14 from crosello/feature/activemq
    By web-flow, 6 months ago
  • ActiveMQ implementation
    By christophe.rosello, 6 months ago
  • Merge pull request #13 from crosello/feature/docker_compose_psr4
    By web-flow, 6 months ago
  • Debug travis tests
    By christophe.rosello, 6 months ago
  • Deprecate Aws SDK V1 Provider
    By christophe.rosello, 6 months ago
  • Merge pull request #11 from Riverline/master
    By web-flow, 6 months ago
  • Move to PSR4 structure
    By christophe.rosello, 6 months ago
  • Merge pull request #10 from snamor/master
    By web-flow, 7 months ago
  • Fix warning thrown when using AmazonSQSv2 and AmazonSQSv3 with PHP 7.2.
    By snamor, 7 months ago
  • Merge pull request #8 from martijngastkemper/patch-2
    By web-flow, 10 months ago
  • Solved call to undefined method
    By web-flow, 10 months ago
  • Merge pull request #7 from martijngastkemper/patch-1
    By web-flow, 10 months ago
  • Fixed AwsSQSv3 config example
    By web-flow, 10 months ago
  • Merge pull request #5 from bijsterdee/upstream
    By web-flow, 1 year ago
  • [AwsSQSv3] Added support for mocked SQS server
    By Dennis Bijsterveld, 1 year ago
  • Merge pull request #4 from joel-bernard/master
    By web-flow, 1 year ago
  • fix decode body content
    By Joel BERNARD, 1 year ago
  • added AWS SQS v3
    By Peter Nijssen, 2 years ago
  • Update Redis provider
    By rcambien, 2 years ago
  • Merge branch 'release/1.1.7'
    By , 3 years ago
  • Long polling on receiveMessage using WaitTimeSeconds option
    By julban, 3 years ago
  • Merge branch 'develop'
    By julban, 3 years ago