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

worker-bundle

by treehouselabs

A Symfony bundle that adds worker functionality to your project, using Beanstalkd as the message queue.

Worker bundle

Latest Version on Packagist
Software License
Build Status
Coverage Status
Quality Score

A Symfony bundle that adds worker functionality to your project, using
Beanstalkd as the message queue.

Installation

For this process, we assume you have a Beanstalk server up and running.

Install via Composer:

$ composer require treehouselabs/worker-bundle:~1.0

Enable the bundle:

# app/AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...

            new TreeHouse\WorkerBundle\TreeHouseWorkerBundle(),
        ];

        // ...
    }

    // ...
}

Configuration

Define a queue and you're good to go:

# app/config/config.yml

tree_house_worker:
  queue:
    server: localhost

The bundle also supports the PheanstalkBundle, if you're using that:

# app/config/config.yml

tree_house_worker:
  pheanstalk: leezy.pheanstalk

Basic Usage

The bundle creates a QueueManager service, which you can use to manage
jobs. The manager has services registered to execute specific tasks, called
executors. An executor receives a job from the QueueManager and processes it.

Defining executors

First you need to register an executor, and tag it as such:

# src/AppBundle/Executor/HelloWorldExecutor.php

use TreeHouse\WorkerBundle\Executor\AbstractExecutor;

class HelloWorldExecutor extends AbstractExecutor
{
    public function getName()
    {
        return 'hello.world';
    }

    public function configurePayload(OptionsResolver $resolver)
    {
        $resolver->setRequired(0);
    }

    public function execute(array $payload)
    {
        $name = array_shift($payload);

        # process stuff here, in this example we just print something
        echo 'Hello, ' . $name;

        return true;
    }
}
# app/config/services.yml
app.executor.hello_world:
  class: AppBundle/Executor/HelloWorldExecutor
  tags:
    - { name: tree_house.worker.executor }

Scheduling jobs

Now you can add jobs, either via code or using the worker:schedule command:

In your application's code:

$queueManager = $container->get('tree_house.worker.queue_manager');
$queueManager->add('hello.world', ['Peter']);

Using the command:

php app/console worker:schedule hello.world Peter

Working jobs

A worker can now receive and process these jobs via the console:

php app/console worker:run

# prints:
# Working hello.world with payload ["Peter"]
# Hello, Peter
# Completed job in 1ms with result: true

You can run workers by adding them to your crontab, creating a
Supervisor program for it, or whatever your preferred method is.

Documentation

  1. Message queues & workers
  2. The QueueManager
  3. Executors
  4. Working jobs

Security

If you discover any security related issues, please email dev@treehouse.nl
instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

Credits

The MIT License (MIT)

Copyright (c) 2015 TreeHouse.

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.