daemons-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ /.idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in daemon_generator.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,20 @@
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
+ > ./lib/daemons/<name>_ctl [start|stop|restart|status]
16
+ > rake daemon:<name> [start|stop|restart|status]
17
+
18
+ App-wide control script:
19
+ > ./script/daemons [start|stop|restart]
20
+ > rake daemons [start|stop|restart|status]
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/daemons/rails/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["mirasrael"]
6
+ gem.email = []
7
+ gem.description = %q{daemons gem support for Rails 3}
8
+ gem.summary = %q{daemons gem support for Rails 3}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "daemons-rails"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Daemons::Rails::VERSION
17
+
18
+ gem.add_dependency 'rails', '~>3.0'
19
+ gem.add_dependency 'daemons'
20
+ gem.add_dependency 'multi_json', '~>1.0'
21
+ gem.add_development_dependency "rake"
22
+ end
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ puts IO.read(File.join(File.dirname(__FILE__), 'README'))
@@ -0,0 +1,15 @@
1
+ require "rails"
2
+ require "daemons/rails/config"
3
+ require "daemons/rails/monitoring"
4
+
5
+ module Daemons
6
+ class Railtie < ::Rails::Railtie
7
+ generators do
8
+ require "generators/daemon_generator.rb"
9
+ end
10
+
11
+ rake_tasks do
12
+ load "tasks/daemons.rake"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ require 'yaml'
2
+ require 'erb'
3
+
4
+ module Daemons
5
+ module Rails
6
+ class Config
7
+ def initialize(app_name, root_path)
8
+ @options = {}
9
+ config_path = File.join(root_path, "config", "#{app_name}-daemon.yml")
10
+ config_path = File.join(root_path, "config", "daemons.yml") unless File.exists?(config_path)
11
+ options = YAML.load(ERB.new(IO.read(config_path)).result)
12
+ options.each { |key, value| @options[key.to_sym] = value }
13
+ @options[:dir_mode] = @options[:dir_mode].to_sym
14
+ @options[:script] ||= File.join(root_path, "lib", "daemons", "#{app_name}.rb")
15
+ end
16
+
17
+ def [](key)
18
+ @options[key]
19
+ end
20
+
21
+ def []=(key, value)
22
+ @options[key] = value
23
+ end
24
+
25
+ def to_hash
26
+ @options
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,44 @@
1
+ require "daemons"
2
+
3
+ module Daemons
4
+ module Rails
5
+ class Monitoring
6
+ def self.daemons_directory=(value)
7
+ @daemons_directory = value
8
+ end
9
+
10
+ def self.daemons_directory
11
+ @daemons_directory ||= ::Rails.root.join('lib', 'daemons')
12
+ end
13
+
14
+ def self.application(app_name)
15
+ group(app_name).applications.first
16
+ end
17
+
18
+ # We do not cache group to be sure we have actual information about running applications
19
+ def self.group(app_name)
20
+ app_config = Daemons::Rails::Config.new(app_name, ::Rails.root)
21
+ group = Daemons::ApplicationGroup.new("#{app_name}.rb", app_config.to_hash)
22
+ group.setup
23
+ group
24
+ end
25
+
26
+ def self.groups
27
+ groups = []
28
+ daemons_directory.each_entry do |file|
29
+ if !file.directory? && file.basename.to_s =~ /(\w+)_ctl/
30
+ groups << group($1)
31
+ end
32
+ end
33
+ groups
34
+ end
35
+
36
+ def self.status
37
+ groups.each_with_object({}) do |group, statuses|
38
+ app = group.applications.first
39
+ statuses[group.app_name.sub(/\.rb$/, "")] = app && app.running? ? :running : :not_exists
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ module Daemons
2
+ module Rails
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ Description:
2
+ Generates a daemon in lib/daemons
3
+
4
+ Example:
5
+ rails generate daemon fraggle
6
+
7
+ This will create:
8
+ lib/daemons/fraggle_ctl
9
+ lib/daemons/fraggle.rb
10
+
11
+ Control the daemon with
12
+ % lib/daemons/fraggle_ctl start|stop|status
13
+
14
+ Control all daemons by invoking:
15
+ % rake daemons:[start|stop|status]
@@ -0,0 +1,22 @@
1
+ require 'rails/generators'
2
+
3
+ class DaemonGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('../templates', __FILE__)
5
+ argument :daemon_name, :type => :string, :default => "application"
6
+
7
+ def generate_daemon
8
+ unless File.exists?(Rails.root.join("script", "daemons"))
9
+ copy_file "daemons", "script/daemons"
10
+ chmod 'script/daemons', 0755
11
+ end
12
+ template "script.rb", "lib/daemons/#{file_name}.rb"
13
+ chmod "lib/daemons/#{file_name}.rb", 0755
14
+
15
+ template "script_ctl", "lib/daemons/#{file_name}_ctl"
16
+ chmod "lib/daemons/#{file_name}_ctl", 0755
17
+
18
+ unless File.exists?(Rails.root.join("config", "daemons.yml"))
19
+ copy_file "daemons.yml", "config/daemons.yml"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ results = []
3
+ Dir[File.dirname(__FILE__) + "/../lib/daemons/*_ctl"].each {|f| results << `#{f} #{ARGV.first}`}
4
+ results.delete_if { |result| result.nil? || result.empty? }
5
+ puts results.join("\n") unless results.empty?
@@ -0,0 +1,6 @@
1
+ dir_mode: script
2
+ dir: ../../log
3
+ multiple: false
4
+ backtrace: true
5
+ monitor: false
6
+ ontop: false
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # You might want to change this
4
+ ENV["RAILS_ENV"] ||= "development"
5
+
6
+ require File.dirname(__FILE__) + "/../../config/application"
7
+ Rails.application.require_environment!
8
+
9
+ $running = true
10
+ Signal.trap("TERM") do
11
+ $running = false
12
+ end
13
+
14
+ while($running) do
15
+
16
+ # Replace this with your code
17
+ Rails.logger.auto_flushing = true
18
+ Rails.logger.info "This daemon is still running at #{Time.now}.\n"
19
+
20
+ sleep 10
21
+ end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require "daemons"
4
+ require "daemons/rails/config"
5
+
6
+ config = Daemons::Rails::Config.new("<%= file_name %>", File.dirname(__FILE__) + "/../../")
7
+
8
+ Daemons.run File.dirname(__FILE__) + "/<%= file_name %>.rb", config.to_hash
@@ -0,0 +1,27 @@
1
+ require 'rake'
2
+
3
+ desc "Show status of daemons"
4
+ task :daemons => "daemons:status"
5
+
6
+ namespace :daemons do
7
+ %w[start stop status].each do |arg|
8
+ desc "#{arg.capitalize} all daemons."
9
+ task :"#{arg}" do
10
+ puts `script/daemons #{arg}`
11
+ end
12
+ end
13
+ end
14
+
15
+ namespace :daemon do
16
+ Dir['lib/daemons/*_ctl'].each do |controller|
17
+ app_name = controller.sub(/.*\/(\w+)_ctl/, '\1')
18
+ namespace app_name do
19
+ %w[start stop status].each do |arg|
20
+ desc "#{arg.capitalize} #{app_name} daemons."
21
+ task :"#{arg}" do
22
+ puts `#{controller} #{arg}`
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: daemons-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - mirasrael
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-01 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &23654780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *23654780
25
+ - !ruby/object:Gem::Dependency
26
+ name: daemons
27
+ requirement: &23654160 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *23654160
36
+ - !ruby/object:Gem::Dependency
37
+ name: multi_json
38
+ requirement: &23653460 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '1.0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *23653460
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &23653000 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *23653000
58
+ description: daemons gem support for Rails 3
59
+ email: []
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - .gitignore
65
+ - Gemfile
66
+ - README
67
+ - Rakefile
68
+ - daemons-rails.gemspec
69
+ - install.rb
70
+ - lib/daemons-rails.rb
71
+ - lib/daemons/rails/config.rb
72
+ - lib/daemons/rails/monitoring.rb
73
+ - lib/daemons/rails/version.rb
74
+ - lib/generators/USAGE
75
+ - lib/generators/daemon_generator.rb
76
+ - lib/generators/templates/daemons
77
+ - lib/generators/templates/daemons.yml
78
+ - lib/generators/templates/script.rb
79
+ - lib/generators/templates/script_ctl
80
+ - lib/tasks/daemons.rake
81
+ homepage: ''
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.10
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: daemons gem support for Rails 3
105
+ test_files: []