Coder Social home page Coder Social logo

sinopower / work Goto Github PK

View Code? Open in Web Editor NEW

This project forked from myclabs/work

0.0 2.0 0.0 227 KB

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

Home Page: http://myclabs.github.io/Work/

License: MIT License

PHP 98.92% Shell 1.08%

work's Introduction

Build Status Coverage Status Scrutinizer Quality Score Total Downloads

MyCLabs\Work is a work queue library letting you run tasks in background using a generic abstraction.

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

Current implementations:

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

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

Extended guides:

How it works

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

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

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

$ php my-worker.php

This worker simply calls:

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

Defining tasks

Define a task:

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

And define the code that executes the task:

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.

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.

That interface offers the runAndWait method:

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.

Contributing

You can run the tests with PHPUnit:

$ 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:

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

work's People

Contributors

mnapoli avatar jdreesen avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.