dev-master
9999999-devGeneric class to queue and run tasks asynchronously
BSD-2-Clause
The Requires
by César D. Rodas
Wallogit.com
2017 © Pedro Peláez
Generic class to queue and run tasks asynchronously
Queue-agnostic way of doing asynchronous jobs in PHP., (*1)
This is a work in process, it isn't ready for production., (*2)
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)
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) { }
Generic class to queue and run tasks asynchronously
BSD-2-Clause