Clojure daemon (jsvc, centos initscript) NOTE: Not very complete initscript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 {})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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