2017 © Pedro Peláez
 

library worker

Generic class to queue and run tasks asynchronously

image

crodas/worker

Generic class to queue and run tasks asynchronously

  • Wednesday, October 29, 2014
  • by crodas
  • Repository
  • 2 Watchers
  • 1 Stars
  • 33 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 3 % Grown

The README.md

Worker

Queue-agnostic way of doing asynchronous jobs in PHP., (*1)

This is a work in process, it isn't ready for production., (*2)

Why?

To provide an easy way of running things in the backend in PHP. Anything that performs IO is a potential candidate for being run in the background. Sending emails, resize a picture, get the content of a website., (*3)

The idea behind this project is to provide a very easy interface to queue jobs/tasks., (*4)

All tasks are runs asynchronously by design, (*5)

How?

It creates a lightweight manager PHP process, which is responsible for creating/checking/killing the "workers". The idea is to run the manager and forget about it., (*6)

The manager should scale/decrease the number of workers (up to a maximum)., (*7)

The are two main classes, the Client and the Server., (*8)

require "vendor/autoload.php";

use crodas\Worker\Config;
use crodas\Worker\Server;

$config = new Config;
$config->setEngine("gearman")->addDirectory("my-tasks/");

new Server($config)->serve();

The Server is quite simple, and it is design so it can be run from the console terminal, never from a web server., (*9)

require "vendor/autoload.php";

use crodas\Worker\Config;
use crodas\Worker\Client;

$config = new Config;
$config->setEngine("gearman");

$client = new Client;

$client->push("do_something", ['arg1', 'arg2']);
$client->push("do_something", ['arg4', 'arg3']);

The Client is object is quite simple, they push tasks and forget about it. Notice that the Config object for the client doesn't have the addDirectory(), this is because a client doesn't need to know where the worker code are located., (*10)

Finally, defining workers are deadly simple. They need to be located in a directory where the server can reach them with addDirectory(). They need to be callable and that's all it takes., (*11)


/** @Worker("do_something") */ function foobar($args) { }

The Versions

29/10 2014

dev-master

9999999-dev

Generic class to queue and run tasks asynchronously

  Sources   Download

BSD-2-Clause

The Requires

 

by César D. Rodas