install gearmand with php & supervisor

reference site :

case : centos, amazon-linux-ami

1. install gearman

sudo yum install uuid-devel libuuid libuuid-devel uuid boost-devel libevent libevent-devel libcurl-devel openssl-devel
sudo yum install mysql-devel sqlite-devel libmemcached-devel #option

wget https://launchpad.net/gearmand/1.2/1.1.5/+download/gearmand-1.1.8.tar.gz
tar xvfz gearmand-1.1.8.tar.gz
cd gearmand-1.1.8
./configure
make
sudo make install

sudo cp support/gearmand.init /etc/init.d/gearmand
sudo chmod 755 /etc/init.d/gearmand
sudo vi /etc/init.d/gearmand

gearmand=/usr/local/sbin/gearmand
OPTIONS="--log-file=/var/log/gearmand/gearmand.log \
--verbose INFO"

daemon --pidfile=$pidfile --user=gearman $gearmand -d $OPTIONS

sudo useradd -M gearman -s /sbin/nologin -r
sudo mkdir /var/run/gearmand
sudo touch /var/log/gearmand.log
sudo chown gearman:gearman /var/run/gearmand /var/log/gearmand.log

2. gearman with PHP

sudo pecl install gearman
sudo vi /etc/php.d/gearman.ini

extension=gearman.so

# check gearman php module
php -i

gearman

gearman support => enabled
extension version => 1.1.1
libgearman version => 1.1.5
Default TCP Host => localhost
Default TCP Port => 4730

3. gearman php test

reverse_worker.php

<?php 

echo "Starting\n"; 

# Create our worker object. 
$gmworker= new GearmanWorker(); 

# Add default server (localhost). 
$gmworker->addServer('127.0.0.1', 4730);

# Register function "reverse" with the server. Change the worker function to
# "reverse_fn_fast" for a faster worker with no output.
$gmworker->addFunction("reverse", "reverse_fn");

print "Waiting for job...\n";
while($gmworker->work())
{
  if ($gmworker->returnCode() != GEARMAN_SUCCESS)
  {
    echo "return_code: " . $gmworker->returnCode() . "\n";
    break;
  }
}

function reverse_fn($job)
{
  echo "Received job: " . $job->handle() . "\n";

  $workload = $job->workload();
  $workload_size = $job->workloadSize();

  echo "Workload: $workload ($workload_size)\n";

  # This status loop is not needed, just showing how it works
  for ($x= 0; $x < $workload_size; $x++)   {     echo "Sending status: " . ($x + 1) . "/$workload_size complete\n";     $job->sendStatus($x, $workload_size);
    sleep(1);
  }

  $result= strrev($workload);
  echo "Result: $result\n";

  # Return what we want to send back to the client.
  return $result;
}

# A much simpler and less verbose version of the above function would be:
function reverse_fn_fast($job)
{
  return strrev($job->workload());
}

reverse_client.php

<?php 

# Create our client object. 
$gmclient= new GearmanClient(); 

# Add default server (localhost). 
$gmclient->addServer('127.0.0.1', 4730);

echo "Sending job\n";

# Send reverse job
do
{
  $result = $gmclient->doNormal("reverse", "Hello!");

  # Check for various return packets and errors.
  switch($gmclient->returnCode())
  {
    case GEARMAN_WORK_DATA:
      echo "Data: $result\n";
      break;
    case GEARMAN_WORK_STATUS:
      list($numerator, $denominator)= $gmclient->doStatus();
      echo "Status: $numerator/$denominator complete\n";
      break;
    case GEARMAN_WORK_FAIL:
      echo "Failed\n";
      exit;
    case GEARMAN_SUCCESS:
      break;
    default:
      echo "RET: " . $gmclient->returnCode() . "\n";
      exit;
  }
}
while($gmclient->returnCode() != GEARMAN_SUCCESS);

At first terminal

php reserve_worker.php
Starting
Waiting for job...

At another terminal

php reserve_client.php
Sending job
php reverse_worker.php 
Starting
Waiting for job...
Received job: H:xxxxxxxx:11258
Workload: Hello! (6)
Sending status: 1/6 complete
Sending status: 2/6 complete
Sending status: 3/6 complete
Sending status: 4/6 complete
Sending status: 5/6 complete
Sending status: 6/6 complete
Result: !olleH

4. using supervisor

reference sitehttp://stackoverflow.com/questions/8217848/running-gearman-workers-in-the-background

sudo yum install python-setuptools
sudo easy_install supervisor

sudo touch /etc/init.d/supervisord;sudo chmod +x /etc/init.d/supervisord
sudo vi /etc/init.d/supervisord

#!/bin/sh
#
# /etc/rc.d/init.d/supervisord
#
# Supervisor is a client/server system that
# allows its users to monitor and control a
# number of processes on UNIX-like operating
# systems.
#
# chkconfig: - 64 36
# description: Supervisor Server
# processname: supervisord

# Source init functions
. /etc/rc.d/init.d/functions

prog="supervisord"

prefix="/usr/"
exec_prefix="${prefix}"
prog_bin="${exec_prefix}/bin/supervisord"
PIDFILE="/var/run/$prog.pid"
OPTIONS="-c /etc/supervisord/supervisord.conf"

start()
{
        echo -n $"Starting $prog: "
        daemon $prog_bin --pidfile $PIDFILE $OPTIONS
        [ -f $PIDFILE ] && success $"$prog startup" || failure $"$prog startup"
        echo
}

stop()
{
        echo -n $"Shutting down $prog: "
        [ -f $PIDFILE ] && killproc $prog || success $"$prog shutdown"
        echo
}

case "$1" in

  start)
    start
  ;;

  stop)
    stop
  ;;

  status)
        status $prog
  ;;

  restart)
    stop
    start
  ;;

  *)
    echo "Usage: $0 {start|stop|restart|status}"
  ;;

esac
sudo mkdir /etc/supervisord
sudo mkdir /etc/supervisord/conf.d
echo "echo_supervisord_conf > /etc/supervisord/supervisord.conf" | sudo sh
sudo vi /etc/supervisord/supervisord.conf

[unix_http_server]
file=/var/run/supervisor.sock
chmod=0700

[supervisord]
logfile=/var/log/supervisord.log
pidfile=/var/run/supervisord.pid

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock

[include]
files = /etc/supervisord/conf.d/*.conf
vi /etc/supervisord/conf.d/reverse.conf

[program:gearman]
command=/usr/bin/php reverse_worker.php
numprocs=1
directory=/home/xxxxx/
stdout_logfile=/home/xxxxx/reverse_worker.log
environment=GEARMAN_USER=gearman
autostart=true
autorestart=true
user=gearman
stopsignal=KILL

reference site : http://till.klampaeckel.de/blog/archives/94-start-stop-daemon,-Gearman-and-a-little-PHP.html

case : ubuntu

sudo apt-get install make gcc g++
sudo apt-get install libboost-thread-dev libboost-program-options-dev libevent-dev uuid-dev libpq-dev libcloog-ppl0 libcurl4-gnutls-dev
sudo apt-get install libmemcached-dev libmariadbclient-dev libsqlite3-dev # option

wget https://launchpad.net/gearmand/1.2/1.1.8/+download/gearmand-1.1.8.tar.gz
tar xvfz gearmand-1.1.8.tar.gz
cd gearmand-1.1.8
./configure
make
sudo make install

sudo useradd -M gearman -s /sbin/nologin -r
sudo mkdir /var/run/gearman /var/log/gearman-job-server
sudo touch /var/log/gearman-job-server/gearman.log
sudo chown gearman:gearman /var/run/gearman /var/log/gearman-job-server

sudo vi /etc/init.d/gearman-job-server

#!/bin/sh

# Gearman server and library
# Copyright (C) 2008 Brian Aker, Eric Day
# All rights reserved.
#
# Use and distribution licensed under the BSD license.  See
# the COPYING file in this directory for full text.

### BEGIN INIT INFO
# Provides:          gearman-job-server
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable gearman job server
### END INIT INFO

prefix=/usr/local
exec_prefix=${prefix}
NAME=gearmand
DAEMON=${exec_prefix}/sbin/gearmand
PIDDIR=/var/run/gearman
PIDFILE=${PIDDIR}/gearmand.pid
GEARMANUSER="gearman"
PARAMS="--verbose INFO"

test -x ${DAEMON} || exit 0

. /lib/lsb/init-functions

test -f /etc/default/gearman-job-server && . /etc/default/gearman-job-server

start()
{
  log_daemon_msg "Starting Gearman Server" "gearmand"
  if ! test -d ${PIDDIR}
  then
    mkdir ${PIDDIR}
    chown ${GEARMANUSER} ${PIDDIR}
  fi
  if start-stop-daemon \
    --start \
    --exec $DAEMON \
    -- --pid-file=$PIDFILE \
       --user=$GEARMANUSER \
       --daemon \
       --log-file=/var/log/gearman-job-server/gearman.log \
       $PARAMS 
  then
    log_end_msg 0
  else
    log_end_msg 1
    log_warning_msg "Please take a look at the syslog"
    exit 1
  fi
}

stop()
{
  log_daemon_msg "Stopping Gearman Server" "gearmand"
  if start-stop-daemon \
    --stop \
    --oknodo \
    --exec $DAEMON \
    --pidfile $PIDFILE
  then
    log_end_msg 0
  else
    log_end_msg 1
    exit 1
  fi
}

status()
{
    status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
}

case "$1" in

  start)
    start
  ;;

  stop)
    stop
  ;;

  status)
    status
  ;;

  restart|force-reload)
    stop
    start
  ;;

  *)
    echo "Usage: $0 {start|stop|restart|force-reload|status}"
  ;;

esac

for using mysql queue table

sudo vi /etc/init.d/gearmand
OPTIONS="--log-file=/var/log/gearmand.log \
--verbose INFO --queue-type=MySQL \
--mysql-host=XXX.XXX.XXX.XXX \
--mysql-port=3306 \
--mysql-user=gearman \
--mysql-password=gearman_password \
--mysql-db=gearman \
--mysql-table=queues"
CREATE USER "gearman"@"XXX.XXX.XXX.XXX" IDENTIFIED BY "gearman_password";
GRANT ALL PRIVILEGES ON gearman.* TO "gearman"@"XXX.XXX.XXX.XXX";
CREATE DATABASE gearman;
USE gearman;
CREATE TABLE queues(unique_key VARCHAR(64) PRIMARY KEY, function_name VARCHAR(255), when_to_run INT, priority INT, data LONGBLOB);

글쓴이

yupmin

Inside Deep Throat.

답글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다

This site uses Akismet to reduce spam. Learn how your comment data is processed.