Git Product home page Git Product logo

gearman's Introduction

Gearman

Build Status Code Climate Latest Stable Version Total Downloads License

PHP library for dispatching, handling and managing Gearman Workers

Todo: Add support for tasks, only jobs are handled right now.
Todo: Tests are working but could cover more.

Table Of Content

  1. Requirements
  2. Installation
  3. Config
  4. Bootstrap
  5. Job example
  6. Dispatcher usage
  7. Start workers daemon
  8. Usage with Supervisor

Requirements

This library uses PHP 5.4+, PECL Gearman and Gearman 1.0+.

Installation

It is recommended that you install the Gearman library through composer. To do so, add the following lines to your composer.json file.

{
    "require": {
       "sinergi/gearman": "dev-master"
    }
}

Config

The library uses a Config class to share configuration between classes.

Example

use Sinergi\Gearman\Config;

$config = (new Config())
    ->addServer('127.0.0.1', 4730)
    ->setUser('apache');

Example using array

Alternatively, you can setup the config with an array.

use Sinergi\Gearman\Config;

$config = new Config([
    'servers' => ['127.0.0.1:4730', '127.0.0.1:4731'],
    'user' => 'apache'
]);

Parameters

  • string server
    The Gearman Server (E.G. 127.0.0.1:4730).

  • array servers
    Pool of Gearman Servers.

  • string bootstrap
    Path to the bootstrap file.

  • string class
    Fully qualified name of the bootstrap class, the class needs to implement the Sinergi\Gearman\BootstrapInterface interface.

  • array env_variables
    Environment variables you want to send to your bootstrap.

  • string user
    The user under which the Gearman Workers will run (E.G. apache).

  • bool auto_update
    Use for development only, automatically updates workers before doing a job or task.

Bootstrap

File /path/to/your/bootstrap.php

use Sinergi\Gearman\BootstrapInterface;
use Sinergi\Gearman\Application;

class MyBootstrap implements BootstrapInterface
{
    public function run(Application $application)
    {
        $application->add(new JobExample());
    }
}

Job example

use Sinergi\Gearman\JobInterface;
use GearmanJob;

class JobExample implements JobInterface
{
    public function getName()
    {
        return 'JobExample';
    }

    public function execute(GearmanJob $job)
    {
        // Do something
    }
}

Dispatcher usage

To send tasks and jobs to the Workers, use the Dispatcher like this:

use Sinergi\Gearman\Dispatcher;

$dispatcher = new Dispatcher($config);
$dispatcher->execute('JobExample', ['data' => 'value']);

Start workers daemon

Starts the Workers as a daemon. You can use something like supervisor to make sure the Workers are always running. You can use the same parameters as in the config.

Single server

php vendor/bin/gearman start --bootstrap="/path/to/your/bootstrap.php" --class="MyBootstrap" --server="127.0.0.1:4730"

Multiple servers

php vendor/bin/gearman start --bootstrap="/path/to/your/bootstrap.php" --class="MyBootstrap" --servers="127.0.0.1:4730,127.0.0.1:4731"

List of commands

  • start
  • stop
  • restart

Usage with Supervisor

This is an example of a Supervisor configuration. Add it to your Supervisor configuration file (E.G. /etc/supervisord.conf).

[program:mygearman]
command=php /path/to/vendor/bin/gearman start --daemon=false
numprocs=12
autostart=true
autorestart=true

gearman's People

Contributors

gabrielbull avatar localheinz avatar filsh avatar rpungello avatar serjramone avatar knigitz avatar

Watchers

James Cloos avatar 克里斯王 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.