Gearman/Supervisor

From Tuxunix
Jump to: navigation, search

Gearman + Supervisor

Debian : 8.x

Installation

#> apt-get install gearman-job-server re2c libgearman-dev supervisor gearadmin
  • Compile du module gearman pour PHP7 (version installé)
#> mkdir -p /tmp/install
#> cd /tmp/install
#> wget https://github.com/wcgallego/pecl-gearman/archive/master.zip
#> unzip master.zip
#> cd pecl-gearman-master
#> phpize
#> ./configure
#> make && make test && make install

Module présent dans le répertoire : "/usr/lib/php/201xxxx/"

#> echo "extension=gearman.so" > /etc/php/7.0/mods-available/gearman.ini
#> phpenmod -v ALL -s ALL gearman
#> cd /tmp && rm -rf install/pecl-gearman-master

  • Vérification du module :
#> php -m | grep gearman
gearman

Test

  • Fichier supervisor :
# cat /etc/supervisor/supervisord.conf
; supervisor config file
[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

[include]
files = /etc/supervisor/conf.d/*.conf


#> cat /etc/supervisor/conf.d/processTest.conf
[program:processTest]
command=/usr/bin/php /var/www/testGearman/TestWorker.php
user=root
numprocs=2
directory=/var/www/testGearman/
process_name=%(program_name)s_%(process_num)02d
autostart=true
autorestart=true
stdout_logfile=/var/www/testGearman/log/processTest.log
stdout_logfile_maxbytes=1MB
stderr_logfile=/var/www/testGearman/log/processTest.log
stderr_logfile_maxbytes=1MB


  • Fichier worker php
#> cat /var/www/testGearman/TestWorker.php
<?php
$worker= new GearmanWorker();
$worker->addServer();
$worker->addFunction("reverse_str", "ReverseStr");
$worker->addFunction("uppercase_name", "uppercase_name");

while ($worker->work());

function ReverseStr($job)
{
// print_r($job->workload());
$name = $job->workload();
return strrev($name);
}

function uppercase_name($job)
{
$name = $job->workload();
return strtoupper($name);
}
 
?>
  • Fichier client PHP
<?php

if(isset($_GET['str']) && !empty($_GET['str']))
{
       $string = $_GET['str'];
}
elseif(isset($argv[1]) && !empty($argv[1]))
{
       $string = $argv[1];
}
else
{
       $string = '.....dummy....';
}
$client= new GearmanClient();
$client->addServer();

print $client->doNormal("reverse_str", $string);
print "\n";
print $client->doNormal("uppercase_name",  $string);
print "\n";

?>

Restart supervisor + vérification

#> service supervisor restart && supervisorctl status

supervisorctl status
processTest:processTest_00       RUNNING    pid 8272, uptime 0:10:11
processTest:processTest_01       RUNNING    pid 8273, uptime 0:10:11
  • Test
#> php testClient.php  anaananana
anananaana
ANAANANANA


Gearman Vérification du bon fonctionnement :

#> gearadmin --status
reverse_str     0       0       2
uppercase_name  0       0       2
.
#> gearadmin --workers
33 127.0.0.1 - : reverse_str uppercase_name
35 127.0.0.1 - :
34 127.0.0.1 - : reverse_str uppercase_name
.
  • Après ajout de fichier de conf supervisor, faire un "service supervisor reload"