amqp-daemon-kit 0.1.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. data/.gitignore +5 -0
  2. data/Configuration.txt +110 -0
  3. data/Deployment.txt +113 -0
  4. data/History.txt +131 -0
  5. data/Logging.txt +96 -0
  6. data/PostInstall.txt +6 -0
  7. data/README.rdoc +132 -0
  8. data/Rakefile +29 -0
  9. data/RuoteParticipants.txt +113 -0
  10. data/TODO.txt +27 -0
  11. data/bin/daemon-kit +18 -0
  12. data/config/website.yml +2 -0
  13. data/daemon-kit.gemspec +265 -0
  14. data/lib/daemon_kit.rb +60 -0
  15. data/lib/daemon_kit/abstract_logger.rb +249 -0
  16. data/lib/daemon_kit/application.rb +230 -0
  17. data/lib/daemon_kit/arguments.rb +165 -0
  18. data/lib/daemon_kit/commands/console.rb +38 -0
  19. data/lib/daemon_kit/commands/destroy.rb +10 -0
  20. data/lib/daemon_kit/commands/generate.rb +10 -0
  21. data/lib/daemon_kit/config.rb +113 -0
  22. data/lib/daemon_kit/console_daemon.rb +2 -0
  23. data/lib/daemon_kit/core_ext.rb +1 -0
  24. data/lib/daemon_kit/core_ext/configurable.rb +96 -0
  25. data/lib/daemon_kit/core_ext/string.rb +22 -0
  26. data/lib/daemon_kit/cron.rb +67 -0
  27. data/lib/daemon_kit/cucumber/world.rb +38 -0
  28. data/lib/daemon_kit/deployment/capistrano.rb +516 -0
  29. data/lib/daemon_kit/dk_amqp.rb +39 -0
  30. data/lib/daemon_kit/em.rb +43 -0
  31. data/lib/daemon_kit/error_handlers/base.rb +32 -0
  32. data/lib/daemon_kit/error_handlers/hoptoad.rb +180 -0
  33. data/lib/daemon_kit/exceptions.rb +15 -0
  34. data/lib/daemon_kit/generators.rb +67 -0
  35. data/lib/daemon_kit/generators/base.rb +60 -0
  36. data/lib/daemon_kit/initializer.rb +449 -0
  37. data/lib/daemon_kit/jabber.rb +171 -0
  38. data/lib/daemon_kit/nanite.rb +7 -0
  39. data/lib/daemon_kit/nanite/agent.rb +77 -0
  40. data/lib/daemon_kit/pid_file.rb +61 -0
  41. data/lib/daemon_kit/ruote_participants.rb +125 -0
  42. data/lib/daemon_kit/ruote_pseudo_participant.rb +68 -0
  43. data/lib/daemon_kit/ruote_workitem.rb +187 -0
  44. data/lib/daemon_kit/safety.rb +84 -0
  45. data/lib/daemon_kit/tasks.rb +2 -0
  46. data/lib/daemon_kit/tasks/environment.rake +11 -0
  47. data/lib/daemon_kit/tasks/framework.rake +123 -0
  48. data/lib/daemon_kit/tasks/god.rake +62 -0
  49. data/lib/daemon_kit/tasks/log.rake +8 -0
  50. data/lib/daemon_kit/tasks/monit.rake +29 -0
  51. data/lib/daemon_kit/vendor/thor-0.13.6/CHANGELOG.rdoc +89 -0
  52. data/lib/daemon_kit/vendor/thor-0.13.6/LICENSE +20 -0
  53. data/lib/daemon_kit/vendor/thor-0.13.6/README.rdoc +297 -0
  54. data/lib/daemon_kit/vendor/thor-0.13.6/Thorfile +69 -0
  55. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor.rb +244 -0
  56. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions.rb +296 -0
  57. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions/create_file.rb +103 -0
  58. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions/directory.rb +91 -0
  59. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions/empty_directory.rb +134 -0
  60. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions/file_manipulation.rb +223 -0
  61. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions/inject_into_file.rb +104 -0
  62. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/base.rb +540 -0
  63. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/file_binary_read.rb +9 -0
  64. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  65. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/ordered_hash.rb +100 -0
  66. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/error.rb +30 -0
  67. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/group.rb +271 -0
  68. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/invocation.rb +180 -0
  69. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/parser.rb +4 -0
  70. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/parser/argument.rb +67 -0
  71. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/parser/arguments.rb +150 -0
  72. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/parser/option.rb +128 -0
  73. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/parser/options.rb +169 -0
  74. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/rake_compat.rb +66 -0
  75. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/runner.rb +314 -0
  76. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/shell.rb +83 -0
  77. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/shell/basic.rb +239 -0
  78. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/shell/color.rb +108 -0
  79. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/task.rb +102 -0
  80. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb +224 -0
  81. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/version.rb +3 -0
  82. data/lib/daemon_kit/xmpp.rb +100 -0
  83. data/lib/generators/daemon_kit/amqp/USAGE +5 -0
  84. data/lib/generators/daemon_kit/amqp/amqp_generator.rb +24 -0
  85. data/lib/generators/daemon_kit/amqp/templates/config/amqp.yml +28 -0
  86. data/lib/generators/daemon_kit/amqp/templates/config/pre-daemonize/amqp.rb +7 -0
  87. data/lib/generators/daemon_kit/amqp/templates/libexec/%app_name%-daemon.rb +37 -0
  88. data/lib/generators/daemon_kit/app/USAGE +7 -0
  89. data/lib/generators/daemon_kit/app/app_generator.rb +140 -0
  90. data/lib/generators/daemon_kit/app/templates/Gemfile +12 -0
  91. data/lib/generators/daemon_kit/app/templates/README.tt +58 -0
  92. data/lib/generators/daemon_kit/app/templates/Rakefile +6 -0
  93. data/lib/generators/daemon_kit/app/templates/bin/daemon.tt +7 -0
  94. data/lib/generators/daemon_kit/app/templates/config/arguments.rb +12 -0
  95. data/lib/generators/daemon_kit/app/templates/config/boot.rb +70 -0
  96. data/lib/generators/daemon_kit/app/templates/config/environment.rb.tt +26 -0
  97. data/lib/generators/daemon_kit/app/templates/config/environments/development.rb +2 -0
  98. data/lib/generators/daemon_kit/app/templates/config/environments/production.rb +5 -0
  99. data/lib/generators/daemon_kit/app/templates/config/environments/test.rb +2 -0
  100. data/lib/generators/daemon_kit/app/templates/config/post-daemonize/readme +5 -0
  101. data/lib/generators/daemon_kit/app/templates/config/pre-daemonize/readme +12 -0
  102. data/lib/generators/daemon_kit/app/templates/lib/%app_name%.rb +2 -0
  103. data/lib/generators/daemon_kit/app/templates/libexec/%app_name%-daemon.rb +18 -0
  104. data/lib/generators/daemon_kit/app/templates/script/console +3 -0
  105. data/lib/generators/daemon_kit/app/templates/script/destroy +3 -0
  106. data/lib/generators/daemon_kit/app/templates/script/generate +3 -0
  107. data/lib/generators/daemon_kit/capistrano/capistrano_generator.rb +26 -0
  108. data/lib/generators/daemon_kit/capistrano/templates/Capfile +10 -0
  109. data/lib/generators/daemon_kit/capistrano/templates/USAGE +10 -0
  110. data/lib/generators/daemon_kit/capistrano/templates/config/deploy.rb.tt +67 -0
  111. data/lib/generators/daemon_kit/capistrano/templates/config/deploy/logrotate.erb +13 -0
  112. data/lib/generators/daemon_kit/capistrano/templates/config/deploy/production.rb.tt +6 -0
  113. data/lib/generators/daemon_kit/capistrano/templates/config/deploy/staging.rb.tt +6 -0
  114. data/lib/generators/daemon_kit/capistrano/templates/config/environments/staging.rb +0 -0
  115. data/lib/generators/daemon_kit/cron/USAGE +5 -0
  116. data/lib/generators/daemon_kit/cron/cron_generator.rb +24 -0
  117. data/lib/generators/daemon_kit/cron/templates/config/pre-daemonize/cron.rb +11 -0
  118. data/lib/generators/daemon_kit/cron/templates/libexec/%app_name%-daemon.rb +48 -0
  119. data/lib/generators/daemon_kit/cucumber/USAGE +11 -0
  120. data/lib/generators/daemon_kit/cucumber/cucumber_generator.rb +45 -0
  121. data/lib/generators/daemon_kit/cucumber/templates/config/environments/cucumber.rb +2 -0
  122. data/lib/generators/daemon_kit/cucumber/templates/features/step_definitions/.empty_directory +0 -0
  123. data/lib/generators/daemon_kit/cucumber/templates/features/support/env.rb +7 -0
  124. data/lib/generators/daemon_kit/cucumber/templates/script/cucumber +7 -0
  125. data/lib/generators/daemon_kit/cucumber/templates/tasks/cucumber.rake +13 -0
  126. data/lib/generators/daemon_kit/nanite_agent/USAGE +5 -0
  127. data/lib/generators/daemon_kit/nanite_agent/nanite_agent_generator.rb +29 -0
  128. data/lib/generators/daemon_kit/nanite_agent/templates/config/nanite.yml +35 -0
  129. data/lib/generators/daemon_kit/nanite_agent/templates/config/pre-daemonize/nanite_agent.rb +6 -0
  130. data/lib/generators/daemon_kit/nanite_agent/templates/lib/actors/sample.rb +11 -0
  131. data/lib/generators/daemon_kit/nanite_agent/templates/libexec/%app_name%-daemon.rb +31 -0
  132. data/lib/generators/daemon_kit/rspec/USAGE +5 -0
  133. data/lib/generators/daemon_kit/rspec/rspec_generator.rb +20 -0
  134. data/lib/generators/daemon_kit/rspec/templates/spec/%app_name%_spec.rb +11 -0
  135. data/lib/generators/daemon_kit/rspec/templates/spec/spec.opts +1 -0
  136. data/lib/generators/daemon_kit/rspec/templates/spec/spec_helper.rb +23 -0
  137. data/lib/generators/daemon_kit/rspec/templates/tasks/rspec.rake +21 -0
  138. data/lib/generators/daemon_kit/ruote/USAGE +5 -0
  139. data/lib/generators/daemon_kit/ruote/ruote_generator.rb +29 -0
  140. data/lib/generators/daemon_kit/ruote/templates/config/amqp.yml +30 -0
  141. data/lib/generators/daemon_kit/ruote/templates/config/pre-daemonize/ruote.rb +13 -0
  142. data/lib/generators/daemon_kit/ruote/templates/config/ruote.yml +23 -0
  143. data/lib/generators/daemon_kit/ruote/templates/lib/%app_name%.rb +4 -0
  144. data/lib/generators/daemon_kit/ruote/templates/lib/sample.rb +26 -0
  145. data/lib/generators/daemon_kit/ruote/templates/libexec/%app_name%-daemon.rb +33 -0
  146. data/lib/generators/daemon_kit/test_unit/USAGE +5 -0
  147. data/lib/generators/daemon_kit/test_unit/templates/tasks/test_unit.rake +7 -0
  148. data/lib/generators/daemon_kit/test_unit/templates/test/%app_name%_test.rb.tt +9 -0
  149. data/lib/generators/daemon_kit/test_unit/templates/test/test_helper.rb +6 -0
  150. data/lib/generators/daemon_kit/test_unit/test_unit_generator.rb +20 -0
  151. data/lib/generators/daemon_kit/xmpp/templates/config/pre-daemonize/xmpp.rb +6 -0
  152. data/lib/generators/daemon_kit/xmpp/templates/config/xmpp.yml +29 -0
  153. data/lib/generators/daemon_kit/xmpp/templates/libexec/%app_name%-daemon.rb +27 -0
  154. data/lib/generators/daemon_kit/xmpp/xmpp_generator.rb +24 -0
  155. data/script/console +10 -0
  156. data/script/destroy +14 -0
  157. data/script/generate +14 -0
  158. data/script/txt2html +71 -0
  159. data/spec/abstract_logger_spec.rb +126 -0
  160. data/spec/argument_spec.rb +70 -0
  161. data/spec/config_spec.rb +83 -0
  162. data/spec/configurable_spec.rb +56 -0
  163. data/spec/daemon_kit_spec.rb +7 -0
  164. data/spec/error_handlers_spec.rb +23 -0
  165. data/spec/fixtures/env.yml +15 -0
  166. data/spec/fixtures/noenv.yml +4 -0
  167. data/spec/initializer_spec.rb +26 -0
  168. data/spec/spec.opts +1 -0
  169. data/spec/spec_helper.rb +27 -0
  170. data/tasks/cucumber.rake +13 -0
  171. data/tasks/rspec.rake +20 -0
  172. data/tasks/tests.rake +6 -0
  173. data/templates/god/god.erb +69 -0
  174. data/templates/monit/monit.erb +14 -0
  175. data/test/test_amqp_generator.rb +48 -0
  176. data/test/test_cron_generator.rb +45 -0
  177. data/test/test_daemon-kit_generator.rb +84 -0
  178. data/test/test_daemon_kit_config.rb +28 -0
  179. data/test/test_deploy_capistrano_generator.rb +48 -0
  180. data/test/test_generator_helper.rb +29 -0
  181. data/test/test_helper.rb +7 -0
  182. data/test/test_nanite_agent_generator.rb +49 -0
  183. data/test/test_ruote_generator.rb +51 -0
  184. data/test/test_test_unit_generator.rb +46 -0
  185. metadata +302 -0
@@ -0,0 +1,165 @@
1
+ require 'optparse'
2
+
3
+ module DaemonKit
4
+
5
+ # A wrapper around OptParse for setting up arguments to the daemon
6
+ # process.
7
+ #
8
+ # TODO: Set rules for basic options that go for all daemons
9
+ # TODO: Load options from config/arguments.rb
10
+ class Arguments
11
+
12
+ # Default command
13
+ @default_command = 'run'
14
+
15
+ # Valid commands
16
+ @commands = [
17
+ 'start',
18
+ 'stop',
19
+ 'run'
20
+ ]
21
+
22
+ class << self
23
+
24
+ attr_reader :default_command, :commands
25
+
26
+ # Parse the argument values and return an array with the command
27
+ # name, config values and argument values
28
+ def parse( argv )
29
+ cmd, argv = self.command( argv )
30
+
31
+ return cmd, *self.configuration( argv )
32
+ end
33
+
34
+ # Parse the provided argument array for a given command, or
35
+ # return the default command and the remaining arguments
36
+ def command( argv )
37
+ # extract command or set default
38
+ cmd = self.commands.include?( argv[0] ) ? argv.shift : self.default_command
39
+
40
+ return cmd.to_sym, argv
41
+ end
42
+
43
+ # Extracts any values for arguments matching '--config' as well
44
+ # as some implication arguments like '-e'. Returns an array with
45
+ # the configs as the first value and the remaing args as the
46
+ # last value.
47
+ #
48
+ # To set a value on the default #Configuration instance, use the
49
+ # following notation:
50
+ #
51
+ # --config attribute=value
52
+ #
53
+ # The above notation can be used several times to set different
54
+ # values.
55
+ #
56
+ # Special, or 'normal' arguments that are mapped to the default
57
+ # #Configuration instance are listed below:
58
+ #
59
+ # -e value or --env value => environment
60
+ # --pid pidfile => pid_file
61
+ # -l path or --log path => /path/to/log/file
62
+ #
63
+ def configuration( argv )
64
+ configs = []
65
+
66
+ i = 0
67
+ while i < argv.size
68
+ if argv[i] == "--config"
69
+ argv.delete_at( i )
70
+ configs << argv.delete_at(i)
71
+ next
72
+ end
73
+
74
+ if argv[i] == "-e" || argv[i] == "--env"
75
+ argv.delete_at( i )
76
+ configs << "environment=#{argv.delete_at(i)}"
77
+ next
78
+ end
79
+
80
+ if argv[i] == "-l" || argv[i] == "--log"
81
+ argv.delete_at( i )
82
+ configs << "log_path=#{argv.delete_at(i)}"
83
+ next
84
+ end
85
+
86
+ if argv[i] == "--pidfile"
87
+ argv.delete_at( i )
88
+ configs << "pid_file=#{argv.delete_at(i)}"
89
+ next
90
+ end
91
+
92
+ i += 1
93
+ end
94
+
95
+ return configs, argv
96
+ end
97
+
98
+ # Return the arguments remaining after running through #configuration
99
+ def arguments( argv )
100
+ self.configuration( argv ).last
101
+ end
102
+ end
103
+
104
+ attr_reader :options
105
+
106
+ def initialize
107
+ @options = {}
108
+
109
+ @parser = OptionParser.new do |opts|
110
+ opts.banner = "Usage: #{File.basename($0)} [command] [options]"
111
+
112
+ opts.separator ""
113
+
114
+ opts.separator "Command is one of the following:"
115
+ opts.separator " run - Run the daemon without forking (default)"
116
+ opts.separator " start - Run the daemon"
117
+ opts.separator " stop - Stop the running daemon"
118
+
119
+ opts.separator ""
120
+
121
+ opts.separator "Options can be:"
122
+
123
+ arg_file = File.join( DaemonKit.root, 'config', 'arguments.rb' )
124
+ eval(IO.read(arg_file), binding, arg_file) if File.exists?( arg_file )
125
+
126
+ opts.on("-e", "--env ENVIRONMENT", "Environment for the process", "Defaults to development") do
127
+ # Nothing, just here for show
128
+ end
129
+
130
+ opts.on("--pidfile PATH", "Path to the pidfile", "Defaults to log/#{DaemonKit.configuration.daemon_name}.pid") do
131
+ # Nothing, just here for show
132
+ end
133
+
134
+ opts.on("-l", "--log /path/to/logfile", "Path to the log file", "Defaults to log/[environment].log") do
135
+ # Nothing, just here for show
136
+ end
137
+
138
+ opts.separator ""
139
+ opts.separator "Advanced configurations:"
140
+ opts.on("--config ATTRIBUTE=VALUE",
141
+ "Change values of the daemon-kit Configuration instance",
142
+ "Example: log_dir=/path/to/log-directory") do
143
+ # Nothing, just here for show
144
+ end
145
+
146
+ opts.separator ""
147
+
148
+ opts.separator "Common options:"
149
+ opts.on("-v", "--version", "Show version information and exit") do
150
+ puts "daemon-kit #{DaemonKit::VERSION} (http://github.com/kennethkalmer/daemon-kit)"
151
+ exit
152
+ end
153
+
154
+ opts.on_tail("-h", "--help", "Show this message") do
155
+ puts opts
156
+ exit
157
+ end
158
+ end
159
+ end
160
+
161
+ def parse( argv )
162
+ @parser.parse!( argv )
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,38 @@
1
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
2
+
3
+ require 'optparse'
4
+
5
+ options = { :irb => irb }
6
+ OptionParser.new do |opt|
7
+ opt.banner = "Usage: console [environment] [options]"
8
+ opt.on("--irb=[#{irb}]", 'Invoke a different irb.') { |v| options[:irb] = v }
9
+ opt.on("--debugger", 'Enable ruby-debugging for the console.') { |v| options[:debugger] = v }
10
+ opt.parse!(ARGV)
11
+ end
12
+
13
+ libs = " -r irb/completion"
14
+ libs << %( -r "#{DAEMON_ROOT}/config/environment")
15
+ libs << " -r daemon_kit/console_daemon"
16
+
17
+ if options[:debugger]
18
+ begin
19
+ require 'ruby-debug'
20
+ libs << " -r ruby-debug"
21
+ puts "=> Debugger enabled"
22
+ rescue Exception
23
+ puts "You need to install ruby-debug to run the console in debugging mode. With gems, use 'gem install ruby-debug'"
24
+ exit
25
+ end
26
+ end
27
+
28
+ ENV['DAEMON_ENV'] = case ARGV.first
29
+ when "p"; "production"
30
+ when "d"; "development"
31
+ when "t"; "test"
32
+ else
33
+ ARGV.first || ENV['DAEMON_ENV'] || 'development'
34
+ end
35
+
36
+ puts "Loading #{ENV['DAEMON_ENV']} environment (daemon-kit #{DaemonKit::VERSION})"
37
+
38
+ exec "#{options[:irb]} #{libs} --simple-prompt"
@@ -0,0 +1,10 @@
1
+ require 'daemon_kit/generators'
2
+ DaemonKit::Generators.configure!
3
+
4
+ if ARGV.size == 0
5
+ DaemonKit::Generators.help
6
+ exit
7
+ end
8
+
9
+ name = ARGV.shift
10
+ DaemonKit::Generators.invoke name, ARGV, :behaviour => :revoke, :destination_root => DaemonKit.root
@@ -0,0 +1,10 @@
1
+ require 'daemon_kit/generators'
2
+ DaemonKit::Generators.configure!
3
+
4
+ if ARGV.size == 0
5
+ DaemonKit::Generators.help
6
+ exit
7
+ end
8
+
9
+ name = ARGV.shift
10
+ DaemonKit::Generators.invoke name, ARGV, :behaviour => :invoke, :destination_root => DaemonKit.root
@@ -0,0 +1,113 @@
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 = config.to_s
24
+ config += '.yml' unless config =~ /\.yml$/
25
+
26
+ path = File.join( DAEMON_ROOT, 'config', config )
27
+
28
+ raise ArgumentError, "Can't find #{path}" unless File.exists?( path )
29
+
30
+ new( YAML.load_file( path ) )
31
+ end
32
+
33
+ # Return the +config+.yml file as a raw hash.
34
+ def hash( config, symbolize = false )
35
+ self.load( config ).to_h( symbolize )
36
+ end
37
+
38
+ end
39
+
40
+ # Expects a hash, looks for DAEMON_ENV key
41
+ def initialize( config_data ) #:nodoc:
42
+ if config_data.has_key?( DAEMON_ENV )
43
+ self.data = config_data[ DAEMON_ENV ]
44
+ else
45
+ self.data = config_data
46
+ end
47
+ end
48
+
49
+ # Pick out a config by name
50
+ def []( key )
51
+ @data[ key.to_s ]
52
+ end
53
+
54
+ # Return the internal hash structure used, optionally symbolizing
55
+ # the first level of keys in the hash
56
+ def to_h( symbolize = false )
57
+ symbolize ? @data.symbolize_keys : @data
58
+ end
59
+
60
+ def method_missing( method_name, *args ) #:nodoc:
61
+ unless method_name.to_s =~ /[\w_]+=$/
62
+ #if @data.keys.include?( method_name.to_s )
63
+ # return @data.send( method_name.to_s )
64
+ #end
65
+ if @data.respond_to?( method_name.to_s )
66
+ return @data.send( method_name.to_s )
67
+ elsif @data.respond_to?( method_name.to_s.gsub(/\-/, '_') )
68
+ return @data.send( method_name.to_s.gsub(/\-/, '_') )
69
+ end
70
+ end
71
+
72
+ super
73
+ end
74
+
75
+ def data=( hash )
76
+ @data = hash
77
+ class << @data
78
+ def symbolize_keys( hash = self )
79
+ hash.inject({}) { |result, (key, value)|
80
+ new_key = case key
81
+ when String then key.to_sym
82
+ else key
83
+ end
84
+ new_value = case value
85
+ when Hash then symbolize_keys(value)
86
+ else value
87
+ end
88
+ result[new_key] = new_value
89
+ result
90
+ }
91
+ end
92
+ end
93
+
94
+ extend_hash( @data )
95
+ end
96
+
97
+ def extend_hash( hash )
98
+ hash.keys.each do |k|
99
+ hash.instance_eval <<-KEY
100
+ def #{k.gsub(/\-/, '_')}
101
+ fetch("#{k}")
102
+ end
103
+ KEY
104
+ end
105
+
106
+ hash.each do |(key, value)|
107
+ case value
108
+ when Hash then extend_hash( value )
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,2 @@
1
+ # Load and configure our daemon process, without daemonizing
2
+ DaemonKit::Application.running!
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/core_ext/string'
@@ -0,0 +1,96 @@
1
+ module DaemonKit
2
+
3
+ # Provide some advanced helpers for managing access to instance variables.
4
+ module Configurable
5
+
6
+ def self.included(base) #:nodoc:
7
+ base.class_eval <<-EOF
8
+ @configurables = {}
9
+ @configurable_defaults = {}
10
+
11
+ class << self
12
+ attr_reader :configurables, :configurable_defaults
13
+ end
14
+ EOF
15
+
16
+ base.extend( ClassMethods )
17
+ base.send( :include, InstanceMethods )
18
+ end
19
+
20
+ module ClassMethods
21
+
22
+ # Create a configurable value on any instance, which can contain
23
+ # a default value, and/or be locked.
24
+ #
25
+ # Create a standard getter/setter without a default value
26
+ #
27
+ # configurable :foo
28
+ #
29
+ # Create a getter/setter with a default value
30
+ #
31
+ # configurable :foo, true
32
+ #
33
+ # The final argument can be an options hash, which currently
34
+ # respects only one key: +locked+ (false by default). Locking a
35
+ # configurable means the value can only be set once by the
36
+ # setter method.
37
+ #
38
+ # configurable :foo, :locked => true
39
+ #
40
+ # As long as the getter method (+foo+) returns nil, the standard
41
+ # setter method will work. As soon as the getter returns a
42
+ # non-nil value the setter won't set a new value. To set a new
43
+ # value you'll have to explicitly use the #set instance method.
44
+ def configurable( name, *args )
45
+ opts = args.last.is_a?( Hash ) ? args.pop : {}
46
+ opts = { :locked => false }.merge( opts )
47
+
48
+ default = args.size <= 1 ? args.pop : args
49
+
50
+ name = name.to_sym
51
+
52
+ self.configurables[ name ] = opts
53
+ self.configurable_defaults[ name ] = default
54
+
55
+ class_eval( <<-EOF, __FILE__, __LINE__ )
56
+ def #{name} # def foo
57
+ if _configurables[:#{name}].nil? # if _configurables[:foo].nil?
58
+ self.class.configurable_defaults[:#{name}] # self.class.configurable_defaults[:foo]
59
+ else # else
60
+ _configurables[:#{name}] # _configurables[:foo]
61
+ end # end
62
+ end #
63
+
64
+ def #{name}=( value ) # def foo=( value )
65
+ if #{name}.nil? || # if foo.nil? ||
66
+ !self.class.configurables[:#{name}][:locked] # !self.class.configurables[:foo][:locked]
67
+ #
68
+ _configurables[:#{name}] = value # _configurables[:foo] = value
69
+ end # end
70
+ end # end
71
+ EOF
72
+ end
73
+
74
+ end
75
+
76
+ module InstanceMethods
77
+
78
+ # Force the value of a configurable to be set without any
79
+ # respect for it's locked status.
80
+ def set( name, value )
81
+ name = name.to_sym
82
+
83
+ if self.class.configurables.has_key?( name )
84
+ _configurables[ name ] = value
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ def _configurables
91
+ @_configurables ||= {}
92
+ end
93
+
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,22 @@
1
+ require 'pathname'
2
+
3
+ class String
4
+
5
+ # Assuming the string is a file or path name, convert it into an
6
+ # absolute path.
7
+ def to_absolute_path
8
+ # Pathname is incompatible with Windows, but Windows doesn't have
9
+ # real symlinks so File.expand_path is safe.
10
+ if RUBY_PLATFORM =~ /(:?mswin|mingw)/
11
+ File.expand_path( self )
12
+
13
+ # Otherwise use Pathname#realpath which respects symlinks.
14
+ else
15
+ begin
16
+ File.expand_path( Pathname.new( self ).realpath.to_s )
17
+ rescue Errno::ENOENT
18
+ File.expand_path( Pathname.new( self ).cleanpath.to_s )
19
+ end
20
+ end
21
+ end
22
+ end