2017 © Pedro Peláez
 

library work

Work queue library letting you run background tasks using a generic abstraction

image

myclabs/work

Work queue library letting you run background tasks using a generic abstraction

  • Tuesday, October 27, 2015
  • by mnapoli
  • Repository
  • 7 Watchers
  • 54 Stars
  • 262 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 1 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Build Status Coverage Status Scrutinizer Quality Score Total Downloads, (*1)

MyCLabs\Work is a work queue library letting you run tasks in background using a generic abstraction., (*2)

It's intent is to be compatible with classic work queue solutions (RabbitMQ, Beanstalkd, …) while offering a high level abstraction., (*3)

Current implementations:, (*4)

  • InMemory: synchronous implementation, task are executed directly (useful for tests or dev environments)
  • RabbitMQ
  • Beanstalkd

Feel free to contribute and submit other implementations (Gearman, …)., (*5)

Extended guides:, (*6)

How it works

In you code (HTTP request for example), you can run a task in background:, (*7)

$workDispatcher = new RabbitMQWorkDispatcher(/* parameters */);
$workDispatcher->run(new MyTask());

Separately, you set up a worker to run continuously on the command line (like a deamon):, (*8)

$ php my-worker.php

This worker simply calls:, (*9)

// my-worker.php
$worker = new RabbitMQWorker(/* parameters */);
// Will loop continuously and execute tasks
$worker->work();

Defining tasks

Define a task:, (*10)

class BigComputation implements MyCLabs\Work\Task\Task
{
    public $parameter1;
}

And define the code that executes the task:, (*11)

class BigComputationExecutor implements MyCLabs\Work\TaskExecutor\TaskExecutor
{
    public function execute(Task $task)
    {
        if (! $task instanceof BigComputation) {
            throw new \Exception("Invalid task type provided");
        }
        // Perform the action (here we just multiply the parameter by 2)
        return $task->parameter1 * 2;
    }
}

Execute a task and wait for its result

The run($task) method runs a task in background., (*12)

If you want to wait for the result of that task, you have to use a WorkDispatcher that implements the \MyCLabs\Work\Dispatcher\SynchronousWorkDispatcher interface. For example, the RabbitMQ adapter implements this interface., (*13)

That interface offers the runAndWait method:, (*14)

interface SynchronousWorkDispatcher extends WorkDispatcher
{
    /**
     * Run a task in background.
     *
     * You can use $wait to wait a given time for the task to complete.
     * If the task hasn't finished during this time, $timedout will be
     * called and this method will return.
     * If the task has finished, $completed will be called.
     *
     * @param Task     $task
     * @param int      $wait      Number of seconds to wait for the task to complete.
     *                            If 0, doesn't wait.
     * @param callable $completed Called (if $wait > 0) when the task has completed.
     * @param callable $timedout  Called (if $wait > 0) if we hit the timeout while
     *                            waiting.
     * @param callable $errored   Called (if $wait > 0) if the task errors. Takes 1
     *                            parameter which is the exception.
     *
     * @return void No results
     */
    public function runAndWait(
        Task $task,
        $wait = 0,
        callable $completed = null,
        callable $timedout = null,
        callable $errored = null
    );
}

Read more

Read more in the docs., (*15)

Contributing

You can run the tests with PHPUnit:, (*16)

$ composer install
$ vendor/bin/phpunit

Some functional tests need external programs like RabbitMQ or Beanstalkd. For practical reasons, you can boot a VM very quickly using Vagrant and the included configuration. You can then run the tests in the VM:, (*17)

$ vagrant up
$ vagrant ssh
$ cd /vagrant
$ composer install
$ vendor/bin/phpunit

The Versions

27/10 2015

dev-master

9999999-dev

Work queue library letting you run background tasks using a generic abstraction

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

rabbitmq task worker background beanstalkd distributed work work queue

30/05 2014

0.4.0

0.4.0.0

Work queue library letting you run distributed tasks using a generic abstraction

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

rabbitmq task distributed work work queue

23/04 2014

0.3.0

0.3.0.0

Work queue library letting you run distributed tasks using a generic abstraction

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

rabbitmq task distributed work work queue

21/10 2013

0.2.0

0.2.0.0

Work queue library letting you run distributed tasks using a generic abstraction

  Sources   Download

MIT

The Requires

 

rabbitmq task distributed work work queue

25/09 2013

0.1.0

0.1.0.0

Work queue library letting you run distributed tasks using a generic abstraction

  Sources   Download

MIT

The Requires

 

rabbitmq task distributed work work queue