Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit.

  • Loading branch information...
commit 72a059fbcd15730cb0c7a14ac0c61690a36148db 0 parents
@dougal dougal authored
14 README
@@ -0,0 +1,14 @@
+Daemon Generator
+================
+
+To get yourself rolling:
+> sudo gem install daemons
+> ./script/generate daemon <name>
+
+Then insert your code in the lib/daemons/<name>.rb stub. All pid's and logs will live in the normal log/ folder. This helps to make things Capistrano friendly.
+
+Individual control script:
+> ./lib/daemons/<name>_ctl [start|stop|restart]
+
+App-wide control script (I add this to my capistrano recipe's after_restart task):
+> ./script/daemons [start|stop|restart]
11 generators/daemon/daemon_generator.rb
@@ -0,0 +1,11 @@
+class DaemonGenerator < Rails::Generator::NamedBase
+ def manifest
+ record do |m|
+ m.directory "lib/daemons"
+ m.file "daemons", "script/daemons", :chmod => 0755
+ m.template "script.rb", "lib/daemons/#{file_name}.rb", :chmod => 0755
+ m.template "script_ctl", "lib/daemons/#{file_name}_ctl", :chmod => 0755
+ m.file "daemons.yml", "config/daemons.yml"
+ end
+ end
+end
2  generators/daemon/templates/daemons
@@ -0,0 +1,2 @@
+#!/usr/bin/env ruby
+Dir[File.dirname(__FILE__) + "/../lib/daemons/*_ctl"].each {|f| `#{f} #{ARGV.first}`}
5 generators/daemon/templates/daemons.yml
@@ -0,0 +1,5 @@
+dir_mode: script
+dir: ../../log
+multiple: false
+backtrace: true
+monitor: true
19 generators/daemon/templates/script.rb
@@ -0,0 +1,19 @@
+#!/usr/bin/env ruby
+
+#You might want to change this
+ENV["RAILS_ENV"] ||= "production"
+
+require File.dirname(__FILE__) + "/../../config/environment"
+
+$running = true;
+Signal.trap("TERM") do
+ $running = false
+end
+
+while($running) do
+
+ # Replace this with your code
+ ActiveRecord::Base.logger << "This daemon is still running at #{Time.now}.\n"
+
+ sleep 10
+end
15 generators/daemon/templates/script_ctl
@@ -0,0 +1,15 @@
+#!/usr/bin/env ruby
+require 'rubygems'
+require "daemons"
+require 'yaml'
+require 'erb'
+require 'active_support'
+
+options = YAML.load(
+ ERB.new(
+ IO.read(
+ File.dirname(__FILE__) + "/../../config/daemons.yml"
+ )).result).with_indifferent_access
+options[:dir_mode] = options[:dir_mode].to_sym
+
+Daemons.run File.dirname(__FILE__) + '/<%=file_name%>.rb', options
1  install.rb
@@ -0,0 +1 @@
+puts IO.read(File.join(File.dirname(__FILE__), 'README'))
Please sign in to comment.
Something went wrong with that request. Please try again.