Skip to content

Commit

Permalink
Refactoring, specs, documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mirasrael committed Jan 13, 2012
1 parent e392edc commit 63d11b4
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .rspec
@@ -0,0 +1,2 @@
--color
--format progress
24 changes: 23 additions & 1 deletion README
Expand Up @@ -11,6 +11,7 @@ Then insert your code in the lib/daemons/<name>.rb stub. All pids and logs will

== CHANGES ==

1.0.0 - changed api for Daemons::Rails::Monitoring, fixed path in template for script, improved documentation, added RSpec
0.0.3 - added rake for running script without daemonization (rake daemon:<name>)

== CONTROL ==
Expand All @@ -19,6 +20,27 @@ Individual control script:
> ./lib/daemons/<name>_ctl [start|stop|restart|status]
> rake daemon:<name>[:(start|stop|status)]

Examples:
rake daemon:test - runs lib/daemons/test.rb not daemonized
rake daemon:test:start - start daemon using lib/daemons/test_ctl start
rake daemon:test:stop - stop daemon using lib/daemons/test_ctl stop
rake daemon:test:status - show running status for daemon using lib/daemons/test_ctl status

App-wide control script:
> ./script/daemons [start|stop|restart]
> rake daemons:(start|stop|status)
> rake daemons:(start|stop|status)

== MONITORING API ==

Daemons::Rails::Monitoring.statuses - hash with all daemons and corresponding statuses
Daemons::Rails::Monitoring.start("test.rb") - start daemon using lib/daemons/test_ctl start
Daemons::Rails::Monitoring.stop("test.rb") - start daemon using lib/daemons/test_ctl stop
Daemons::Rails::Monitoring.controllers - list of controllers
Daemons::Rails::Monitoring.controller("test.rb") - controller for test.rb application

controller = Daemons::Rails::Monitoring.controller("test.rb")
controller.path # => lib/daemons/test_ctl
controller.app_name # => test.rb
controller.start # => starts daemon
controller.stop # => stops daemon
controller.status # => :not_exists or :running
1 change: 1 addition & 0 deletions daemons-rails.gemspec
Expand Up @@ -19,4 +19,5 @@ Gem::Specification.new do |gem|
gem.add_dependency 'daemons'
gem.add_dependency 'multi_json', '~>1.0'
gem.add_development_dependency "rake"
gem.add_development_dependency 'rspec', '~>2.7.0'
end
28 changes: 28 additions & 0 deletions lib/daemons/rails/controller.rb
@@ -0,0 +1,28 @@
module Daemons
module Rails
class Controller
attr_reader :path, :app_name

def initialize(controller_path)
@path = controller_path
@app_name = "#{controller_path.basename.to_s[0..-'_ctrl'.length]}.rb"
end

def run(command)
`#{path} #{command}`
end

def start
run('start')
end

def stop
run('stop')
end

def status
run('status').to_s.split("\n").last =~ /: running \[pid \d+\]$/ ? :running : :not_exists
end
end
end
end
35 changes: 14 additions & 21 deletions lib/daemons/rails/monitoring.rb
@@ -1,4 +1,6 @@
require "daemons"
require "daemons/rails/config"
require "daemons/rails/controller"

module Daemons
module Rails
Expand All @@ -11,33 +13,24 @@ def self.daemons_directory
@daemons_directory ||= ::Rails.root.join('lib', 'daemons')
end

def self.application(app_name)
group(app_name).applications.first
def self.controller(app_name)
controllers.find { |controller| controller.app_name == app_name }
end

# We do not cache group to be sure we have actual information about running applications
def self.group(app_name)
app_config = Daemons::Rails::Config.new(app_name, ::Rails.root)
group = Daemons::ApplicationGroup.new("#{app_name}.rb", app_config.to_hash)
group.setup
group
def self.controllers
Pathname.glob(daemons_directory.join('*_ctl')).map { |path| Daemons::Rails::Controller.new(path) }
end

def self.groups
groups = []
daemons_directory.each_entry do |file|
if !file.directory? && file.basename.to_s =~ /(\w+)_ctl/
groups << group($1)
end
end
groups
def self.statuses
controllers.each_with_object({}) { |controller, statuses| statuses[controller.app_name] = controller.status }
end

def self.status
groups.each_with_object({}) do |group, statuses|
app = group.applications.first
statuses[group.app_name.sub(/\.rb$/, "")] = app && app.running? ? :running : :not_exists
end
def self.start(app_name)
controller(app_name).start
end

def self.stop(app_name)
controller(app_name).stop
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/daemons/rails/version.rb
@@ -1,5 +1,5 @@
module Daemons
module Rails
VERSION = "0.0.3"
VERSION = "1.0.0"
end
end
2 changes: 1 addition & 1 deletion lib/generators/templates/script.rb
Expand Up @@ -3,7 +3,7 @@
# You might want to change this
ENV["RAILS_ENV"] ||= "production"

require File.dirname(__FILE__) + "/../../config/environment"
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "config", "environment"))

$running = true
Signal.trap("TERM") do
Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/config/daemons.yml
@@ -0,0 +1,6 @@
dir_mode: script
dir: "../../log"
multiple: false
backtrace: true
monitor: false
ontop: false
1 change: 1 addition & 0 deletions spec/fixtures/lib/daemons/test.rb
@@ -0,0 +1 @@
#Test script
1 change: 1 addition & 0 deletions spec/fixtures/lib/daemons/test_ctl
@@ -0,0 +1 @@
# Test controller
37 changes: 37 additions & 0 deletions spec/lib/daemons/rails/monitoring_spec.rb
@@ -0,0 +1,37 @@
require "spec_helper"
require "daemons/rails/monitoring"
require "ostruct"

describe Daemons::Rails::Monitoring do
subject { Daemons::Rails::Monitoring }

it "should get list of controllers" do
controllers = subject.controllers
controllers.should have(1).item
controller = controllers[0]
controller.path.should == Rails.root.join('lib', 'daemons', 'test_ctl')
controller.app_name.should == 'test.rb'
end

describe "using controllers" do
before :each do
@controller = Daemons::Rails::Controller.new(Rails.root.join('lib', 'daemons', 'test_ctl'))
subject.stub(:controllers).and_return([@controller])
end

it "should return status for all controllers" do
@controller.should_receive(:run).with('status').and_return('test.rb: running [pid 10880]')
subject.statuses.should == {'test.rb' => :running}
end

it "should start controller by name" do
@controller.should_receive(:run).with('start')
subject.start('test.rb')
end

it "should stop controller by name" do
@controller.should_receive(:run).with('stop')
subject.stop('test.rb')
end
end
end
11 changes: 11 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,11 @@
class Rails
def Rails.root
@root ||= Pathname.new(File.absolute_path(File.dirname(__FILE__))).join('fixtures')
end
end

RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus
end

0 comments on commit 63d11b4

Please sign in to comment.