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.
This library uses PHP 5.4+ and Gearman 1.0+.
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"
}
}
The library uses a Config class to share configuration between classes.
use Sinergi\Gearman\Config;
$config = (new Config())
->addServer('127.0.0.1', 4730)
->setUser('apache');
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'
]);
-
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 theSinergi\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.
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());
}
}
use Sinergi\Gearman\JobInterface;
use GearmanJob;
class JobExample implements JobInterface
{
public function getName()
{
return 'JobExample';
}
public function execute(GearmanJob $job)
{
// Do something
}
}
To send tasks and jobs to the Workers, use the Distpacher like this:
use Sinergi\Gearman\Dispatcher;
$dispatcher = new Dispatcher($config);
$dispatcher->execute('JobExample', ['data' => 'value']);
Starts the Workers as a daemon. You can use something like supervisord to make sure the Workers are always running. You can use the same parameters as in the config.
php vendor/bin/gearman start --bootstrap="/path/to/your/bootstrap.php" --class="MyBootstrap" --server="127.0.0.1:4730"
php vendor/bin/gearman start --bootstrap="/path/to/your/bootstrap.php" --class="MyBootstrap" --servers="127.0.0.1:4730,127.0.0.1:4731"
start
stop
restart