Gearman
, (*1)
PHP library for dispatching, handling and managing Gearman Workers, (*2)
Todo: Add support for tasks, only jobs are handled right now._
_Todo: Tests are working but could cover more., (*3)
Table Of Content
- Requirements
- Installation
- Config
- Bootstrap
- Job example
- Dispatcher usage
- Start workers daemon
- Usage with Supervisor
, (*4)
Requirements
This library uses PHP 5.4+, PECL Gearman and
Gearman 1.0+., (*5)
, (*6)
Installation
It is recommended that you install the Gearman library through composer. To do so, add the following lines to your composer.json
file., (*7)
{
"require": {
"websightnl/gearman": "~1.0"
}
}
, (*8)
Config
The library uses a Config class to share configuration between classes., (*9)
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., (*10)
use Sinergi\Gearman\Config;
$config = new Config([
'servers' => ['127.0.0.1:4730', '127.0.0.1:4731'],
'user' => 'apache'
]);
, (*11)
Parameters
-
string __server__
The Gearman Server (E.G. 127.0.0.1:4730)., (*12)
-
array __servers__
Pool of Gearman Servers., (*13)
-
string __bootstrap__
Path to the bootstrap file., (*14)
-
string __class__
Fully qualified name of the bootstrap class, the class needs to implement the Sinergi\Gearman\BootstrapInterface
interface., (*15)
-
array __env_variables__
Environment variables you want to send to your bootstrap., (*16)
-
string __user__
The user under which the Gearman Workers will run (E.G. apache)., (*17)
-
bool auto_update__
Use for __development only, automatically updates workers before doing a job or task., (*18)
-
string __pidFilename__
Change the filename of the created PID file (defaults to gearmanhandler.pid). The file is always created in the system temp path., (*19)
-
string __lockFilename__
Change the filename of the created lock file (defaults to gearmanhandler.lock). The file is always created in the system temp path., (*20)
-
int __loopTimeout__
Change the time (in milliseconds) between pinging the Gearman server. Defaults to the low value of 10 milliseconds, for legacy reasons. **Change this value if you experience high load on your Gearman server!**, (*21)
, (*22)
Bootstrap
File /path/to/your/bootstrap.php
, (*23)
use Sinergi\Gearman\BootstrapInterface;
use Sinergi\Gearman\Application;
class MyBootstrap implements BootstrapInterface
{
public function run(Application $application)
{
$application->add(new JobExample());
}
}
, (*24)
Job example
use Sinergi\Gearman\JobInterface;
use GearmanJob;
class JobExample implements JobInterface
{
public function getName()
{
return 'JobExample';
}
public function execute(GearmanJob $job)
{
// Do something
}
}
, (*25)
Dispatcher usage
To send tasks and jobs to the Workers, use the Dispatcher like this:, (*26)
use Sinergi\Gearman\Dispatcher;
$dispatcher = new Dispatcher($config);
$dispatcher->execute('JobExample', ['data' => 'value']);
, (*27)
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., (*28)
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
, (*29)
Usage with Supervisor
This is an example of a Supervisor configuration. Add it to your Supervisor configuration file (E.G. /etc/supervisord.conf)., (*30)
[program:mygearman]
command=php /path/to/vendor/bin/gearman start --daemon=false
process_name=%(program_name)s-procnum-%(process_num)s
numprocs=12
autostart=true
autorestart=true