Debian PHP jabber client deamon

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sabine Dinis Blochberger

    #1

    Debian PHP jabber client deamon

    I have written a rather simple php script using class_jabber.ph p
    (http://www.centova.com). It just logs in as a bot, and keeps track of
    the on/offline status of our support staff (writing to a firebird2
    table). It works fine as is when it's running.

    The problem is that the daemon will stop working (exits memory) without
    any trace in the log files after seemingly random amounts of time.

    I use set_time_limit( 0); to prevent PHP stopping the script. I have put
    up signal handlers (I know that some signals aren't supposed to be
    catchable).

    Here's some code from the php script:

    function sig_handler($si gno) {
    switch ($signo) {
    case SIGTERM:
    case SIGSTOP:
    case SIGKILL:
    case SIGINT:
    // handle shutdown tasks
    error_log(date( '\[d-m-Y H:i:s\] ') . "Received TERM signal.\r\n",
    3, $logfile);
    $jab->terminated = true;
    // disconnect from the Jabber server
    $jab->disconnect() ;

    // update database, all unavailable when bot exits
    setAllUnavailab le();
    return;
    break;
    case SIGHUP:
    // handle restart tasks
    break;
    default:
    // handle all other signals
    }
    }
    // setup signal handlers
    pcntl_signal(SI GTERM, "sig_handle r");
    pcntl_signal(SI GSTOP, "sig_handle r");
    pcntl_signal(SI GKILL, "sig_handle r");
    pcntl_signal(SI GINT, "sig_handle r");

    define("CBK_FRE Q", 1); // fire a callback event every second

    // create an instance of the Jabber class
    $jab = new Jabber(false);

    // create an instance of our event handler class
    $test = new TestMessenger($ jab);

    // set handlers for the events we wish to be notified about
    $jab->set_handler("c onnected", $test, "handleConnecte d");
    $jab->set_handler("a uthenticated", $test, "handleAuthenti cated");
    $jab->set_handler("a uthfailure", $test, "handleAuthFail ure");
    $jab->set_handler("h eartbeat", $test, "handleHeartbea t");
    $jab->set_handler("e rror", $test, "handleErro r");
    $jab->set_handler("m essage_normal", $test, "handleMessage" );
    $jab->set_handler("m essage_chat", $test, "handleMessage" );

    // connect to the Jabber server
    if (!$jab->connect(JABBER _SERVER)) {
    die("Could not connect to the Jabber server!\n");
    }

    // now, tell the Jabber class to begin its execution loop
    $jab->execute(CBK_FR EQ);

    // Note that we will not reach this point (and the execute() method
    // will not
    // return) until $jab->terminated is set to TRUE. The execute()
    // method simply
    // loops, processing data from (and to) the Jabber server, and
    // firing events
    // (which are handled by our TestMessenger class) until we tell it
    // to terminate.
    //
    // This event-based model will be familiar to programmers who have
    // worked on
    // desktop applications, particularly in Win32 environments.

    // disconnect from the Jabber server
    $jab->disconnect() ;

    // update database, all unavailable when bot exits
    setAllUnavailab le();

    error_log(date( '\[d-m-Y H:i:s\] ') . "Stopped.\r \n", 3, $logfile);

    ibase_close($co nnection);

    Here's the daemon script for running it in Debian (etch).

    #! /bin/sh
    #
    # supportbotd initscript
    # script for a php-based jabber control bot
    #
    # Author: Sabine Dinis Blochberger, Op3racional.
    #
    #

    set -e

    PATH=/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/loc\
    al/imbot
    DESC="IM support bot"
    NAME=op3_suppor tbot.php
    DAEMON=/usr/local/imbot/$NAME
    PIDFILE=/var/run/$NAME.pid
    SCRIPTNAME=/usr/local/imbot/supportbotd

    # Gracefully exit if the package has been removed.
    #test -x $DAEMON || exit 0

    # Read config file if it is present.
    #if [ -r /etc/default/$NAME ]
    #then
    # . /etc/default/$NAME
    #fi

    #
    # Function that starts the daemon/service.
    #
    d_start() {
    start-stop-daemon --start --quiet --background \
    --make-pidfile --pidfile $PIDFILE \
    --exec /usr/bin/php $DAEMON &
    }

    #
    # Function that stops the daemon/service.
    #
    d_stop() {
    start-stop-daemon --stop --quiet --pidfile $PIDFILE \
    --signal SIGTERM
    }

    #
    # Function that sends a SIGHUP to the daemon/service.
    #
    d_reload() {
    start-stop-daemon --stop --quiet --pidfile $PIDFILE \
    --name $NAME --signal 1
    }

    case "$1" in
    start)
    echo -n "Starting $DESC: $NAME"
    d_start
    echo "."
    ;;
    stop)
    echo -n "Stopping $DESC: $NAME"
    d_stop
    echo "."
    ;;
    #reload)
    #
    # If the daemon can reload its configuration without
    # restarting (for example, when it is sent a SIGHUP),
    # then implement that here.
    #
    # If the daemon responds to changes in its config file
    # directly anyway, make this an "exit 0".
    #
    # echo -n "Reloading $DESC configuration.. ."
    # d_reload
    # echo "done."
    #;;
    restart|force-reload)
    #
    # If the "reload" option is implemented, move the
    "force-reload"
    # option to the "reload" entry above. If not,
    "force-reload" is
    # just the same as "restart".
    #
    echo -n "Restarting $DESC: $NAME"
    d_stop
    sleep 1
    d_start
    echo "."
    ;;
    *)
    # echo "Usage: $SCRIPTNAME
    {start|stop|res tart|reload|for ce-reload}" >&\
    2
    echo "Usage: $SCRIPTNAME {start|stop|res tart|force-reload}" >&2
    exit 1
    ;;
    esac

    exit 0


    Note that stopping via this bash script doesn't work. I have to kill -9
    the process. Any help on either these codes is appreciated. Thanks :)

    PHP version is 5.
  • Sabine Dinis Blochberger

    #2
    Re: Debian PHP jabber client deamon

    Sabine Dinis Blochberger wrote:
    I have written a rather simple php script using class_jabber.ph p
    (http://www.centova.com). It just logs in as a bot, and keeps track of
    the on/offline status of our support staff (writing to a firebird2
    table). It works fine as is when it's running.
    >
    The problem is that the daemon will stop working (exits memory) without
    any trace in the log files after seemingly random amounts of time.
    >
    Is there a way at least to restart the process once it leaves memory?

    Thanks,
    Sabine

    Comment

    Working...