Skip to content

Instantly share code, notes, and snippets.

@Deraen
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deraen/e81d350a552767bf50b2 to your computer and use it in GitHub Desktop.
Save Deraen/e81d350a552767bf50b2 to your computer and use it in GitHub Desktop.
Clojure daemon (jsvc, centos initscript) NOTE: Not very complete initscript
(ns foobar.daemon
(:require [foobar.main :as main])
(:import [org.apache.commons.daemon Daemon DaemonContext])
(:gen-class
:implements [org.apache.commons.daemon.Daemon]))
(def state (atom nil))
;; Daemon implementation
(defn -init [this ^DaemonContext context])
(defn -start [this]
(swap! state main/start))
(defn -stop [this]
(swap! state main/stop))
(defn -destroy [this])
;; Enable command-line invocation
(defn -main [& args]
(main/start {}))
#!/bin/bash
#
# chkconfig: - 99 5
# description: Foobar
#
### BEGIN INIT INFO
# Provides: foobar
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop Foobar
### END INIT INFO
# Exit statuses:
# 1 = General error
# Symlink this into /etc/init.d/{foobar-full,foobar-alt}
# "./foobar-full" -> "full"
APP="$(echo $(basename $0) | sed -s 's/foobar-\(.*\)/\1/')"
case "$APP" in
"full") PORT="3000";;
"alt") PORT="3001";;
*) echo "invalid target: $APP"; exit 1;;
esac
CURRENT_JARFILE="/home/foobar/app/foobar_$APP.jar"
PIDFILE="/var/run/foobar_$APP.pid"
LOGFILE="/var/log/foobar_$APP.log"
USER="foobar"
CLASS="foobar.daemon"
JAVA_HOME="/usr/lib/jvm/java-1.7.0"
jsvc_exec() {
/usr/bin/jsvc \
-java-home "$JAVA_HOME" \
-cp "/usr/share/java/commons-daemon.jar:$CURRENT_JARFILE" \
-pidfile "$PIDFILE" \
-outfile "$LOGFILE" \
-errfile "&1" \
-user "$USER" \
-Dport="$PORT" \
-Dfoobar.mode="test" \
$1 \
$CLASS
}
start() {
echo "Starting Foobar ($APP)... "
jsvc_exec
RETVAL=$?
return $RETVAL
}
stop() {
echo "Stopping Foobar ($APP)..."
jsvc_exec "-stop"
RETVAL=$?
}
status() {
# FIXME: test pid is running
if [ -a $PIDFILE ]; then
echo "Running"
else
echo "Stopped"
fi
}
RETVAL=0
case "$1" in
start) start;;
stop) stop;;
restart)
stop
start
;;
status) status;;
*) echo "invalid command: $1"; exit 1;;
esac
exit $RETVAL
(ns foobar.main
(:require [ring.adapter.jetty :as jetty]
[clojure.string :as s]
[foobar.db :as db]
[foobar.handler :refer [resolve-handler]]))
(defn start [system & [options]]
(db/connect! (and options (:db options)))
;; FIXME: use foobar.env
(assoc system :jetty (jetty/run-jetty (resolve-handler) {:port (or (:port options) (Integer/parseInt (System/getProperty "port")) 3000) :join? false})))
(defn stop [{:keys [jetty] :as system}]
(if jetty
(.stop jetty))
(db/disconnect!)
nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment