Navigation Menu

Skip to content

Commit

Permalink
package deb: support daemonization by forever
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Aug 15, 2012
1 parent f7bac7f commit 4f4ec39
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/debian/gcs.dirs
@@ -1,2 +1,3 @@
var/lib/gcs
var/log/gcs
var/run/gcs
33 changes: 26 additions & 7 deletions packages/debian/gcs.init
Expand Up @@ -19,26 +19,37 @@ PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="an Amazon CloudSearch compatible open source full text search server"
NAME=gcs
DAEMON=/usr/sbin/$NAME
LIB_DIR=/usr/lib/`dpkg-architecture -qDEB_HOST_MULTIARCH`
FOREVER=$LIB_DIR/gcs/node_modules/.bin/forever
LOG_DIR=/var/log/$NAME
USER=gcs
GROUP=gcs
PORT=7575
PIDFILE=/var/run/gcs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
NODE_ENV=production
OPTION_ARGS=""
START_STOP_DAEMON_ARGS=""
ENABLE="no"

export NODE_ENV

DEFAULT_FILE=/etc/default/gcs

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
[ -x "$FOREVER" ] || exit 0

# Read configuration variable file if it is present
[ -r $DEFAEULT_FILE ] && . $DEFAULT_FILE

[ "$ENABLE" = "yes" ] || exit 0

DAEMON_ARGS="--daemon --pid-file ${PIDFILE}"
FOREVER_ARGS="--append --pidFile ${PIDFILE}"
FOREVER_ARGS="$FOREVER_ARGS -l $LOG_DIR/forever.log"
FOREVER_ARGS="$FOREVER_ARGS -o $LOG_DIR/output.log"
FOREVER_ARGS="$FOREVER_ARGS -e $LOG_DIR/error.log"
DAEMON_ARGS=""
if [ -n "${PORT}" ]; then
DAEMON_ARGS="${DAEMON_ARGS} --port ${PORT}"
fi
Expand Down Expand Up @@ -77,10 +88,11 @@ do_start()
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON \
${START_STOP_DAEMON_ARGS} --test > /dev/null || return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON \
${START_STOP_DAEMON_ARGS} -- $DAEMON_ARGS || return 2
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec \
$FOREVER $START_STOP_DAEMON_ARGS --test > /dev/null || return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec \
$FOREVER $START_STOP_DAEMON_ARGS -- \
start $FOREVER_ARGS $DAEMON $DAEMON_ARGS || return 2
# Add code here, if necessary, that waits for the process to be ready
# to handle requests from services started subsequently which depend
# on this one. As a last resort, sleep for some time.
Expand All @@ -97,6 +109,12 @@ do_stop()
# 2 if daemon could not be stopped
# other if a failure occurred

[ $FOREVER list $FOREVER_ARGS | \
grep --quiet "No forever processes running" ] && return 1

[ $FOREVER stop $FOREVER_ARGS $DAEMON 2>&1 | \
grep --quiet "error" ] && return 2

start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \
--pidfile $PIDFILE --name $NAME
RETVAL="$?"
Expand All @@ -107,7 +125,8 @@ do_stop()
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 \
--exec $FOREVER
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
Expand All @@ -123,7 +142,7 @@ do_reload() {
# restarting (for example, when it is sent a SIGHUP),
# then implement that here.
#
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
$FOREVER restart $FOREVER_ARGS $DAEMON $DAEMON_ARGS
return 0
}

Expand Down
13 changes: 13 additions & 0 deletions packages/debian/gcs.logrotate
@@ -0,0 +1,13 @@
/var/log/gcs/*.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 640 gcs gcs
sharedscripts
postrotate
service gcs status && service gcs restart
endscript
}
6 changes: 4 additions & 2 deletions packages/debian/gcs.postinst
Expand Up @@ -11,10 +11,12 @@ add_system_user() {
}

fixperms() {
dpkg-statoverride --list /var/lib/gcs >/dev/null || \
dpkg-statoverride --update --add gcs gcs 0750 /var/lib/gcs
dpkg-statoverride --list /var/lob/gcs >/dev/null || \
dpkg-statoverride --update --add gcs gcs 0750 /var/log/gcs
dpkg-statoverride --list /var/run/gcs >/dev/null || \
dpkg-statoverride --update --add gcs gcs 0755 /var/run/gcs
dpkg-statoverride --list /var/lib/gcs >/dev/null || \
dpkg-statoverride --update --add gcs gcs 0755 /var/lib/gcs
}

case "$1" in
Expand Down
14 changes: 14 additions & 0 deletions packages/debian/gcs.upstart
@@ -0,0 +1,14 @@
description 'Groonga CloudSearch'
author 'Yoji Shidara <dara@shidara.net>'

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

env NODE_ENV=production

setuid gcs
setgid gcs

script
exec /usr/sbin/gcs
end script
1 change: 1 addition & 0 deletions packages/debian/rules
Expand Up @@ -28,6 +28,7 @@ DESTDIR = debian/tmp

override_dh_install:
npm install --production node-gyp
npm install --production forever
npm install --production

eval $$(dpkg-architecture); \
Expand Down

0 comments on commit 4f4ec39

Please sign in to comment.