kennethkalmer-daemon-kit 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ == 0.1.5 2009-05-07
2
+
3
+ * DaemonKit::Config class to easy the use of YAML configs internally,
4
+ and in generated daemons
5
+
1
6
  == 0.1.2 2009-04-28
2
7
 
3
8
  * Added missing rubigen dependency
data/Manifest.txt CHANGED
@@ -15,6 +15,7 @@ app_generators/daemon_kit/templates/config/environments/development.rb
15
15
  app_generators/daemon_kit/templates/config/environments/production.rb
16
16
  app_generators/daemon_kit/templates/config/environments/test.rb
17
17
  app_generators/daemon_kit/templates/config/initializers/readme
18
+ app_generators/daemon_kit/templates/lib/daemon.rb
18
19
  app_generators/daemon_kit/templates/libexec/daemon.erb
19
20
  bin/daemon_kit
20
21
  daemon_generators/amqp/USAGE
@@ -40,6 +41,7 @@ daemon_generators/nanite_agent/templates/libexec/daemon.rb
40
41
  lib/daemon_kit.rb
41
42
  lib/daemon_kit/amqp.rb
42
43
  lib/daemon_kit/application.rb
44
+ lib/daemon_kit/config.rb
43
45
  lib/daemon_kit/cron.rb
44
46
  lib/daemon_kit/initializer.rb
45
47
  lib/daemon_kit/jabber.rb
@@ -66,6 +68,8 @@ tasks/rspec.rake
66
68
  test/test_amqp_generator.rb
67
69
  test/test_cron_generator.rb
68
70
  test/test_daemon-kit_generator.rb
71
+ test/test_daemon_kit_config.rb
69
72
  test/test_generator_helper.rb
73
+ test/test_helper.rb
70
74
  test/test_jabber_generator.rb
71
75
  test/test_nanite_agent_generator.rb
@@ -49,7 +49,7 @@ class DaemonKitGenerator < RubiGen::Base
49
49
  # Generator
50
50
  if installer == "default"
51
51
  m.directory "libexec"
52
- m.template "libexec/daemon.erb", "libexec/#{daemon_name}.rb"
52
+ m.template "libexec/daemon.erb", "libexec/#{daemon_name}-daemon.rb"
53
53
  else
54
54
  m.dependency installer, [daemon_name], :destination => destination_root, :collision => :force
55
55
  end
@@ -65,6 +65,7 @@ class DaemonKitGenerator < RubiGen::Base
65
65
 
66
66
  # Libraries
67
67
  m.directory "lib"
68
+ m.file "lib/daemon.rb", "lib/#{daemon_name}.rb"
68
69
 
69
70
  # Tests
70
71
  m.directory "tasks"
@@ -0,0 +1 @@
1
+ # Your starting point for daemon specific classes
data/lib/daemon_kit.rb CHANGED
@@ -4,10 +4,11 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  require 'rubygems'
5
5
 
6
6
  module DaemonKit
7
- VERSION = '0.1.4'
8
-
7
+ VERSION = '0.1.5'
8
+
9
9
  autoload :Initializer, 'daemon_kit/initializer'
10
10
  autoload :Application, 'daemon_kit/application'
11
+ autoload :Config, 'daemon_kit/config'
11
12
  autoload :Cron, 'daemon_kit/cron'
12
13
  autoload :Jabber, 'daemon_kit/jabber'
13
14
  autoload :AMQP, 'daemon_kit/amqp'
@@ -11,11 +11,7 @@ module DaemonKit
11
11
  class << self
12
12
 
13
13
  def instance
14
- @instance ||= (
15
- config = YAML.load_file( "#{DAEMON_ROOT}/config/amqp.yml" )[DAEMON_ENV]
16
- raise ArgumentError, "Missing AMQP configuration for #{DAEMON_ENV} environment" if config.nil?
17
- new( config )
18
- )
14
+ @instance ||= new
19
15
  end
20
16
 
21
17
  private :new
@@ -26,7 +22,7 @@ module DaemonKit
26
22
  end
27
23
 
28
24
  def initialize( config = {} )
29
- @config = config.inject({}) { |m,c| m[c[0].to_sym] = c[1]; m } # symbolize_keys
25
+ @config = DaemonKit::Config.load('amqp').to_h( true )
30
26
  end
31
27
 
32
28
  def run(&block)
@@ -0,0 +1,64 @@
1
+ module DaemonKit
2
+
3
+ # Simplify simple config file loading for daemons. Assumes the
4
+ # config files all live in DAEMON_ROOT/config and are YAML
5
+ # files. Loaded configs are accessed like a hash with string
6
+ # keys.
7
+ #
8
+ # Config files can either be keyed by environment (default behavior)
9
+ # or be a normal hash.
10
+ #
11
+ # Load a config by passing the filename (with or without the .yml
12
+ # extension) to #load.
13
+ #
14
+ # At this stage the configs are read-only.
15
+ #
16
+ # Any of the keys can be called as methods as well.
17
+ class Config
18
+
19
+ class << self
20
+
21
+ # Load the +config+.yml file from DAEMON_ROOT/config
22
+ def load( config )
23
+ config += '.yml' unless config =~ /\.yml$/
24
+
25
+ path = File.join( DAEMON_ROOT, 'config', config )
26
+
27
+ raise ArgumentError, "Can't find #{path}" unless File.exists?( path )
28
+
29
+ new( YAML.load_file( path ) )
30
+ end
31
+
32
+ end
33
+
34
+ # Expects a hash, looks for DAEMON_ENV key
35
+ def initialize( config_data ) #:nodoc:
36
+ if config_data.has_key?( DAEMON_ENV )
37
+ @data = config_data[ DAEMON_ENV ]
38
+ else
39
+ @data = config_data
40
+ end
41
+ end
42
+
43
+ # Pick out a config by name
44
+ def []( key )
45
+ @data[ key.to_s ]
46
+ end
47
+
48
+ # Return the internal hash structure used, optionally symbolizing
49
+ # the first level of keys in the hash
50
+ def to_h( symbolize = false )
51
+ symbolize ? @data.inject({}) { |m,c| m[c[0].to_sym] = c[1]; m } : @data
52
+ end
53
+
54
+ def method_missing( method_name, *args ) #:nodoc:
55
+ # don't match setters
56
+ unless method_name.to_s =~ /[\w_]+=$/
57
+ # pick a key if we have it
58
+ return @data[ method_name.to_s ] if @data.keys.include?( method_name.to_s )
59
+ end
60
+
61
+ super
62
+ end
63
+ end
64
+ end
@@ -12,28 +12,24 @@ module DaemonKit
12
12
  @@message_handler = nil
13
13
  @@presence_handler = nil
14
14
  @@subscription_handler = nil
15
-
15
+
16
16
  class << self
17
17
 
18
18
  # Deliver a message to the specified jid.
19
19
  def deliver( jid, message )
20
20
  instance.connection.deliver( jid, message )
21
21
  end
22
-
22
+
23
23
  # Use this instead of initializing, keeps it singleton
24
24
  def instance
25
- @instance ||= (
26
- config = YAML.load_file( "#{DAEMON_ROOT}/config/jabber.yml" )[DAEMON_ENV]
27
- raise ArgumentError, "Missing Jabber configuration for #{DAEMON_ENV} environment" if config.nil?
28
- new( config )
29
- )
25
+ @instance ||= new
30
26
  @instance.startup!
31
27
  end
32
28
  private :new
33
29
 
34
30
  def run
35
31
  DaemonKit.logger.info "Starting jabber loop"
36
-
32
+
37
33
  loop do
38
34
  process_messages
39
35
  process_updates
@@ -47,7 +43,7 @@ module DaemonKit
47
43
  end
48
44
  end
49
45
  end
50
-
46
+
51
47
  def process_messages
52
48
  @message_handler ||= Proc.new { |m| DaemonKit.logger.info "Received message from #{m.from}: #{m.body}" }
53
49
 
@@ -62,7 +58,7 @@ module DaemonKit
62
58
  instance.connection.presence_updates { |friend, old_presence, new_presence|
63
59
  @presence_handler.call(friend, old_presence, new_presence)
64
60
  }
65
-
61
+
66
62
  end
67
63
 
68
64
  def process_subscriptions
@@ -70,11 +66,11 @@ module DaemonKit
70
66
 
71
67
  instance.connection.subscription_requests { |friend,presence| @subscription_handler.call(friend,presence) }
72
68
  end
73
-
69
+
74
70
  def received_messages(&block)
75
71
  @message_handler = block
76
72
  end
77
-
73
+
78
74
  def presence_updates(&block)
79
75
  @presence_handler = block
80
76
  end
@@ -82,10 +78,12 @@ module DaemonKit
82
78
  def subscription_requests(&block)
83
79
  @subscription_handler = block
84
80
  end
85
-
81
+
86
82
  end
87
83
 
88
- def initialize( options = {} )
84
+ def initialize
85
+ options = DaemonKit::Config.load( 'jabber' )
86
+
89
87
  @jabber_id = options.delete("jabber_id")
90
88
  @password = options.delete("password")
91
89
  @resource = options.delete("resource") || 'daemon_kit'
@@ -103,7 +101,7 @@ module DaemonKit
103
101
 
104
102
  DaemonKit.trap( 'INT', Proc.new { self.shutdown! } )
105
103
  DaemonKit.trap( 'TERM', Proc.new { self.shutdown! } )
106
-
104
+
107
105
  @booted = true
108
106
 
109
107
  self
@@ -141,9 +139,9 @@ module DaemonKit
141
139
  def status_line
142
140
  "#{DaemonKit.configuration.daemon_name} ready for instructions"
143
141
  end
144
-
142
+
145
143
  private
146
-
144
+
147
145
  def connect!
148
146
  jid = @jabber_id + '/' + @resource
149
147
 
@@ -167,6 +165,6 @@ module DaemonKit
167
165
  end
168
166
  end
169
167
  end
170
-
168
+
171
169
  end
172
170
  end
@@ -8,11 +8,7 @@ module DaemonKit
8
8
  class << self
9
9
 
10
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
- )
11
+ @instance ||= new
16
12
  end
17
13
 
18
14
  private :new
@@ -23,8 +19,8 @@ module DaemonKit
23
19
 
24
20
  end
25
21
 
26
- def initialize( options = {} )
27
- @config = options.inject({}) { |m,c| m[c[0].to_sym] = c[1]; m } # symbolize_keys
22
+ def initialize
23
+ @config = DaemonKit::Config.load( 'nanite' )
28
24
 
29
25
  config_agent
30
26
  end
@@ -37,7 +33,7 @@ module DaemonKit
37
33
  # Start our mapper
38
34
  mapper_thread = Thread.new do
39
35
  EM.run do
40
- agent = ::Nanite.start_agent( @config )
36
+ agent = ::Nanite.start_agent( @config.to_h( true ) )
41
37
  block.call( agent ) if block
42
38
  end
43
39
  end
@@ -8,3 +8,14 @@ end
8
8
 
9
9
  require File.dirname(__FILE__) + '/../config/boot'
10
10
  require '<%= gem_name %>'
11
+
12
+ Spec::Runner.configure do |config|
13
+ # == Mock Framework
14
+ #
15
+ # RSpec uses it's own mocking framework by default. If you prefer to
16
+ # use mocha, flexmock or RR, uncomment the appropriate line:
17
+ #
18
+ config.mock_with :mocha
19
+ # config.mock_with :flexmock
20
+ # config.mock_with :rr
21
+ end
@@ -43,8 +43,9 @@ class TestDaemonKitGenerator < Test::Unit::TestCase
43
43
  assert_directory_exists "config/initializers"
44
44
  assert_generated_file "config/initializers/readme"
45
45
  assert_directory_exists "lib"
46
+ assert_generated_file "lib/#{daemon_name}.rb"
46
47
  assert_directory_exists "libexec"
47
- assert_generated_file "libexec/#{daemon_name}.rb"
48
+ assert_generated_file "libexec/#{daemon_name}-daemon.rb"
48
49
  assert_directory_exists "log"
49
50
  assert_directory_exists "spec"
50
51
  assert_directory_exists "tasks"
@@ -0,0 +1,28 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class TestDaemonKitConfig < Test::Unit::TestCase
4
+
5
+ def test_initialize_with_env_hash
6
+ data = {
7
+ 'test' => {
8
+ 'foo' => 'bar'
9
+ }
10
+ }
11
+
12
+ config = DaemonKit::Config.new( data )
13
+
14
+ assert_equal config['foo'], 'bar'
15
+ assert_equal config.foo, 'bar'
16
+ end
17
+
18
+ def test_initialize_without_env_hash
19
+ data = {
20
+ 'foo' => 'bar'
21
+ }
22
+
23
+ config = DaemonKit::Config.new( data )
24
+
25
+ assert_equal config['foo'], 'bar'
26
+ assert_equal config.foo, 'bar'
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ require 'test/unit'
2
+
3
+ DAEMON_ENV = "test"
4
+
5
+ require File.dirname(__FILE__) + '/../lib/daemon_kit'
6
+
7
+
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
4
+ version: 0.1.5
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-30 00:00:00 -07:00
12
+ date: 2009-05-07 00:00:00 -07:00
13
13
  default_executable: daemon_kit
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -82,6 +82,7 @@ files:
82
82
  - app_generators/daemon_kit/templates/config/environments/production.rb
83
83
  - app_generators/daemon_kit/templates/config/environments/test.rb
84
84
  - app_generators/daemon_kit/templates/config/initializers/readme
85
+ - app_generators/daemon_kit/templates/lib/daemon.rb
85
86
  - app_generators/daemon_kit/templates/libexec/daemon.erb
86
87
  - bin/daemon_kit
87
88
  - daemon_generators/amqp/USAGE
@@ -107,6 +108,7 @@ files:
107
108
  - lib/daemon_kit.rb
108
109
  - lib/daemon_kit/amqp.rb
109
110
  - lib/daemon_kit/application.rb
111
+ - lib/daemon_kit/config.rb
110
112
  - lib/daemon_kit/cron.rb
111
113
  - lib/daemon_kit/initializer.rb
112
114
  - lib/daemon_kit/jabber.rb
@@ -133,7 +135,9 @@ files:
133
135
  - test/test_amqp_generator.rb
134
136
  - test/test_cron_generator.rb
135
137
  - test/test_daemon-kit_generator.rb
138
+ - test/test_daemon_kit_config.rb
136
139
  - test/test_generator_helper.rb
140
+ - test/test_helper.rb
137
141
  - test/test_jabber_generator.rb
138
142
  - test/test_nanite_agent_generator.rb
139
143
  has_rdoc: true
@@ -170,9 +174,11 @@ signing_key:
170
174
  specification_version: 2
171
175
  summary: Daemon Kit aims to simplify creating Ruby daemons by providing a sound application skeleton (through a generator), task specific generators (jabber bot, etc) and robust environment management code.
172
176
  test_files:
173
- - test/test_daemon-kit_generator.rb
174
177
  - test/test_generator_helper.rb
175
178
  - test/test_jabber_generator.rb
176
179
  - test/test_cron_generator.rb
177
180
  - test/test_amqp_generator.rb
178
181
  - test/test_nanite_agent_generator.rb
182
+ - test/test_daemon-kit_generator.rb
183
+ - test/test_daemon_kit_config.rb
184
+ - test/test_helper.rb