daemons-rails 1.0.0 → 1.1.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,66 @@
1
+ Daemons Rails support (based on http://github.com/dougal/daemon_generator)
2
+ ================
3
+
4
+ To get it work just add dependency to this gem in your Gemfile.
5
+
6
+ ## GENERATOR ##
7
+
8
+ rails generate daemon <name>
9
+
10
+ Then insert your code in the lib/daemons/\<name\>.rb stub. All pids and logs will live in the normal log/ folder. This helps to make things Capistrano friendly.
11
+
12
+ ## CONTROL ##
13
+
14
+ Individual control script:
15
+
16
+ ./lib/daemons/<name>_ctl [start|stop|restart|status]
17
+ rake daemon:<name>[:(start|stop|status)]
18
+
19
+ Examples:
20
+
21
+ rake daemon:test - runs lib/daemons/test.rb not daemonized
22
+ rake daemon:test:start - start daemon using lib/daemons/test_ctl start
23
+ rake daemon:test:stop - stop daemon using lib/daemons/test_ctl stop
24
+ rake daemon:test:status - show running status for daemon using lib/daemons/test_ctl status
25
+
26
+ App-wide control script:
27
+
28
+ ./script/daemons [start|stop|restart]
29
+ rake daemons:(start|stop|status)
30
+
31
+ ## MONITORING API ##
32
+
33
+ Daemons::Rails::Monitoring.statuses - hash with all daemons and corresponding statuses
34
+ Daemons::Rails::Monitoring.start("test.rb") - start daemon using lib/daemons/test_ctl start
35
+ Daemons::Rails::Monitoring.stop("test.rb") - start daemon using lib/daemons/test_ctl stop
36
+ Daemons::Rails::Monitoring.controllers - list of controllers
37
+ Daemons::Rails::Monitoring.controller("test.rb") - controller for test.rb application
38
+
39
+ controller = Daemons::Rails::Monitoring.controller("test.rb")
40
+ controller.path # => lib/daemons/test_ctl
41
+ controller.app_name # => test.rb
42
+ controller.start # => starts daemon
43
+ controller.stop # => stops daemon
44
+ controller.status # => :not_exists or :running
45
+
46
+ ## CONFIGURATION ##
47
+
48
+ You can set default settings for your daemons into config/daemons.yml file. Full list of options you can get from documentation to Daemons.daemonize method (http://daemons.rubyforge.org/classes/Daemons.html#M000007). Also it possible to set individual daemon options using file config/\<daemon_name\>-daemon.yml.
49
+ If you want to use directory other than default lib/daemons then you should add to application initialization block following lines:
50
+
51
+ class MyApp < Rails::Application
52
+ ...
53
+ Daemons::Rails.configure do |c|
54
+ c.daemons_path = Rails.root.join("new_daemons_path")
55
+ end
56
+ ...
57
+ end
58
+
59
+ If you change your mind, you can easily move content of this directory to other place and change config.
60
+ Notice: this feature available only from version 1.1 and old generated daemons can't be free moved, because uses hard-coded path to lib/daemons. So, you can generate daemons with same names and then move client code to generated templates.
61
+
62
+ ## CHANGES ##
63
+
64
+ * 1.1.0 - supported custom directory for daemons
65
+ * 1.0.0 - changed api for Daemons::Rails::Monitoring, fixed path in template for script, improved documentation, added RSpec
66
+ * 0.0.3 - added rake for running script without daemonization (rake daemon:\<name\>)
@@ -0,0 +1,14 @@
1
+ require "daemons/rails/configuration"
2
+
3
+ module Daemons
4
+ module Rails
5
+ # @return [Daemons::Rails::Configuration]
6
+ def self.configuration
7
+ @configuration ||= Daemons::Rails::Configuration.new
8
+ end
9
+
10
+ def self.configure
11
+ yield configuration
12
+ end
13
+ end
14
+ end
@@ -4,14 +4,14 @@ require 'erb'
4
4
  module Daemons
5
5
  module Rails
6
6
  class Config
7
- def initialize(app_name, root_path)
7
+ def initialize(app_name, root_path, daemons_dir = File.join('lib', 'daemons'))
8
8
  @options = {}
9
9
  config_path = File.join(root_path, "config", "#{app_name}-daemon.yml")
10
10
  config_path = File.join(root_path, "config", "daemons.yml") unless File.exists?(config_path)
11
11
  options = YAML.load(ERB.new(IO.read(config_path)).result)
12
12
  options.each { |key, value| @options[key.to_sym] = value }
13
13
  @options[:dir_mode] = @options[:dir_mode].to_sym
14
- @options[:script] ||= File.join(root_path, "lib", "daemons", "#{app_name}.rb")
14
+ @options[:script] ||= File.join(root_path, daemons_dir, "#{app_name}.rb")
15
15
  end
16
16
 
17
17
  def [](key)
@@ -0,0 +1,37 @@
1
+ module Daemons
2
+ module Rails
3
+ class Configuration
4
+ def detect_root
5
+ if defined?(::Rails)
6
+ ::Rails.root
7
+ else
8
+ root = Pathname.new(FileUtils.pwd)
9
+ root = root.parent unless root.directory?
10
+ root = root.parent until File.exists?(root.join('config.ru')) || root.root?
11
+ raise "Can't detect Rails application root" if root.root?
12
+ root
13
+ end
14
+ end
15
+
16
+ def daemons_path=(path)
17
+ @daemons_path = path && (path.is_a?(Pathname) ? path : Pathname.new(File.expand_path(path)))
18
+ end
19
+
20
+ def root=(path)
21
+ @root = path && (path.is_a?(Pathname) ? path : Pathname.new(File.expand_path(path)))
22
+ end
23
+
24
+ def root
25
+ @root ||= detect_root
26
+ end
27
+
28
+ def daemons_path
29
+ @daemons_path || root.join('lib', 'daemons')
30
+ end
31
+
32
+ def daemons_directory
33
+ daemons_path.relative_path_from(root)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,4 +1,5 @@
1
1
  require "daemons"
2
+ require "daemons/rails"
2
3
  require "daemons/rails/config"
3
4
  require "daemons/rails/controller"
4
5
 
@@ -10,7 +11,7 @@ module Daemons
10
11
  end
11
12
 
12
13
  def self.daemons_directory
13
- @daemons_directory ||= ::Rails.root.join('lib', 'daemons')
14
+ @daemons_directory || Daemons::Rails.configuration.daemons_path
14
15
  end
15
16
 
16
17
  def self.controller(app_name)
@@ -1,5 +1,5 @@
1
1
  module Daemons
2
2
  module Rails
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0.alpha"
4
4
  end
5
5
  end
@@ -1,19 +1,23 @@
1
1
  require 'rails/generators'
2
+ require 'daemons/rails'
2
3
 
3
4
  class DaemonGenerator < Rails::Generators::NamedBase
4
5
  source_root File.expand_path('../templates', __FILE__)
5
6
  argument :daemon_name, :type => :string, :default => "application"
6
7
 
7
8
  def generate_daemon
8
- unless File.exists?(Rails.root.join("script", "daemons"))
9
- copy_file "daemons", "script/daemons"
10
- chmod 'script/daemons', 0755
9
+ daemons_dir = Daemons::Rails.configuration.daemons_directory
10
+
11
+ unless File.exists?(Rails.root.join(daemons_dir, 'daemons'))
12
+ copy_file "daemons", daemons_dir.join('daemons')
13
+ chmod daemons_dir.join('daemons'), 0755
11
14
  end
12
- template "script.rb", "lib/daemons/#{file_name}.rb"
13
- chmod "lib/daemons/#{file_name}.rb", 0755
14
15
 
15
- template "script_ctl", "lib/daemons/#{file_name}_ctl"
16
- chmod "lib/daemons/#{file_name}_ctl", 0755
16
+ template "script.rb", daemons_dir.join("#{file_name}.rb")
17
+ chmod daemons_dir.join("#{file_name}.rb"), 0755
18
+
19
+ template "script_ctl", daemons_dir.join("#{file_name}_ctl")
20
+ chmod daemons_dir.join("#{file_name}_ctl"), 0755
17
21
 
18
22
  unless File.exists?(Rails.root.join("config", "daemons.yml"))
19
23
  copy_file "daemons.yml", "config/daemons.yml"
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
  results = []
3
- Dir[File.dirname(__FILE__) + "/../lib/daemons/*_ctl"].each {|f| results << `#{f} #{ARGV.first}`}
3
+ Dir[File.dirname(__FILE__) + "/*_ctl"].each {|f| results << `ruby #{f} #{ARGV.first}`}
4
4
  results.delete_if { |result| result.nil? || result.empty? }
5
5
  puts results.join unless results.empty?
@@ -3,7 +3,9 @@
3
3
  # You might want to change this
4
4
  ENV["RAILS_ENV"] ||= "production"
5
5
 
6
- require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "config", "environment"))
6
+ root = File.expand_path(File.dirname(__FILE__))
7
+ root = File.dirname(root) until File.exists?(File.join(root, 'config'))
8
+ require File.join(root, "config", "environment")
7
9
 
8
10
  $running = true
9
11
  Signal.trap("TERM") do
@@ -2,7 +2,8 @@
2
2
  require 'rubygems'
3
3
  require "daemons"
4
4
  require "daemons/rails/config"
5
+ require "daemons/rails"
5
6
 
6
- config = Daemons::Rails::Config.new("<%= file_name %>", File.dirname(__FILE__) + "/../../")
7
+ config = Daemons::Rails::Config.new("<%= file_name %>", Daemons::Rails.configuration.root, Daemons::Rails.configuration.daemons_directory)
7
8
 
8
9
  Daemons.run File.dirname(__FILE__) + "/<%= file_name %>.rb", config.to_hash
@@ -3,21 +3,23 @@ require 'rake'
3
3
  desc "Show status of daemons"
4
4
  task :daemons => "daemons:status"
5
5
 
6
+ daemons_dir = Daemons::Rails.configuration.daemons_directory
7
+
6
8
  namespace :daemons do
7
9
  %w[start stop status].each do |arg|
8
10
  desc "#{arg.capitalize} all daemons."
9
11
  task :"#{arg}" do
10
- puts `script/daemons #{arg}`
12
+ puts `#{daemons_dir}/daemons #{arg}`
11
13
  end
12
14
  end
13
15
  end
14
16
 
15
17
  namespace :daemon do
16
- Dir['lib/daemons/*_ctl'].each do |controller|
18
+ Dir[daemons_dir.join('*_ctl')].each do |controller|
17
19
  app_name = controller.sub(/.*\/(\w+)_ctl/, '\1')
18
20
  desc "Start #{app_name} script"
19
21
  task app_name do
20
- FileUtils.cd 'lib/daemons' do
22
+ FileUtils.cd daemons_dir do
21
23
  load "#{app_name}.rb"
22
24
  end
23
25
  end
File without changes
@@ -0,0 +1 @@
1
+ #Test 2 script
@@ -0,0 +1 @@
1
+ # Test 2 controller
@@ -0,0 +1,52 @@
1
+ require "spec_helper"
2
+ require "daemons/rails/configuration"
3
+ require "daemons/rails/monitoring"
4
+ require "daemons/rails"
5
+
6
+ describe Daemons::Rails::Configuration do
7
+ subject { Daemons::Rails.configuration }
8
+
9
+ describe "Default configuration" do
10
+ describe "rails env" do
11
+ its(:root) { should == Rails.root }
12
+ its(:daemons_path) { should == Rails.root.join('lib', 'daemons') }
13
+ its(:daemons_directory) { should == Pathname.new('lib').join('daemons') }
14
+ end
15
+
16
+ describe "no rails" do
17
+ before :all do
18
+ Dir.chdir Rails.root
19
+ Object.const_set :Rails_, Rails
20
+ Object.send :remove_const, :Rails
21
+ end
22
+ after :all do
23
+ Object.const_set :Rails, Rails_
24
+ Object.send :remove_const, :Rails_
25
+ Dir.chdir Rails.root.parent.parent
26
+ end
27
+ its(:root) { should == Rails_.root }
28
+ its(:daemons_path) { should == Rails_.root.join('lib', 'daemons') }
29
+ its(:daemons_directory) { should == Pathname.new('lib').join('daemons') }
30
+ end
31
+ end
32
+
33
+ describe "Overridden daemons directory" do
34
+ around :each do |example|
35
+ Daemons::Rails.configure do |c|
36
+ c.daemons_path = Rails.root.join('daemons')
37
+ end
38
+ example.run
39
+ Daemons::Rails.configure do |c|
40
+ c.daemons_path = nil
41
+ end
42
+ end
43
+
44
+ its(:daemons_path) { should == Rails.root.join('daemons') }
45
+ its(:daemons_directory) { should == Pathname.new('daemons') }
46
+
47
+ it "should override daemons directory" do
48
+ Daemons::Rails::Monitoring.daemons_directory.should == Rails.root.join('daemons')
49
+ Daemons::Rails::Monitoring.controllers.map(&:app_name).should == %w(test2.rb)
50
+ end
51
+ end
52
+ end
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: daemons-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
5
- prerelease:
4
+ version: 1.1.0.alpha
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - mirasrael
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-13 00:00:00.000000000Z
12
+ date: 2012-08-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &14263920 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *14263920
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: daemons
27
- requirement: &14260740 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *14260740
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: multi_json
38
- requirement: &14257180 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ~>
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '1.0'
44
54
  type: :runtime
45
55
  prerelease: false
46
- version_requirements: *14257180
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: rake
49
- requirement: &14233540 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,10 +69,15 @@ dependencies:
54
69
  version: '0'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *14233540
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  - !ruby/object:Gem::Dependency
59
79
  name: rspec
60
- requirement: &14228840 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
61
81
  none: false
62
82
  requirements:
63
83
  - - ~>
@@ -65,7 +85,12 @@ dependencies:
65
85
  version: 2.7.0
66
86
  type: :development
67
87
  prerelease: false
68
- version_requirements: *14228840
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 2.7.0
69
94
  description: daemons gem support for Rails 3
70
95
  email: []
71
96
  executables: []
@@ -75,12 +100,14 @@ files:
75
100
  - .gitignore
76
101
  - .rspec
77
102
  - Gemfile
78
- - README
103
+ - README.markdown
79
104
  - Rakefile
80
105
  - daemons-rails.gemspec
81
106
  - install.rb
82
107
  - lib/daemons-rails.rb
108
+ - lib/daemons/rails.rb
83
109
  - lib/daemons/rails/config.rb
110
+ - lib/daemons/rails/configuration.rb
84
111
  - lib/daemons/rails/controller.rb
85
112
  - lib/daemons/rails/monitoring.rb
86
113
  - lib/daemons/rails/version.rb
@@ -91,9 +118,13 @@ files:
91
118
  - lib/generators/templates/script.rb
92
119
  - lib/generators/templates/script_ctl
93
120
  - lib/tasks/daemons.rake
121
+ - spec/fixtures/config.ru
94
122
  - spec/fixtures/config/daemons.yml
123
+ - spec/fixtures/daemons/test2.rb
124
+ - spec/fixtures/daemons/test2_ctl
95
125
  - spec/fixtures/lib/daemons/test.rb
96
126
  - spec/fixtures/lib/daemons/test_ctl
127
+ - spec/lib/daemons/rails/configuration_spec.rb
97
128
  - spec/lib/daemons/rails/monitoring_spec.rb
98
129
  - spec/spec_helper.rb
99
130
  homepage: ''
@@ -108,22 +139,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
139
  - - ! '>='
109
140
  - !ruby/object:Gem::Version
110
141
  version: '0'
111
- segments:
112
- - 0
113
- hash: -1263531604415930102
114
142
  required_rubygems_version: !ruby/object:Gem::Requirement
115
143
  none: false
116
144
  requirements:
117
- - - ! '>='
145
+ - - ! '>'
118
146
  - !ruby/object:Gem::Version
119
- version: '0'
120
- segments:
121
- - 0
122
- hash: -1263531604415930102
147
+ version: 1.3.1
123
148
  requirements: []
124
149
  rubyforge_project:
125
- rubygems_version: 1.8.10
150
+ rubygems_version: 1.8.24
126
151
  signing_key:
127
152
  specification_version: 3
128
153
  summary: daemons gem support for Rails 3
129
154
  test_files: []
155
+ has_rdoc:
data/README DELETED
@@ -1,46 +0,0 @@
1
- Daemons Rails support (based on http://github.com/dougal/daemon_generator)
2
- ================
3
-
4
- To get it work just add dependency to this gem in your Gemfile.
5
-
6
- == GENERATOR ==
7
-
8
- rails generate daemon <name>
9
-
10
- Then insert your code in the lib/daemons/<name>.rb stub. All pids and logs will live in the normal log/ folder. This helps to make things Capistrano friendly.
11
-
12
- == CHANGES ==
13
-
14
- 1.0.0 - changed api for Daemons::Rails::Monitoring, fixed path in template for script, improved documentation, added RSpec
15
- 0.0.3 - added rake for running script without daemonization (rake daemon:<name>)
16
-
17
- == CONTROL ==
18
-
19
- Individual control script:
20
- > ./lib/daemons/<name>_ctl [start|stop|restart|status]
21
- > rake daemon:<name>[:(start|stop|status)]
22
-
23
- Examples:
24
- rake daemon:test - runs lib/daemons/test.rb not daemonized
25
- rake daemon:test:start - start daemon using lib/daemons/test_ctl start
26
- rake daemon:test:stop - stop daemon using lib/daemons/test_ctl stop
27
- rake daemon:test:status - show running status for daemon using lib/daemons/test_ctl status
28
-
29
- App-wide control script:
30
- > ./script/daemons [start|stop|restart]
31
- > rake daemons:(start|stop|status)
32
-
33
- == MONITORING API ==
34
-
35
- Daemons::Rails::Monitoring.statuses - hash with all daemons and corresponding statuses
36
- Daemons::Rails::Monitoring.start("test.rb") - start daemon using lib/daemons/test_ctl start
37
- Daemons::Rails::Monitoring.stop("test.rb") - start daemon using lib/daemons/test_ctl stop
38
- Daemons::Rails::Monitoring.controllers - list of controllers
39
- Daemons::Rails::Monitoring.controller("test.rb") - controller for test.rb application
40
-
41
- controller = Daemons::Rails::Monitoring.controller("test.rb")
42
- controller.path # => lib/daemons/test_ctl
43
- controller.app_name # => test.rb
44
- controller.start # => starts daemon
45
- controller.stop # => stops daemon
46
- controller.status # => :not_exists or :running