Upstart script for Glu agents (sharing)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Upstart script for Glu agents (sharing)

sodul
While the agents are quite resilient they can crash once in a while, or can even self kill from the All Processes view.

I've written an upstart script that I'm sharing here. It should start the agent automatically on reboot as well as keeping it alive if it crashes.

Save it as /etc/init/glu-agent.conf and you should be good to go.

Note that you will have to update the 'env' lines as well as 'setuid', 'setgid' and 'chdir'.

Tested on Ubuntu 12.04 LTS.

Enjoy!

description "Glu agent"
author "Stephane Odul"
# based on http://tad-do.net/2013/07/24/writing-upstart-script-for-forky-java-application/


start on (filesystem and net-device-up IFACE=lo)
stop on runlevel [!2345]

env APP_USER=glu
env APP_DIR=/home/glu/current
env APP_SCRIPT=/home/glu/current/bin/agent-server.sh

# respawn the job up to 10 times within a 5 second period.
respawn
respawn limit 10 5
# Note: the variables do not expand for setuid, setgid or chdir in upstart config files.
setuid glu
setgid glu
chdir /home/glu/current


# Start the app in the pre-start script
pre-start script
    echo "$(date): calling $APP_SCRIPT stop (pre-start)"
    $APP_SCRIPT stop
    echo "$(date): calling $APP_SCRIPT start"
    $APP_SCRIPT start
end script

# Main script is actually tracking the process
script
    trackApp() {
        echo "$(date): trackApp: tracking $APP_USER org.linkedin.glu.agent-server"
        while [ $(pgrep -U $APP_USER -c -f org.linkedin.glu.agent-server) -gt 0 ]; do
            sleep 1
        done
        echo "$(date): trackApp: no $APP_USER org.linkedin.glu.agent-server process"
    }
    trackApp
end script

# Graceful shutdown
pre-stop script
    echo $(date) calling $APP_SCRIPT stop
    exec $APP_SCRIPT stop
    sleep 2
end script
Reply | Threaded
Open this post in threaded view
|

Re: Upstart script for Glu agents (sharing)

frenchyan
Administrator
Thanks for sharing your script with us. If you want we can add it to the glu-contribs project (https://github.com/pongasoft/glu-contribs).

How does this interact with the auto upgrade capability? During the auto upgrade procedure, the agent needs to be brought down to be restarted. I am just wondering if this is not going to conflict.

Yan
Reply | Threaded
Open this post in threaded view
|

Re: Upstart script for Glu agents (sharing)

sodul
I actually do not know, we do not use the autoupgrade feature since our Glu agent files are deployed and managed by Salt (similar to Puppet but in Python).

I'll try to find some time to run the tutorial on a VM and see what needs to happen with the auto-upgrade procedure. My guess is that the auto-upgrade would replace some steps with 'sudo stop glu-agent', perform the upgrade, then 'sudo start glu-agent'.

I've forked the flu-contrib project and will pull/push the upstart script under setups.

Note: the respawn rule is not very good. It is set to stop if it fails 10 times over 5s. Since we have some 'sleeps' in there I should probably rewrite it as failing 5 times over 10s.