Client:
<?php
$gmclient= new GearmanClient();
$gmclient->addServer();
$gmclient->doBackground("reverse", "this is a test");
echo "done!\n";
Worker:
<?php
echo "Starting\n";
$gmworker= new GearmanWorker();
$gmworker->addServer();
$gmworker->addFunction("reverse", "reverse_fn");
while($gmworker->work()){}
function reverse_fn($job)
{
$result= strrev($job->workload());
$fp = fopen('test.txt', 'a');
fwrite($fp, $result);
fclose($fp);
echo "Result: $result\n";
return $result;
}
?>
The problem is that when I call the client nothing happens until I run
php worker.php
then it writes to the file, but the shell is just stuck at
[user@server gearman]$ php worker.php
Starting
Result: tset a si siht
Is there anyway I can make it so that when it calls doBackground, it runs the worker.php file in the background? I also want to make sure this work when gearmand is restarted so that I don’t have to run worker.php if the daemon is restarted.
Posted as answer from my comment:
Take a look at supervisord (supervisord.org) for managing your php scripts. supervisor will do the process management for you, and respawn php scripts in the background if they die