Skip to content

Commit

Permalink
Some better instructions and a working example which hopefully corres…
Browse files Browse the repository at this point in the history
…ponds to the README
  • Loading branch information
Nick Griffiths committed Sep 6, 2010
1 parent ebca99a commit 2d65605
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
15 changes: 0 additions & 15 deletions README

This file was deleted.

16 changes: 16 additions & 0 deletions README.markdown
@@ -0,0 +1,16 @@
## jruby-jsvc

Run your jruby application as a unix daemon, taking advantage of the features you get in jsvc - http://commons.apache.org/daemon/

JSVC is the best way to run a java program (and hence jruby) as an init.d style daemon. Unfortunately, you can't fork in jruby like you would with MRI-ruby because the vm doesn't survive being forked (lots of important threads die).

Using jsvc lets you can bring up your application in the foreground, check your db connection, ping a mail server or whatever your application needs to survive, possibly setuid (change user - jsvc options), before backgrounding it.

## How to create a jruby-jsvc daemon

1. Install jsvc-jruby. This consists of getting jsvc-jruby.jar and jsvc-wrapper.sh on to your system or in to your project folder.
1. Take a look at `example/lib/crazy_daemon.rb` - you need to create a Daemon singleton module underneath your application's namespace, something like `Crazy::Daemon`. This should have a `setup?`, `start` and `stop` method. `setup?` should return true, and is basically a detection method to check that the daemon was defined and was able to bring up whatever resources it needed to be daemonic. `start` is called once the daemon has been backgrounded and is your signal to start accepting connections or chomping strings or whatever your application does.
1. Create a boot-up script. This should require your daemon module and initialize your application so that it is ready to start serving once Daemon.start is called.
1. Create an init.d script that invokes `jsvc-wrapper.bin` (or a copy of it). You'll need to set a few environment variables, and possibly tweak `jsvc-wrapper.sh` a bit. For development, you can call `jsvc-wrapper.sh` directly to test running your app as a daemon - Just invoke it from your applications root directory, with something like (works for the example application):

JRUBY_JSVC_JAR=../target/jsvc-0.1.0.jar JRUBY_DAEMON_DEV=true MODULE_NAME=Crazy SCRIPT_NAME=daemon.rb ../bin/jsvc-wrapper.sh start
3 changes: 3 additions & 0 deletions example/bin/daemon.rb
@@ -0,0 +1,3 @@
require 'lib/crazy_daemon.rb'

Crazy::Daemon.init
10 changes: 2 additions & 8 deletions example/lib/crazy_daemon.rb
Expand Up @@ -6,11 +6,11 @@
module Crazy
module Daemon

def initialize
def init
puts "Pretending to setup stuff I need, and bind to sockets, etc"
@setup = true
end

def setup?
@setup
end
Expand All @@ -29,9 +29,3 @@ def stop
extend self
end
end

# When we are evaluated, we are expected to initialize ourselves
# so that when start is called, we are all ready to start serving
# and going about our daemonic business
Crazy::Daemon.initialize

0 comments on commit 2d65605

Please sign in to comment.