kennethkalmer-daemon-kit 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Manifest.txt +9 -0
- data/TODO.txt +8 -1
- data/app_generators/daemon_kit/daemon_kit_generator.rb +7 -7
- data/daemon_generators/nanite_agent/USAGE +5 -0
- data/daemon_generators/nanite_agent/nanite_agent_generator.rb +68 -0
- data/daemon_generators/nanite_agent/templates/config/initializers/nanite_agent.rb +6 -0
- data/daemon_generators/nanite_agent/templates/config/nanite.yml +35 -0
- data/daemon_generators/nanite_agent/templates/lib/actors/sample.rb +11 -0
- data/daemon_generators/nanite_agent/templates/libexec/daemon.rb +31 -0
- data/lib/daemon_kit/nanite/agent.rb +60 -0
- data/lib/daemon_kit/nanite.rb +7 -0
- data/lib/daemon_kit.rb +2 -1
- data/test/test_nanite_agent_generator.rb +49 -0
- metadata +12 -2
data/Manifest.txt
CHANGED
@@ -31,12 +31,20 @@ daemon_generators/jabber/jabber_generator.rb
|
|
31
31
|
daemon_generators/jabber/templates/config/initializers/jabber.rb
|
32
32
|
daemon_generators/jabber/templates/config/jabber.yml
|
33
33
|
daemon_generators/jabber/templates/libexec/daemon.rb
|
34
|
+
daemon_generators/nanite_agent/USAGE
|
35
|
+
daemon_generators/nanite_agent/nanite_agent_generator.rb
|
36
|
+
daemon_generators/nanite_agent/templates/config/initializers/nanite_agent.rb
|
37
|
+
daemon_generators/nanite_agent/templates/config/nanite.yml
|
38
|
+
daemon_generators/nanite_agent/templates/lib/actors/sample.rb
|
39
|
+
daemon_generators/nanite_agent/templates/libexec/daemon.rb
|
34
40
|
lib/daemon_kit.rb
|
35
41
|
lib/daemon_kit/amqp.rb
|
36
42
|
lib/daemon_kit/application.rb
|
37
43
|
lib/daemon_kit/cron.rb
|
38
44
|
lib/daemon_kit/initializer.rb
|
39
45
|
lib/daemon_kit/jabber.rb
|
46
|
+
lib/daemon_kit/nanite.rb
|
47
|
+
lib/daemon_kit/nanite/agent.rb
|
40
48
|
lib/daemon_kit/patches/force_kill_wait.rb
|
41
49
|
lib/daemon_kit/tasks.rb
|
42
50
|
lib/daemon_kit/tasks/framework.rake
|
@@ -60,3 +68,4 @@ test/test_cron_generator.rb
|
|
60
68
|
test/test_daemon-kit_generator.rb
|
61
69
|
test/test_generator_helper.rb
|
62
70
|
test/test_jabber_generator.rb
|
71
|
+
test/test_nanite_agent_generator.rb
|
data/TODO.txt
CHANGED
@@ -3,7 +3,7 @@ DaemonKit TODO List
|
|
3
3
|
|
4
4
|
This is purely a drop in the bucket of what has to come...
|
5
5
|
|
6
|
-
* [DONE] Easy way to trap signals
|
6
|
+
* [DONE] Easy way to trap signals
|
7
7
|
* Error handling to the degree Rails does
|
8
8
|
* Easy configuration of an ORM of choice, including patching it if needed (ActiveRecord *cough*)
|
9
9
|
* Improved generators for creating skeleton daemons:
|
@@ -20,5 +20,12 @@ This is purely a drop in the bucket of what has to come...
|
|
20
20
|
* Improved and cleaned up logging
|
21
21
|
* Plenty of docs, seriously a lot of docs
|
22
22
|
* Specs & features, tons of them too
|
23
|
+
* Integration tests for the specific daemons
|
23
24
|
* Some level of thread safety, or mechanisms to ease thread safety
|
25
|
+
* Built in utilities:
|
26
|
+
* Thread pool for 1.8
|
27
|
+
* Fibre pool for 1.9
|
28
|
+
* Some activesupport-esque functions until activesupport 3.0 hits the streets
|
29
|
+
* DRY up the following:
|
30
|
+
* Loading configuration files for the daemons
|
24
31
|
* DON'T FORGET 1.9 SUPPORT
|
@@ -3,8 +3,8 @@ class DaemonKitGenerator < RubiGen::Base
|
|
3
3
|
DEFAULT_SHEBANG = File.join(Config::CONFIG['bindir'],
|
4
4
|
Config::CONFIG['ruby_install_name'])
|
5
5
|
|
6
|
-
VALID_GENERATORS = ['default', 'jabber', 'cron', 'amqp']
|
7
|
-
|
6
|
+
VALID_GENERATORS = ['default', 'jabber', 'cron', 'amqp', 'nanite_agent']
|
7
|
+
|
8
8
|
default_options :shebang => DEFAULT_SHEBANG, :author => nil
|
9
9
|
|
10
10
|
attr_reader :daemon_name
|
@@ -25,9 +25,9 @@ class DaemonKitGenerator < RubiGen::Base
|
|
25
25
|
$stderr.puts "Valid generators are: #{VALID_GENERATORS.join(', ')}"
|
26
26
|
exit 1
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
script_options = { :chmod => 0755, :shebang => options[:shebang] == DEFAULT_SHEBANG ? nil : options[:shebang] }
|
30
|
-
|
30
|
+
|
31
31
|
record do |m|
|
32
32
|
# Ensure appropriate folder(s) exists
|
33
33
|
m.directory ''
|
@@ -37,11 +37,11 @@ class DaemonKitGenerator < RubiGen::Base
|
|
37
37
|
# m.template_copy_each ["template.rb", "template2.rb"]
|
38
38
|
# m.file "file", "some_file_copied"
|
39
39
|
# m.file_copy_each ["path/to/file", "path/to/file2"]
|
40
|
-
|
40
|
+
|
41
41
|
# Readme
|
42
42
|
m.template "README", "README"
|
43
43
|
m.template "Rakefile", "Rakefile"
|
44
|
-
|
44
|
+
|
45
45
|
# Executable
|
46
46
|
m.directory "bin"
|
47
47
|
m.template "bin/daemon.erb", "bin/#{daemon_name}", script_options
|
@@ -65,7 +65,7 @@ class DaemonKitGenerator < RubiGen::Base
|
|
65
65
|
|
66
66
|
# Libraries
|
67
67
|
m.directory "lib"
|
68
|
-
|
68
|
+
|
69
69
|
# Tests
|
70
70
|
m.directory "tasks"
|
71
71
|
m.dependency "install_rspec", [daemon_name], :destination => destination_root, :collision => :force
|
@@ -0,0 +1,68 @@
|
|
1
|
+
class NaniteAgentGenerator < RubiGen::Base
|
2
|
+
|
3
|
+
default_options :author => nil
|
4
|
+
|
5
|
+
attr_reader :name
|
6
|
+
|
7
|
+
def initialize(runtime_args, runtime_options = {})
|
8
|
+
super
|
9
|
+
usage if args.empty?
|
10
|
+
@name = args.shift
|
11
|
+
extract_options
|
12
|
+
end
|
13
|
+
|
14
|
+
def manifest
|
15
|
+
record do |m|
|
16
|
+
# Ensure appropriate folder(s) exists
|
17
|
+
m.directory ''
|
18
|
+
|
19
|
+
# Create stubs
|
20
|
+
# m.template "template.rb.erb", "some_file_after_erb.rb"
|
21
|
+
# m.template_copy_each ["template.rb", "template2.rb"]
|
22
|
+
# m.template_copy_each ["template.rb", "template2.rb"], "some/path"
|
23
|
+
# m.file "file", "some_file_copied"
|
24
|
+
# m.file_copy_each ["path/to/file", "path/to/file2"]
|
25
|
+
# m.file_copy_each ["path/to/file", "path/to/file2"], "some/path"
|
26
|
+
|
27
|
+
# Copy over our configs
|
28
|
+
m.directory 'lib'
|
29
|
+
m.directory 'lib/actors'
|
30
|
+
m.file 'lib/actors/sample.rb', 'lib/actors/sample.rb'
|
31
|
+
m.directory 'config'
|
32
|
+
m.template 'config/nanite.yml', 'config/nanite.yml'
|
33
|
+
m.directory 'config/initializers'
|
34
|
+
m.template 'config/initializers/nanite_agent.rb', "config/initializers/#{name}.rb"
|
35
|
+
|
36
|
+
# Copy over our daemon
|
37
|
+
m.directory 'libexec'
|
38
|
+
m.template 'libexec/daemon.rb', "libexec/#{name}.rb"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
protected
|
43
|
+
def banner
|
44
|
+
<<-EOS
|
45
|
+
Creates a ...
|
46
|
+
|
47
|
+
USAGE: #{$0} #{spec.name} name
|
48
|
+
EOS
|
49
|
+
end
|
50
|
+
|
51
|
+
def add_options!(opts)
|
52
|
+
# opts.separator ''
|
53
|
+
# opts.separator 'Options:'
|
54
|
+
# For each option below, place the default
|
55
|
+
# at the top of the file next to "default_options"
|
56
|
+
# opts.on("-a", "--author=\"Your Name\"", String,
|
57
|
+
# "Some comment about this option",
|
58
|
+
# "Default: none") { |o| options[:author] = o }
|
59
|
+
# opts.on("-v", "--version", "Show the #{File.basename($0)} version number and quit.")
|
60
|
+
end
|
61
|
+
|
62
|
+
def extract_options
|
63
|
+
# for each option, extract it into a local variable (and create an "attr_reader :author" at the top)
|
64
|
+
# Templates can access these value via the attr_reader-generated methods, but not the
|
65
|
+
# raw instance variable value.
|
66
|
+
# @author = options[:author]
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# Nanite agent configuration file
|
2
|
+
|
3
|
+
# These values will be used to configure the nanite agent, any values
|
4
|
+
# omitted will let the gem use it's own defaults.
|
5
|
+
|
6
|
+
# The configuration specifies the following keys:
|
7
|
+
# * user - Specify the rabbitmq username
|
8
|
+
# * pass - Specify the rabbitmq password
|
9
|
+
# * host - Specify the rabbitmq hostname
|
10
|
+
# * vhost - Specify the rabbitmq vhost
|
11
|
+
# * port - Specify the rabbitmq PORT, default 5672
|
12
|
+
# * token - Specify the agent identity
|
13
|
+
# * security - Use Security features of rabbitmq to restrict nanites to themselves
|
14
|
+
# * format - The serialization type to use for transfering data. Can be marshal, json or yaml. Default is marshal
|
15
|
+
# * tag - YAML array of tags
|
16
|
+
# * ping-time - Specify how often the agents contacts the mapper
|
17
|
+
# * actors - Comma separated list of actors to load (all ruby files in actors directory by default)
|
18
|
+
|
19
|
+
defaults: &defaults
|
20
|
+
username: nanite
|
21
|
+
password: testing
|
22
|
+
host: localhost
|
23
|
+
vhost: /nanite
|
24
|
+
# tag:
|
25
|
+
# - foo
|
26
|
+
# - bar
|
27
|
+
|
28
|
+
development:
|
29
|
+
<<: *defaults
|
30
|
+
|
31
|
+
test:
|
32
|
+
<<: *defaults
|
33
|
+
|
34
|
+
production:
|
35
|
+
<<: *defaults
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# Generated nanite agent daemon
|
2
|
+
|
3
|
+
# Do your post daemonization configuration here
|
4
|
+
# At minimum you need just the first line (without the block), or a lot
|
5
|
+
# of strange things might start happening...
|
6
|
+
DaemonKit::Application.running! do |config|
|
7
|
+
# Trap signals with blocks or procs
|
8
|
+
# config.trap( 'INT' ) do
|
9
|
+
# # do something clever
|
10
|
+
# end
|
11
|
+
# config.trap( 'TERM', Proc.new { puts 'Going down' } )
|
12
|
+
end
|
13
|
+
|
14
|
+
# IMPORTANT CONFIGURATION NOTE
|
15
|
+
#
|
16
|
+
# Please review and update 'config/nanite.yml' accordingly or this
|
17
|
+
# daemon won't work as advertised.
|
18
|
+
#
|
19
|
+
# Your actors live in DAEMON_ROOT/lib/actors
|
20
|
+
|
21
|
+
# Run the agent, and get the running agent.
|
22
|
+
DaemonKit::Nanite::Agent.run do |agent|
|
23
|
+
# Use the yielded agent instance to register your actors:
|
24
|
+
agent.register Sample.new
|
25
|
+
|
26
|
+
# This block can used to make your agent perform other tasks as
|
27
|
+
# well. Remember that you have access to a running EventMachine
|
28
|
+
# reactor since the AMQP gem used by nanite uses it. Other than that
|
29
|
+
# you can mostly leave this file alone and concentrate on developing
|
30
|
+
# your actors in the lib/actors/ directory of the project.
|
31
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module DaemonKit
|
2
|
+
module Nanite
|
3
|
+
# Pull support into a daemon for being a nanite agent.
|
4
|
+
class Agent
|
5
|
+
|
6
|
+
@@instance = nil
|
7
|
+
|
8
|
+
class << self
|
9
|
+
|
10
|
+
def instance
|
11
|
+
@instance ||= (
|
12
|
+
config = YAML.load_file( "#{DAEMON_ROOT}/config/nanite.yml" )[DAEMON_ENV]
|
13
|
+
raise ArgumentError, "Missing nanite configuration for #{DAEMON_ENV} environment" if config.nil?
|
14
|
+
new( config )
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
private :new
|
19
|
+
|
20
|
+
def run(&block)
|
21
|
+
instance.run(&block)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize( options = {} )
|
27
|
+
@config = options.inject({}) { |m,c| m[c[0].to_sym] = c[1]; m } # symbolize_keys
|
28
|
+
|
29
|
+
config_agent
|
30
|
+
end
|
31
|
+
|
32
|
+
def run(&block)
|
33
|
+
# Ensure graceful shutdown of the connection to the broker
|
34
|
+
DaemonKit.trap('INT') { ::EM.stop }
|
35
|
+
DaemonKit.trap('TERM') { ::EM.stop }
|
36
|
+
|
37
|
+
# Start our mapper
|
38
|
+
mapper_thread = Thread.new do
|
39
|
+
EM.run do
|
40
|
+
agent = ::Nanite.start_agent( @config )
|
41
|
+
block.call( agent ) if block
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
#block.call if block
|
46
|
+
|
47
|
+
mapper_thread.join
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
# Make sure to fine tune the agent config to be DK friendly
|
53
|
+
def config_agent
|
54
|
+
@config[:root] = DAEMON_ROOT
|
55
|
+
@config[:daemonize] = false
|
56
|
+
@config[:actors_dir] = File.join(DAEMON_ROOT, 'lib', 'actors')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/daemon_kit.rb
CHANGED
@@ -4,11 +4,12 @@ $:.unshift(File.dirname(__FILE__)) unless
|
|
4
4
|
require 'rubygems'
|
5
5
|
|
6
6
|
module DaemonKit
|
7
|
-
VERSION = '0.1.
|
7
|
+
VERSION = '0.1.4'
|
8
8
|
|
9
9
|
autoload :Initializer, 'daemon_kit/initializer'
|
10
10
|
autoload :Application, 'daemon_kit/application'
|
11
11
|
autoload :Cron, 'daemon_kit/cron'
|
12
12
|
autoload :Jabber, 'daemon_kit/jabber'
|
13
13
|
autoload :AMQP, 'daemon_kit/amqp'
|
14
|
+
autoload :Nanite, 'daemon_kit/nanite'
|
14
15
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_generator_helper.rb")
|
2
|
+
|
3
|
+
|
4
|
+
class TestNaniteAgentGenerator < Test::Unit::TestCase
|
5
|
+
include RubiGen::GeneratorTestHelper
|
6
|
+
|
7
|
+
def setup
|
8
|
+
bare_setup
|
9
|
+
end
|
10
|
+
|
11
|
+
def teardown
|
12
|
+
bare_teardown
|
13
|
+
end
|
14
|
+
|
15
|
+
# Some generator-related assertions:
|
16
|
+
# assert_generated_file(name, &block) # block passed the file contents
|
17
|
+
# assert_directory_exists(name)
|
18
|
+
# assert_generated_class(name, &block)
|
19
|
+
# assert_generated_module(name, &block)
|
20
|
+
# assert_generated_test_for(name, &block)
|
21
|
+
# The assert_generated_(class|module|test_for) &block is passed the body of the class/module within the file
|
22
|
+
# assert_has_method(body, *methods) # check that the body has a list of methods (methods with parentheses not supported yet)
|
23
|
+
#
|
24
|
+
# Other helper methods are:
|
25
|
+
# app_root_files - put this in teardown to show files generated by the test method (e.g. p app_root_files)
|
26
|
+
# bare_setup - place this in setup method to create the APP_ROOT folder for each test
|
27
|
+
# bare_teardown - place this in teardown method to destroy the TMP_ROOT or APP_ROOT folder after each test
|
28
|
+
|
29
|
+
def test_generator_without_options
|
30
|
+
name = "myapp"
|
31
|
+
run_generator('nanite_agent', [name], sources)
|
32
|
+
assert_directory_exists "lib/actors"
|
33
|
+
assert_generated_file "lib/actors/sample.rb"
|
34
|
+
assert_directory_exists "config"
|
35
|
+
assert_generated_file "config/nanite.yml"
|
36
|
+
assert_directory_exists "config/initializers"
|
37
|
+
assert_generated_file "config/initializers/myapp.rb"
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def sources
|
42
|
+
[RubiGen::PathSource.new(:test, File.join(File.dirname(__FILE__),"..", generator_path))
|
43
|
+
]
|
44
|
+
end
|
45
|
+
|
46
|
+
def generator_path
|
47
|
+
"daemon_generators"
|
48
|
+
end
|
49
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kennethkalmer-daemon-kit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenneth Kalmer
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-04-
|
12
|
+
date: 2009-04-30 00:00:00 -07:00
|
13
13
|
default_executable: daemon_kit
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -98,12 +98,20 @@ files:
|
|
98
98
|
- daemon_generators/jabber/templates/config/initializers/jabber.rb
|
99
99
|
- daemon_generators/jabber/templates/config/jabber.yml
|
100
100
|
- daemon_generators/jabber/templates/libexec/daemon.rb
|
101
|
+
- daemon_generators/nanite_agent/USAGE
|
102
|
+
- daemon_generators/nanite_agent/nanite_agent_generator.rb
|
103
|
+
- daemon_generators/nanite_agent/templates/config/initializers/nanite_agent.rb
|
104
|
+
- daemon_generators/nanite_agent/templates/config/nanite.yml
|
105
|
+
- daemon_generators/nanite_agent/templates/lib/actors/sample.rb
|
106
|
+
- daemon_generators/nanite_agent/templates/libexec/daemon.rb
|
101
107
|
- lib/daemon_kit.rb
|
102
108
|
- lib/daemon_kit/amqp.rb
|
103
109
|
- lib/daemon_kit/application.rb
|
104
110
|
- lib/daemon_kit/cron.rb
|
105
111
|
- lib/daemon_kit/initializer.rb
|
106
112
|
- lib/daemon_kit/jabber.rb
|
113
|
+
- lib/daemon_kit/nanite.rb
|
114
|
+
- lib/daemon_kit/nanite/agent.rb
|
107
115
|
- lib/daemon_kit/patches/force_kill_wait.rb
|
108
116
|
- lib/daemon_kit/tasks.rb
|
109
117
|
- lib/daemon_kit/tasks/framework.rake
|
@@ -127,6 +135,7 @@ files:
|
|
127
135
|
- test/test_daemon-kit_generator.rb
|
128
136
|
- test/test_generator_helper.rb
|
129
137
|
- test/test_jabber_generator.rb
|
138
|
+
- test/test_nanite_agent_generator.rb
|
130
139
|
has_rdoc: true
|
131
140
|
homepage:
|
132
141
|
post_install_message: |+
|
@@ -166,3 +175,4 @@ test_files:
|
|
166
175
|
- test/test_jabber_generator.rb
|
167
176
|
- test/test_cron_generator.rb
|
168
177
|
- test/test_amqp_generator.rb
|
178
|
+
- test/test_nanite_agent_generator.rb
|