Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
dougal committed Apr 10, 2008
0 parents commit 72a059f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 0 deletions.
14 changes: 14 additions & 0 deletions 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 changes: 11 additions & 0 deletions 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 changes: 2 additions & 0 deletions 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 changes: 5 additions & 0 deletions generators/daemon/templates/daemons.yml
@@ -0,0 +1,5 @@
dir_mode: script
dir: ../../log
multiple: false
backtrace: true
monitor: true
19 changes: 19 additions & 0 deletions 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 changes: 15 additions & 0 deletions 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 change: 1 addition & 0 deletions install.rb
@@ -0,0 +1 @@
puts IO.read(File.join(File.dirname(__FILE__), 'README'))

0 comments on commit 72a059f

Please sign in to comment.