bunnicula 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. data/Rakefile +2 -2
  2. data/bunnicula.gemspec +180 -3
  3. data/lib/bunnicula/version.rb +1 -1
  4. data/vendor/daemon_kit/daemon-kit/Configuration.txt +110 -0
  5. data/vendor/daemon_kit/daemon-kit/Deployment.txt +113 -0
  6. data/vendor/daemon_kit/daemon-kit/History.txt +113 -0
  7. data/vendor/daemon_kit/daemon-kit/Logging.txt +96 -0
  8. data/vendor/daemon_kit/daemon-kit/Manifest.txt +172 -0
  9. data/vendor/daemon_kit/daemon-kit/PostInstall.txt +6 -0
  10. data/vendor/daemon_kit/daemon-kit/README.rdoc +126 -0
  11. data/vendor/daemon_kit/daemon-kit/Rakefile +29 -0
  12. data/vendor/daemon_kit/daemon-kit/RuoteParticipants.txt +113 -0
  13. data/vendor/daemon_kit/daemon-kit/TODO.txt +28 -0
  14. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/USAGE +7 -0
  15. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/daemon_kit_generator.rb +178 -0
  16. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/templates/README +48 -0
  17. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/templates/Rakefile +6 -0
  18. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/templates/bin/daemon.erb +7 -0
  19. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/templates/config/arguments.rb +12 -0
  20. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/templates/config/boot.rb +59 -0
  21. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/templates/config/environment.rb +23 -0
  22. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/templates/config/environments/development.rb +2 -0
  23. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/templates/config/environments/production.rb +5 -0
  24. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/templates/config/environments/test.rb +2 -0
  25. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/templates/config/post-daemonize/readme +5 -0
  26. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/templates/config/pre-daemonize/readme +12 -0
  27. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/templates/lib/daemon.rb +2 -0
  28. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/templates/libexec/daemon.erb +18 -0
  29. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/templates/script/console +3 -0
  30. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/templates/script/destroy +14 -0
  31. data/vendor/daemon_kit/daemon-kit/app_generators/daemon_kit/templates/script/generate +14 -0
  32. data/vendor/daemon_kit/daemon-kit/bin/daemon-kit +18 -0
  33. data/vendor/daemon_kit/daemon-kit/config/website.yml +2 -0
  34. data/vendor/daemon_kit/daemon-kit/daemon-kit.gemspec +264 -0
  35. data/vendor/daemon_kit/daemon-kit/daemon_generators/amqp/USAGE +5 -0
  36. data/vendor/daemon_kit/daemon-kit/daemon_generators/amqp/amqp_generator.rb +65 -0
  37. data/vendor/daemon_kit/daemon-kit/daemon_generators/amqp/templates/config/amqp.yml +28 -0
  38. data/vendor/daemon_kit/daemon-kit/daemon_generators/amqp/templates/config/initializers/amqp.rb +7 -0
  39. data/vendor/daemon_kit/daemon-kit/daemon_generators/amqp/templates/libexec/daemon.rb +37 -0
  40. data/vendor/daemon_kit/daemon-kit/daemon_generators/cron/USAGE +5 -0
  41. data/vendor/daemon_kit/daemon-kit/daemon_generators/cron/cron_generator.rb +64 -0
  42. data/vendor/daemon_kit/daemon-kit/daemon_generators/cron/templates/config/initializers/cron.rb +11 -0
  43. data/vendor/daemon_kit/daemon-kit/daemon_generators/cron/templates/libexec/daemon.rb +43 -0
  44. data/vendor/daemon_kit/daemon-kit/daemon_generators/cucumber/USAGE +11 -0
  45. data/vendor/daemon_kit/daemon-kit/daemon_generators/cucumber/cucumber_generator.rb +38 -0
  46. data/vendor/daemon_kit/daemon-kit/daemon_generators/cucumber/templates/cucumber +8 -0
  47. data/vendor/daemon_kit/daemon-kit/daemon_generators/cucumber/templates/cucumber.rake +13 -0
  48. data/vendor/daemon_kit/daemon-kit/daemon_generators/cucumber/templates/cucumber_environment.rb +2 -0
  49. data/vendor/daemon_kit/daemon-kit/daemon_generators/cucumber/templates/env.rb +7 -0
  50. data/vendor/daemon_kit/daemon-kit/daemon_generators/deploy_capistrano/deploy_capistrano_generator.rb +35 -0
  51. data/vendor/daemon_kit/daemon-kit/daemon_generators/deploy_capistrano/templates/Capfile +10 -0
  52. data/vendor/daemon_kit/daemon-kit/daemon_generators/deploy_capistrano/templates/USAGE +10 -0
  53. data/vendor/daemon_kit/daemon-kit/daemon_generators/deploy_capistrano/templates/config/deploy.rb +53 -0
  54. data/vendor/daemon_kit/daemon-kit/daemon_generators/deploy_capistrano/templates/config/deploy/production.rb +6 -0
  55. data/vendor/daemon_kit/daemon-kit/daemon_generators/deploy_capistrano/templates/config/deploy/staging.rb +6 -0
  56. data/vendor/daemon_kit/daemon-kit/daemon_generators/deploy_capistrano/templates/config/environments/staging.rb +0 -0
  57. data/vendor/daemon_kit/daemon-kit/daemon_generators/jabber/USAGE +5 -0
  58. data/vendor/daemon_kit/daemon-kit/daemon_generators/jabber/jabber_generator.rb +65 -0
  59. data/vendor/daemon_kit/daemon-kit/daemon_generators/jabber/templates/config/initializers/jabber.rb +7 -0
  60. data/vendor/daemon_kit/daemon-kit/daemon_generators/jabber/templates/config/jabber.yml +26 -0
  61. data/vendor/daemon_kit/daemon-kit/daemon_generators/jabber/templates/libexec/daemon.rb +27 -0
  62. data/vendor/daemon_kit/daemon-kit/daemon_generators/nanite_agent/USAGE +5 -0
  63. data/vendor/daemon_kit/daemon-kit/daemon_generators/nanite_agent/nanite_agent_generator.rb +68 -0
  64. data/vendor/daemon_kit/daemon-kit/daemon_generators/nanite_agent/templates/config/initializers/nanite_agent.rb +6 -0
  65. data/vendor/daemon_kit/daemon-kit/daemon_generators/nanite_agent/templates/config/nanite.yml +35 -0
  66. data/vendor/daemon_kit/daemon-kit/daemon_generators/nanite_agent/templates/lib/actors/sample.rb +11 -0
  67. data/vendor/daemon_kit/daemon-kit/daemon_generators/nanite_agent/templates/libexec/daemon.rb +31 -0
  68. data/vendor/daemon_kit/daemon-kit/daemon_generators/rspec/USAGE +5 -0
  69. data/vendor/daemon_kit/daemon-kit/daemon_generators/rspec/rspec_generator.rb +55 -0
  70. data/vendor/daemon_kit/daemon-kit/daemon_generators/rspec/templates/spec.rb +11 -0
  71. data/vendor/daemon_kit/daemon-kit/daemon_generators/rspec/templates/spec/spec.opts +1 -0
  72. data/vendor/daemon_kit/daemon-kit/daemon_generators/rspec/templates/spec/spec_helper.rb +23 -0
  73. data/vendor/daemon_kit/daemon-kit/daemon_generators/rspec/templates/tasks/rspec.rake +19 -0
  74. data/vendor/daemon_kit/daemon-kit/daemon_generators/ruote/USAGE +5 -0
  75. data/vendor/daemon_kit/daemon-kit/daemon_generators/ruote/ruote_generator.rb +67 -0
  76. data/vendor/daemon_kit/daemon-kit/daemon_generators/ruote/templates/config/amqp.yml +30 -0
  77. data/vendor/daemon_kit/daemon-kit/daemon_generators/ruote/templates/config/initializers/ruote.rb +13 -0
  78. data/vendor/daemon_kit/daemon-kit/daemon_generators/ruote/templates/config/ruote.yml +23 -0
  79. data/vendor/daemon_kit/daemon-kit/daemon_generators/ruote/templates/lib/daemon.rb +4 -0
  80. data/vendor/daemon_kit/daemon-kit/daemon_generators/ruote/templates/lib/sample.rb +26 -0
  81. data/vendor/daemon_kit/daemon-kit/daemon_generators/ruote/templates/libexec/daemon.rb +33 -0
  82. data/vendor/daemon_kit/daemon-kit/daemon_generators/test_unit/USAGE +5 -0
  83. data/vendor/daemon_kit/daemon-kit/daemon_generators/test_unit/templates/tasks/test_unit.rake +7 -0
  84. data/vendor/daemon_kit/daemon-kit/daemon_generators/test_unit/templates/test/test.rb +9 -0
  85. data/vendor/daemon_kit/daemon-kit/daemon_generators/test_unit/templates/test/test_helper.rb +6 -0
  86. data/vendor/daemon_kit/daemon-kit/daemon_generators/test_unit/test_unit_generator.rb +51 -0
  87. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit.rb +62 -0
  88. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/abstract_logger.rb +241 -0
  89. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/amqp.rb +38 -0
  90. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/application.rb +188 -0
  91. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/arguments.rb +170 -0
  92. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/commands/console.rb +38 -0
  93. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/config.rb +108 -0
  94. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/console_daemon.rb +2 -0
  95. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/core_ext.rb +1 -0
  96. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/core_ext/configurable.rb +96 -0
  97. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/core_ext/string.rb +22 -0
  98. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/cron.rb +48 -0
  99. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/cucumber/world.rb +38 -0
  100. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/deployment/capistrano.rb +482 -0
  101. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/em.rb +43 -0
  102. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/error_handlers/base.rb +32 -0
  103. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/error_handlers/hoptoad.rb +61 -0
  104. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/error_handlers/mail.rb +85 -0
  105. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/exceptions.rb +15 -0
  106. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/initializer.rb +453 -0
  107. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/jabber.rb +170 -0
  108. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/nanite.rb +7 -0
  109. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/nanite/agent.rb +77 -0
  110. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/pid_file.rb +61 -0
  111. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/ruote_participants.rb +119 -0
  112. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/ruote_pseudo_participant.rb +68 -0
  113. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/ruote_workitem.rb +177 -0
  114. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/safety.rb +85 -0
  115. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/tasks.rb +2 -0
  116. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/tasks/environment.rake +10 -0
  117. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/tasks/framework.rake +120 -0
  118. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/tasks/god.rake +62 -0
  119. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/tasks/log.rake +8 -0
  120. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/tasks/monit.rake +29 -0
  121. data/vendor/daemon_kit/daemon-kit/lib/daemon_kit/xmpp.rb +31 -0
  122. data/vendor/daemon_kit/daemon-kit/script/console +10 -0
  123. data/vendor/daemon_kit/daemon-kit/script/destroy +14 -0
  124. data/vendor/daemon_kit/daemon-kit/script/generate +14 -0
  125. data/vendor/daemon_kit/daemon-kit/script/txt2html +71 -0
  126. data/vendor/daemon_kit/daemon-kit/spec/abstract_logger_spec.rb +126 -0
  127. data/vendor/daemon_kit/daemon-kit/spec/argument_spec.rb +70 -0
  128. data/vendor/daemon_kit/daemon-kit/spec/config_spec.rb +79 -0
  129. data/vendor/daemon_kit/daemon-kit/spec/configurable_spec.rb +56 -0
  130. data/vendor/daemon_kit/daemon-kit/spec/daemon_kit_spec.rb +7 -0
  131. data/vendor/daemon_kit/daemon-kit/spec/error_handlers_spec.rb +23 -0
  132. data/vendor/daemon_kit/daemon-kit/spec/fixtures/env.yml +15 -0
  133. data/vendor/daemon_kit/daemon-kit/spec/fixtures/noenv.yml +4 -0
  134. data/vendor/daemon_kit/daemon-kit/spec/initializer_spec.rb +26 -0
  135. data/vendor/daemon_kit/daemon-kit/spec/spec.opts +1 -0
  136. data/vendor/daemon_kit/daemon-kit/spec/spec_helper.rb +27 -0
  137. data/vendor/daemon_kit/daemon-kit/tasks/cucumber.rake +13 -0
  138. data/vendor/daemon_kit/daemon-kit/tasks/rspec.rake +21 -0
  139. data/vendor/daemon_kit/daemon-kit/tasks/tests.rake +6 -0
  140. data/vendor/daemon_kit/daemon-kit/templates/god/god.erb +69 -0
  141. data/vendor/daemon_kit/daemon-kit/templates/monit/monit.erb +14 -0
  142. data/vendor/daemon_kit/daemon-kit/test/test_amqp_generator.rb +48 -0
  143. data/vendor/daemon_kit/daemon-kit/test/test_cron_generator.rb +45 -0
  144. data/vendor/daemon_kit/daemon-kit/test/test_daemon-kit_generator.rb +84 -0
  145. data/vendor/daemon_kit/daemon-kit/test/test_daemon_kit_config.rb +28 -0
  146. data/vendor/daemon_kit/daemon-kit/test/test_deploy_capistrano_generator.rb +48 -0
  147. data/vendor/daemon_kit/daemon-kit/test/test_generator_helper.rb +29 -0
  148. data/vendor/daemon_kit/daemon-kit/test/test_helper.rb +7 -0
  149. data/vendor/daemon_kit/daemon-kit/test/test_jabber_generator.rb +49 -0
  150. data/vendor/daemon_kit/daemon-kit/test/test_nanite_agent_generator.rb +49 -0
  151. data/vendor/daemon_kit/daemon-kit/test/test_ruote_generator.rb +51 -0
  152. data/vendor/daemon_kit/daemon-kit/test/test_test_unit_generator.rb +46 -0
  153. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail.rb +5 -0
  154. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/address.rb +426 -0
  155. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/attachments.rb +46 -0
  156. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/base64.rb +46 -0
  157. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/compat.rb +41 -0
  158. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/config.rb +67 -0
  159. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/core_extensions.rb +63 -0
  160. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/encode.rb +581 -0
  161. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/header.rb +960 -0
  162. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/index.rb +9 -0
  163. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/interface.rb +1130 -0
  164. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/loader.rb +3 -0
  165. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/mail.rb +578 -0
  166. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/mailbox.rb +495 -0
  167. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/main.rb +6 -0
  168. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/mbox.rb +3 -0
  169. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/net.rb +248 -0
  170. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/obsolete.rb +132 -0
  171. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/parser.rb +1476 -0
  172. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/port.rb +379 -0
  173. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/quoting.rb +118 -0
  174. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/require_arch.rb +58 -0
  175. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/scanner.rb +49 -0
  176. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/scanner_r.rb +261 -0
  177. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/stringio.rb +280 -0
  178. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/utils.rb +337 -0
  179. data/vendor/daemon_kit/daemon-kit/vendor/tmail-1.2.3/tmail/version.rb +39 -0
  180. data/vendor/daemon_kit/daemon-kit/vendor/tmail.rb +13 -0
  181. metadata +181 -4
@@ -0,0 +1,43 @@
1
+ module DaemonKit
2
+
3
+ # EventMachine forms a critical part of the daemon-kit toolset, and
4
+ # especially of daemon process developers.
5
+ #
6
+ # This class abstracts away the difficulties of managing multiple
7
+ # libraries that all utilize the event reactor.
8
+ class EM
9
+
10
+ class << self
11
+
12
+ # Start a reactor, just like classical EM.run. If the block is
13
+ # provided, the method will block and call the provided block
14
+ # argument inside the running reactor. If the block argument is
15
+ # not provided the reactor will be started in a separate thread
16
+ # and the program will continue to run after the method. All the
17
+ # signal traps are configured to shutdown the reactor when the
18
+ # daemon exists.
19
+ def run(&block)
20
+ if ::EM.reactor_running?
21
+ DaemonKit.logger.warn "EventMachine reactor already running"
22
+ block.call if block_given?
23
+
24
+ else
25
+ if block_given?
26
+ ::EM.run { block.call }
27
+ else
28
+ Thread.main[:_dk_reactor] = Thread.new { EM.run {} }
29
+ DaemonKit.trap( 'INT' ) { DaemonKit::EM.stop }
30
+ DaemonKit.trap( 'TERM' ) { DaemonKit::EM.stop }
31
+ end
32
+ end
33
+ end
34
+
35
+ # Stop the reactor
36
+ def stop
37
+ ::EM.stop_event_loop if ::EM.reactor_running?
38
+ Thread.main[:_dk_reactor].join
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,32 @@
1
+ module DaemonKit
2
+ module ErrorHandlers
3
+ # Error handlers in DaemonKit are used by the #Safety class. Any
4
+ # error handler has to support the interface provided by this
5
+ # class. It's also required that safety handlers implement a
6
+ # singleton approach (handled by default by #Base).
7
+ class Base
8
+
9
+ class << self
10
+
11
+ @instance = nil
12
+
13
+ def instance
14
+ @instance ||= new
15
+ end
16
+ private :new
17
+
18
+ # When we're inherited, immediately register the handler with
19
+ # the safety net
20
+ def inherited( child ) #:nodoc:
21
+ Safety.register_error_handler( child )
22
+ end
23
+ end
24
+
25
+ # Error handlers should overwrite this method and implement
26
+ # their own reporting method.
27
+ def handle_exception( exception )
28
+ raise NoMethodError, "Error handler doesn't support #handle_exception"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,61 @@
1
+ require 'net/http'
2
+
3
+ module DaemonKit
4
+ module ErrorHandlers
5
+ # Error reporting via Hoptoad.
6
+ class Hoptoad < Base
7
+
8
+ # Your hoptoad API key
9
+ @api_key = nil
10
+ attr_accessor :api_key
11
+
12
+ def handle_exception( exception )
13
+ headers = {
14
+ 'Content-type' => 'application/x-yaml',
15
+ 'Accept' => 'text/xml, application/xml'
16
+ }
17
+
18
+ http = Net::HTTP.new( url.host, url.port )
19
+ data = clean_exception( exception )
20
+
21
+ response = begin
22
+ http.post( url.path, {"notice" => data}.to_yaml, headers )
23
+ rescue TimeoutError => e
24
+ DaemonKit.logger.error("Timeout while contacting the Hoptoad server.")
25
+ nil
26
+ end
27
+ case response
28
+ when Net::HTTPSuccess then
29
+ DaemonKit.logger.info "Hoptoad Success: #{response.class}"
30
+ else
31
+ DaemonKit.logger.error "Hoptoad Failure: #{response.class}\n#{response.body if response.respond_to? :body}"
32
+ end
33
+ end
34
+
35
+ def url
36
+ URI.parse("http://hoptoadapp.com/notices/")
37
+ end
38
+
39
+ def clean_exception( exception )
40
+ data = {
41
+ :api_key => self.api_key,
42
+ :error_class => exception.class.name,
43
+ :error_message => "#{exception.class.name}: #{exception.message}",
44
+ :backtrace => exception.backtrace,
45
+ :environment => ENV.to_hash,
46
+ :request => {},
47
+ :session => {}
48
+ }
49
+
50
+ stringify_keys( data )
51
+ end
52
+
53
+ def stringify_keys(hash) #:nodoc:
54
+ hash.inject({}) do |h, pair|
55
+ h[pair.first.to_s] = pair.last.is_a?(Hash) ? stringify_keys(pair.last) : pair.last
56
+ h
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,85 @@
1
+ require DaemonKit.framework_root + '/vendor/tmail'
2
+ require 'net/smtp'
3
+
4
+ module DaemonKit
5
+ module ErrorHandlers
6
+ # Send an email notification of the exception via SMTP.
7
+ class Mail < Base
8
+
9
+ # SMTP hostname
10
+ @host = 'localhost'
11
+
12
+ # SMTP port
13
+ @port = 25
14
+
15
+ # Recipients of the notification
16
+ @recipients = []
17
+
18
+ # Subject prefix
19
+ @prefix = '[DAEMON-KIT]'
20
+
21
+ # Sender address
22
+ @sender = 'daemon-kit'
23
+
24
+ # SMTP username
25
+ @username = nil
26
+
27
+ # SMTP password
28
+ @password = nil
29
+
30
+ # Authentication mechanism (:plain, :login, or :cram_md5)
31
+ @authentication = nil
32
+
33
+ # Use TLS?
34
+ @tls = false
35
+
36
+ # Domain used when talking to SMTP server
37
+ @domain = 'localhost.localdomain'
38
+
39
+ class << self
40
+ attr_accessor :host, :port, :recipients, :prefix, :sender, :username,
41
+ :password, :authentication, :tls, :domain
42
+ end
43
+
44
+ [ :host, :port, :recipients, :prefix, :sender, :username, :password,
45
+ :authentication, :tls, :domain ].each do |cm|
46
+ class_eval(<<-EOM, __FILE__, __LINE__)
47
+ def #{cm}=( val )
48
+ self.class.#{cm} = val
49
+ end
50
+ EOM
51
+ end
52
+
53
+ def handle_exception( exception )
54
+
55
+ mail = TMail::Mail.new
56
+ mail.to = self.class.recipients
57
+ mail.from = self.class.sender
58
+ mail.subject = "#{self.class.prefix} #{exception.message}"
59
+ mail.set_content_type 'text', 'plain'
60
+ mail.mime_version = '1.0'
61
+ mail.date = Time.now
62
+
63
+ mail.body = <<EOF
64
+ DaemonKit caught an exception inside #{DaemonKit.configuration.daemon_name}.
65
+
66
+ Message: #{exception.message}
67
+ Backtrace:
68
+ #{exception.backtrace.join("\n ")}
69
+
70
+ Environment: #{ENV.inspect}
71
+ EOF
72
+ begin
73
+ smtp = Net::SMTP.new( self.class.host, self.class.port )
74
+ smtp.enable_starttls_auto if self.class.tls && smtp.respond_to?(:enable_starttls_auto)
75
+ smtp.start( self.class.domain, self.class.username, self.class.password,
76
+ self.class.authentication ) do |smtp|
77
+ smtp.sendmail( mail.to_s, mail.from, mail.to )
78
+ end
79
+ rescue => e
80
+ DaemonKit.logger.error "Failed to send exception mail: #{e.message}" if DaemonKit.logger
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,15 @@
1
+ module DaemonKit
2
+ # The core of daemon-kit exceptions
3
+ class Exception < ::StandardError
4
+ end
5
+
6
+ # Raised when no class is registered to process a ruote workitem
7
+ class MissingParticipant < Exception; end
8
+
9
+ # Raised when the daemon itself cannot be found.
10
+ class DaemonNotFound < Exception
11
+ def initialize( file )
12
+ super "No daemon found at the path '#{file}'"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,453 @@
1
+ require 'pathname'
2
+
3
+ DAEMON_ENV = (ENV['DAEMON_ENV'] || 'development').dup unless defined?(DAEMON_ENV)
4
+
5
+ # Absolute paths to the daemon_kit libraries added to $:
6
+ incdir = ( File.dirname(__FILE__) + '/..' )
7
+ absincdir = if RUBY_PLATFORM =~ /(:?mswin|mingw)/
8
+ File.expand_path( incdir )
9
+ else
10
+ File.expand_path( Pathname.new( incdir ).realpath.to_s )
11
+ end
12
+ $:.unshift absincdir unless $:.include?( absincdir )
13
+
14
+ require 'daemon_kit'
15
+
16
+ module DaemonKit
17
+
18
+ class << self
19
+
20
+ def configuration
21
+ @configuration
22
+ end
23
+
24
+ def configuration=( configuration )
25
+ @configuration = configuration
26
+ end
27
+
28
+ def arguments
29
+ @arguments
30
+ end
31
+
32
+ def arguments=( args )
33
+ @arguments = args
34
+ end
35
+
36
+ def trap( *args, &block )
37
+ self.configuration.trap( *args, &block )
38
+ end
39
+
40
+ def at_shutdown( &block )
41
+ self.configuration.at_shutdown( &block )
42
+ end
43
+
44
+ end
45
+
46
+
47
+ # This class does all the nightmare work of setting up a working
48
+ # environment for your daemon.
49
+ class Initializer
50
+
51
+ attr_reader :configuration
52
+
53
+ def self.run
54
+ configuration = DaemonKit.configuration || Configuration.new
55
+
56
+ yield configuration if block_given?
57
+ initializer = new configuration
58
+ initializer.before_daemonize
59
+ initializer
60
+ end
61
+
62
+ def self.continue!
63
+ initializer = new DaemonKit.configuration
64
+ initializer.after_daemonize
65
+ end
66
+
67
+ def self.shutdown( clean = false, do_exit = false )
68
+ return unless $daemon_kit_shutdown_hooks_ran.nil?
69
+ $daemon_kit_shutdown_hooks_ran = true
70
+
71
+ DaemonKit.logger.info "Running shutdown hooks"
72
+
73
+ DaemonKit.configuration.shutdown_hooks.each do |hook|
74
+ begin
75
+ hook.call
76
+ rescue => e
77
+ DaemonKit.logger.exception( e )
78
+ end
79
+ end
80
+
81
+ log_exceptions if DaemonKit.configuration.backtraces && !clean
82
+
83
+ DaemonKit.logger.warn "Shutting down #{DaemonKit.configuration.daemon_name}"
84
+
85
+ exit if do_exit
86
+ end
87
+
88
+ def initialize( configuration )
89
+ @configuration = configuration
90
+ end
91
+
92
+ def before_daemonize
93
+ DaemonKit.configuration = @configuration
94
+
95
+ set_load_path
96
+ load_gems
97
+ load_patches
98
+ load_environment
99
+ load_predaemonize_configs
100
+ end
101
+
102
+ def after_daemonize
103
+ set_umask
104
+
105
+ initialize_logger
106
+ initialize_signal_traps
107
+
108
+ include_core_lib
109
+ load_postdaemonize_configs
110
+ configure_backtraces
111
+
112
+ set_process_name
113
+
114
+ DaemonKit.logger.info( "DaemonKit (#{DaemonKit::VERSION}) booted, now running #{DaemonKit.configuration.daemon_name}" )
115
+
116
+ if DaemonKit.configuration.user || DaemonKit.configuration.group
117
+ euid = Process.euid
118
+ egid = Process.egid
119
+ uid = Process.uid
120
+ gid = Process.gid
121
+ DaemonKit.logger.info( "DaemonKit dropped privileges to: #{euid} (EUID), #{egid} (EGID), #{uid} (UID), #{gid} (GID)" )
122
+ end
123
+ end
124
+
125
+ def set_load_path
126
+ configuration.load_paths.each do |d|
127
+ $:.unshift( "#{DAEMON_ROOT}/#{d}" ) if File.directory?( "#{DAEMON_ROOT}/#{d}" )
128
+ end
129
+ end
130
+
131
+ def load_gems
132
+
133
+ end
134
+
135
+ def load_patches
136
+
137
+ end
138
+
139
+ def load_environment
140
+ # Needs to be global to prevent loading the files twice
141
+ return if $_daemon_environment_loaded
142
+ $_daemon_environment_loaded = true
143
+
144
+ config = configuration
145
+
146
+ eval(IO.read(configuration.environment_path), binding, configuration.environment_path)
147
+
148
+ eval(IO.read(configuration.daemon_initializer), binding, configuration.daemon_initializer) if File.exist?( configuration.daemon_initializer )
149
+ end
150
+
151
+ def load_predaemonize_configs
152
+ Dir[ File.join( DAEMON_ROOT, 'config', 'pre-daemonize', '*.rb' ) ].each do |f|
153
+ next if File.basename( f ) == File.basename( configuration.daemon_initializer )
154
+
155
+ require f
156
+ end
157
+ end
158
+
159
+ def load_postdaemonize_configs
160
+ Dir[ File.join( DAEMON_ROOT, 'config', 'post-daemonize', '*.rb' ) ].each do |f|
161
+ require f
162
+ end
163
+ end
164
+
165
+ def set_umask
166
+ File.umask configuration.umask
167
+ end
168
+
169
+ def initialize_logger
170
+ return if DaemonKit.logger
171
+
172
+ unless logger = configuration.logger
173
+ logger = AbstractLogger.new( configuration.log_path )
174
+ logger.level = configuration.log_level
175
+ logger.copy_to_stdout = configuration.log_stdout
176
+ end
177
+
178
+ DaemonKit.logger = logger
179
+
180
+ DaemonKit.logger.info "DaemonKit (#{DaemonKit::VERSION}) booting in #{DAEMON_ENV} mode"
181
+
182
+ configuration.trap("USR1") {
183
+ DaemonKit.logger.level = DaemonKit.logger.debug? ? :info : :debug
184
+ DaemonKit.logger.info "Log level changed to #{DaemonKit.logger.debug? ? 'DEBUG' : 'INFO' }"
185
+ }
186
+ configuration.trap("USR2") {
187
+ DaemonKit.logger.level = :debug
188
+ DaemonKit.logger.info "Log level changed to DEBUG"
189
+ }
190
+ configuration.trap("HUP") {
191
+ DaemonKit.logger.close
192
+ }
193
+ end
194
+
195
+ def initialize_signal_traps
196
+ # Only exit the process if we're not in the 'test' environment
197
+ term_proc = Proc.new { DaemonKit::Initializer.shutdown( true, DAEMON_ENV != 'test' ) }
198
+ configuration.trap( 'INT', term_proc )
199
+ configuration.trap( 'TERM', term_proc )
200
+ at_exit { DaemonKit::Initializer.shutdown }
201
+ end
202
+
203
+ def include_core_lib
204
+ if File.exists?( core_lib = File.join( DAEMON_ROOT, 'lib', configuration.daemon_name + '.rb' ) )
205
+ require core_lib
206
+ end
207
+ end
208
+
209
+ def configure_backtraces
210
+ Thread.abort_on_exception = configuration.backtraces
211
+ end
212
+
213
+ def set_process_name
214
+ $0 = configuration.daemon_name
215
+ end
216
+
217
+ def self.log_exceptions
218
+ trace_file = File.join( DaemonKit.root, 'log', "backtrace-#{Time.now.strftime('%Y%m%d%H%M%S')}-#{Process.pid}.log" )
219
+ trace_log = Logger.new( trace_file )
220
+
221
+ # Find the last exception
222
+ e = nil
223
+ ObjectSpace.each_object {|o|
224
+ if ::Exception === o
225
+ e = o
226
+ end
227
+ }
228
+
229
+ trace_log.info "*** Below you'll find the most recent exception thrown, this will likely (but not certainly) be the exception that made #{DaemonKit.configuration.daemon_name} exit abnormally ***"
230
+ trace_log.error e
231
+
232
+ trace_log.info "*** Below you'll find all the exception objects in memory, some of them may have been thrown in your application, others may just be in memory because they are standard exceptions ***"
233
+ ObjectSpace.each_object {|o|
234
+ if ::Exception === o
235
+ trace_log.error o
236
+ end
237
+ }
238
+
239
+ trace_log.close
240
+ end
241
+ end
242
+
243
+ # Holds our various configuration values
244
+ class Configuration
245
+
246
+ include Configurable
247
+
248
+ # Root to the daemon
249
+ attr_reader :root_path
250
+
251
+ # List of load paths
252
+ attr_accessor :load_paths
253
+
254
+ # Custom logger instance to use
255
+ attr_accessor :logger
256
+
257
+ # The log level to use, defaults to DEBUG
258
+ attr_reader :log_level
259
+
260
+ # Path to the log file, defaults to 'log/<environment>.log'
261
+ configurable :log_path
262
+
263
+ # Duplicate log data to stdout
264
+ attr_accessor :log_stdout
265
+
266
+ # Path to the pid file, defaults to 'log/<daemon_name>.pid'
267
+ attr_accessor :pid_file
268
+
269
+ # The application name
270
+ configurable :daemon_name, :locked => true
271
+
272
+ # Use the force kill patch? Give the number of seconds
273
+ configurable :force_kill_wait
274
+
275
+ # Should be log backtraces
276
+ configurable :backtraces, false
277
+
278
+ # Configurable umask
279
+ configurable :umask, 0022
280
+
281
+ # Configurable user
282
+ configurable :user, :locked => true
283
+
284
+ # Confgiruable group
285
+ configurable :group, :locked => true
286
+
287
+ # Collection of signal traps
288
+ attr_reader :signal_traps
289
+
290
+ # Our safety net (#Safety) instance
291
+ attr_accessor :safety_net
292
+
293
+ # :nodoc: Shutdown hooks
294
+ attr_reader :shutdown_hooks
295
+
296
+ def initialize
297
+ parse_arguments!
298
+
299
+ set_root_path!
300
+ set_daemon_defaults!
301
+
302
+ self.load_paths = default_load_paths
303
+ self.log_level ||= default_log_level
304
+ self.log_path ||= default_log_path
305
+
306
+ self.force_kill_wait = false
307
+
308
+ self.safety_net = DaemonKit::Safety.instance
309
+
310
+ @signal_traps = {}
311
+ @shutdown_hooks = []
312
+ end
313
+
314
+ def environment
315
+ ::DAEMON_ENV
316
+ end
317
+
318
+ # The path to the current environment's file (<tt>development.rb</tt>, etc.). By
319
+ # default the file is at <tt>config/environments/#{environment}.rb</tt>.
320
+ def environment_path
321
+ "#{root_path}/config/environments/#{environment}.rb"
322
+ end
323
+
324
+ def daemon_initializer
325
+ "#{root_path}/config/initializers/#{self.daemon_name}.rb"
326
+ end
327
+
328
+ # Add a trap for the specified signal, can be code block or a proc
329
+ def trap( signal, proc = nil, &block )
330
+ return if proc.nil? && !block_given?
331
+
332
+ # One step towards running on windows, not enough though
333
+ unless Signal.list.include?( signal )
334
+ DaemonKit.logger.warn( "Trapping #{signal} signals not supported on this platform" )
335
+ return
336
+ end
337
+
338
+ unless @signal_traps.has_key?( signal )
339
+ set_trap( signal )
340
+ end
341
+
342
+ @signal_traps[signal].unshift( proc || block )
343
+ end
344
+
345
+ # Add a block or proc to be called during shutdown
346
+ def at_shutdown( proc = nil, &block )
347
+ return if proc.nil? && !block_given?
348
+
349
+ @shutdown_hooks << ( proc || block )
350
+ end
351
+
352
+ def pid_file
353
+ @pid_file ||= "#{File.dirname(self.log_path)}/#{self.daemon_name}.pid"
354
+ end
355
+
356
+ # Set the log level
357
+ def log_level=( level )
358
+ @log_level = level
359
+ DaemonKit.logger.level = @log_level if DaemonKit.logger
360
+ end
361
+
362
+ protected
363
+
364
+ def run_traps( signal )
365
+ DaemonKit.logger.info "Running signal traps for #{signal}"
366
+ self.signal_traps[ signal ].each { |trap| trap.call }
367
+ end
368
+
369
+ private
370
+
371
+ def set_trap( signal )
372
+ DaemonKit.logger.info "Setting up trap for #{signal}"
373
+ @signal_traps[ signal ] = []
374
+ Signal.trap( signal, Proc.new { self.run_traps( signal ) } )
375
+ end
376
+
377
+ def parse_arguments!
378
+ return unless own_args?
379
+
380
+ configs = Arguments.configuration( ARGV ).first
381
+ @unused_arguments = {}
382
+
383
+ configs.each do |c|
384
+ k,v = c.split('=')
385
+
386
+ if v.nil?
387
+ error( "#{k} has no value" )
388
+ next
389
+ end
390
+
391
+ begin
392
+ if self.respond_to?( k )
393
+ self.send( "#{k}=", v ) # pid_file = /var/run/foo.pid
394
+ else
395
+ @unused_arguments[ k ] = v
396
+ end
397
+ rescue => e
398
+ error( "Couldn't set `#{k}' to `#{v}': #{e.message}" )
399
+ end
400
+ end
401
+ end
402
+
403
+ # DANGEROUS: Change the value of DAEMON_ENV
404
+ def environment=( env )
405
+ ::DAEMON_ENV.replace( env )
406
+ end
407
+
408
+ def set_root_path!
409
+ raise "DAEMON_ROOT is not set" unless defined?(::DAEMON_ROOT)
410
+ raise "DAEMON_ROOT is not a directory" unless File.directory?(::DAEMON_ROOT)
411
+
412
+ @root_path = ::DAEMON_ROOT.to_absolute_path
413
+
414
+ Object.const_set(:RELATIVE_DAEMON_ROOT, ::DAEMON_ROOT.dup) unless defined?(::RELATIVE_DAEMON_ROOT)
415
+ ::DAEMON_ROOT.replace @root_path
416
+ end
417
+
418
+ def set_daemon_defaults!
419
+ self.log_stdout = false
420
+ end
421
+
422
+ def default_load_paths
423
+ [ 'lib' ]
424
+ end
425
+
426
+ def default_log_path
427
+ File.join(root_path, 'log', "#{environment}.log")
428
+ end
429
+
430
+ def default_log_level
431
+ environment == 'production' ? :info : :debug
432
+ end
433
+
434
+ def error( msg )
435
+ msg = "[E] Configuration: #{msg}"
436
+
437
+ if DaemonKit.logger
438
+ DaemonKit.logger.error( msg )
439
+ else
440
+ STDERR.puts msg
441
+ end
442
+ end
443
+
444
+ # If we are executed with any of these commands, don't allow
445
+ # arguments to be parsed cause they will interfere with the
446
+ # script encapsulating DaemonKit, like capistrano
447
+ def own_args?
448
+ Arguments.parser_available
449
+ end
450
+ end
451
+
452
+
453
+ end