From 63d11b4329a4469d07e044ab55add9e55100c5a0 Mon Sep 17 00:00:00 2001 From: mirasrael Date: Fri, 13 Jan 2012 17:43:56 +0400 Subject: [PATCH] Refactoring, specs, documentation --- .rspec | 2 ++ README | 24 ++++++++++++++- daemons-rails.gemspec | 1 + lib/daemons/rails/controller.rb | 28 +++++++++++++++++ lib/daemons/rails/monitoring.rb | 35 +++++++++------------ lib/daemons/rails/version.rb | 2 +- lib/generators/templates/script.rb | 2 +- spec/fixtures/config/daemons.yml | 6 ++++ spec/fixtures/lib/daemons/test.rb | 1 + spec/fixtures/lib/daemons/test_ctl | 1 + spec/lib/daemons/rails/monitoring_spec.rb | 37 +++++++++++++++++++++++ spec/spec_helper.rb | 11 +++++++ 12 files changed, 126 insertions(+), 24 deletions(-) create mode 100644 .rspec create mode 100644 lib/daemons/rails/controller.rb create mode 100644 spec/fixtures/config/daemons.yml create mode 100644 spec/fixtures/lib/daemons/test.rb create mode 100644 spec/fixtures/lib/daemons/test_ctl create mode 100644 spec/lib/daemons/rails/monitoring_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..5f16476 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--format progress diff --git a/README b/README index d9c7ba3..5ee7a43 100644 --- a/README +++ b/README @@ -11,6 +11,7 @@ Then insert your code in the lib/daemons/.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:) == CONTROL == @@ -19,6 +20,27 @@ Individual control script: > ./lib/daemons/_ctl [start|stop|restart|status] > rake daemon:[:(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) \ No newline at end of file +> 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 \ No newline at end of file diff --git a/daemons-rails.gemspec b/daemons-rails.gemspec index 1f9354c..ba7d7a7 100644 --- a/daemons-rails.gemspec +++ b/daemons-rails.gemspec @@ -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 diff --git a/lib/daemons/rails/controller.rb b/lib/daemons/rails/controller.rb new file mode 100644 index 0000000..2f4c425 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/lib/daemons/rails/monitoring.rb b/lib/daemons/rails/monitoring.rb index 4ccb5c9..15891f9 100644 --- a/lib/daemons/rails/monitoring.rb +++ b/lib/daemons/rails/monitoring.rb @@ -1,4 +1,6 @@ require "daemons" +require "daemons/rails/config" +require "daemons/rails/controller" module Daemons module Rails @@ -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 diff --git a/lib/daemons/rails/version.rb b/lib/daemons/rails/version.rb index 9a2ce8e..f5f4e81 100644 --- a/lib/daemons/rails/version.rb +++ b/lib/daemons/rails/version.rb @@ -1,5 +1,5 @@ module Daemons module Rails - VERSION = "0.0.3" + VERSION = "1.0.0" end end diff --git a/lib/generators/templates/script.rb b/lib/generators/templates/script.rb index 46ce84d..e525ab0 100644 --- a/lib/generators/templates/script.rb +++ b/lib/generators/templates/script.rb @@ -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 diff --git a/spec/fixtures/config/daemons.yml b/spec/fixtures/config/daemons.yml new file mode 100644 index 0000000..523afd4 --- /dev/null +++ b/spec/fixtures/config/daemons.yml @@ -0,0 +1,6 @@ +dir_mode: script +dir: "../../log" +multiple: false +backtrace: true +monitor: false +ontop: false \ No newline at end of file diff --git a/spec/fixtures/lib/daemons/test.rb b/spec/fixtures/lib/daemons/test.rb new file mode 100644 index 0000000..202a80f --- /dev/null +++ b/spec/fixtures/lib/daemons/test.rb @@ -0,0 +1 @@ +#Test script \ No newline at end of file diff --git a/spec/fixtures/lib/daemons/test_ctl b/spec/fixtures/lib/daemons/test_ctl new file mode 100644 index 0000000..42bec39 --- /dev/null +++ b/spec/fixtures/lib/daemons/test_ctl @@ -0,0 +1 @@ +# Test controller \ No newline at end of file diff --git a/spec/lib/daemons/rails/monitoring_spec.rb b/spec/lib/daemons/rails/monitoring_spec.rb new file mode 100644 index 0000000..2301987 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..55e55ac --- /dev/null +++ b/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