apphunkd 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. data/.document +5 -0
  2. data/.gitignore +22 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +18 -0
  5. data/Rakefile +78 -0
  6. data/TODO +1 -0
  7. data/VERSION +1 -0
  8. data/apphunkd.gemspec +258 -0
  9. data/bin/apphunkd +4 -0
  10. data/config/arguments.rb +12 -0
  11. data/config/boot.rb +68 -0
  12. data/config/environment.rb +23 -0
  13. data/config/environments/development.rb +2 -0
  14. data/config/environments/production.rb +2 -0
  15. data/config/environments/test.rb +2 -0
  16. data/config/post-daemonize/readme +5 -0
  17. data/config/pre-daemonize/readme +12 -0
  18. data/config/pre-daemonize/requires.rb +2 -0
  19. data/lib/apphunkd.rb +25 -0
  20. data/lib/apphunkd/api.rb +5 -0
  21. data/lib/apphunkd/api/service.rb +27 -0
  22. data/lib/apphunkd/queue.rb +96 -0
  23. data/lib/apphunkd/remote.rb +29 -0
  24. data/lib/apphunkd/remote/result.rb +15 -0
  25. data/libexec/apphunkd-daemon.rb +13 -0
  26. data/script/console +3 -0
  27. data/script/destroy +14 -0
  28. data/script/generate +14 -0
  29. data/spec/lib/apphunkd/api/service_spec.rb +47 -0
  30. data/spec/lib/apphunkd/queue_spec.rb +129 -0
  31. data/spec/lib/apphunkd/remote_spec.rb +61 -0
  32. data/spec/lib/apphunkd_spec.rb +50 -0
  33. data/spec/spec.opts +1 -0
  34. data/spec/spec_helper.rb +13 -0
  35. data/support/apphunkd.initd +47 -0
  36. data/support/apphunkd.monitrc +3 -0
  37. data/tasks/rspec.rake +21 -0
  38. data/vendor/daemon-kit/Configuration.txt +102 -0
  39. data/vendor/daemon-kit/Deployment.txt +113 -0
  40. data/vendor/daemon-kit/History.txt +97 -0
  41. data/vendor/daemon-kit/Logging.txt +92 -0
  42. data/vendor/daemon-kit/Manifest.txt +166 -0
  43. data/vendor/daemon-kit/PostInstall.txt +6 -0
  44. data/vendor/daemon-kit/README.rdoc +130 -0
  45. data/vendor/daemon-kit/Rakefile +37 -0
  46. data/vendor/daemon-kit/RuoteParticipants.txt +113 -0
  47. data/vendor/daemon-kit/TODO.txt +37 -0
  48. data/vendor/daemon-kit/app_generators/daemon_kit/USAGE +7 -0
  49. data/vendor/daemon-kit/app_generators/daemon_kit/daemon_kit_generator.rb +161 -0
  50. data/vendor/daemon-kit/app_generators/daemon_kit/templates/README +48 -0
  51. data/vendor/daemon-kit/app_generators/daemon_kit/templates/Rakefile +6 -0
  52. data/vendor/daemon-kit/app_generators/daemon_kit/templates/bin/daemon.erb +7 -0
  53. data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/arguments.rb +12 -0
  54. data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/boot.rb +68 -0
  55. data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/environment.rb +23 -0
  56. data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/environments/development.rb +2 -0
  57. data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/environments/production.rb +2 -0
  58. data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/environments/test.rb +2 -0
  59. data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/post-daemonize/readme +5 -0
  60. data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/pre-daemonize/readme +12 -0
  61. data/vendor/daemon-kit/app_generators/daemon_kit/templates/lib/daemon.rb +2 -0
  62. data/vendor/daemon-kit/app_generators/daemon_kit/templates/libexec/daemon.erb +18 -0
  63. data/vendor/daemon-kit/app_generators/daemon_kit/templates/script/console +3 -0
  64. data/vendor/daemon-kit/app_generators/daemon_kit/templates/script/destroy +14 -0
  65. data/vendor/daemon-kit/app_generators/daemon_kit/templates/script/generate +14 -0
  66. data/vendor/daemon-kit/bin/daemon_kit +18 -0
  67. data/vendor/daemon-kit/daemon_generators/amqp/USAGE +5 -0
  68. data/vendor/daemon-kit/daemon_generators/amqp/amqp_generator.rb +65 -0
  69. data/vendor/daemon-kit/daemon_generators/amqp/templates/config/amqp.yml +28 -0
  70. data/vendor/daemon-kit/daemon_generators/amqp/templates/config/initializers/amqp.rb +7 -0
  71. data/vendor/daemon-kit/daemon_generators/amqp/templates/libexec/daemon.rb +37 -0
  72. data/vendor/daemon-kit/daemon_generators/cron/USAGE +5 -0
  73. data/vendor/daemon-kit/daemon_generators/cron/cron_generator.rb +64 -0
  74. data/vendor/daemon-kit/daemon_generators/cron/templates/config/initializers/cron.rb +11 -0
  75. data/vendor/daemon-kit/daemon_generators/cron/templates/libexec/daemon.rb +43 -0
  76. data/vendor/daemon-kit/daemon_generators/cucumber/USAGE +11 -0
  77. data/vendor/daemon-kit/daemon_generators/cucumber/cucumber_generator.rb +38 -0
  78. data/vendor/daemon-kit/daemon_generators/cucumber/templates/cucumber +8 -0
  79. data/vendor/daemon-kit/daemon_generators/cucumber/templates/cucumber.rake +13 -0
  80. data/vendor/daemon-kit/daemon_generators/cucumber/templates/cucumber_environment.rb +2 -0
  81. data/vendor/daemon-kit/daemon_generators/cucumber/templates/env.rb +7 -0
  82. data/vendor/daemon-kit/daemon_generators/deploy_capistrano/deploy_capistrano_generator.rb +35 -0
  83. data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/Capfile +10 -0
  84. data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/USAGE +10 -0
  85. data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/config/deploy.rb +53 -0
  86. data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/config/deploy/production.rb +6 -0
  87. data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/config/deploy/staging.rb +6 -0
  88. data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/config/environments/staging.rb +0 -0
  89. data/vendor/daemon-kit/daemon_generators/jabber/USAGE +5 -0
  90. data/vendor/daemon-kit/daemon_generators/jabber/jabber_generator.rb +65 -0
  91. data/vendor/daemon-kit/daemon_generators/jabber/templates/config/initializers/jabber.rb +7 -0
  92. data/vendor/daemon-kit/daemon_generators/jabber/templates/config/jabber.yml +26 -0
  93. data/vendor/daemon-kit/daemon_generators/jabber/templates/libexec/daemon.rb +27 -0
  94. data/vendor/daemon-kit/daemon_generators/nanite_agent/USAGE +5 -0
  95. data/vendor/daemon-kit/daemon_generators/nanite_agent/nanite_agent_generator.rb +68 -0
  96. data/vendor/daemon-kit/daemon_generators/nanite_agent/templates/config/initializers/nanite_agent.rb +6 -0
  97. data/vendor/daemon-kit/daemon_generators/nanite_agent/templates/config/nanite.yml +35 -0
  98. data/vendor/daemon-kit/daemon_generators/nanite_agent/templates/lib/actors/sample.rb +11 -0
  99. data/vendor/daemon-kit/daemon_generators/nanite_agent/templates/libexec/daemon.rb +31 -0
  100. data/vendor/daemon-kit/daemon_generators/rspec/USAGE +5 -0
  101. data/vendor/daemon-kit/daemon_generators/rspec/rspec_generator.rb +55 -0
  102. data/vendor/daemon-kit/daemon_generators/rspec/templates/spec.rb +11 -0
  103. data/vendor/daemon-kit/daemon_generators/rspec/templates/spec/spec.opts +1 -0
  104. data/vendor/daemon-kit/daemon_generators/rspec/templates/spec/spec_helper.rb +21 -0
  105. data/vendor/daemon-kit/daemon_generators/rspec/templates/tasks/rspec.rake +21 -0
  106. data/vendor/daemon-kit/daemon_generators/ruote/USAGE +5 -0
  107. data/vendor/daemon-kit/daemon_generators/ruote/ruote_generator.rb +67 -0
  108. data/vendor/daemon-kit/daemon_generators/ruote/templates/config/amqp.yml +30 -0
  109. data/vendor/daemon-kit/daemon_generators/ruote/templates/config/initializers/ruote.rb +13 -0
  110. data/vendor/daemon-kit/daemon_generators/ruote/templates/config/ruote.yml +23 -0
  111. data/vendor/daemon-kit/daemon_generators/ruote/templates/lib/daemon.rb +4 -0
  112. data/vendor/daemon-kit/daemon_generators/ruote/templates/lib/sample.rb +26 -0
  113. data/vendor/daemon-kit/daemon_generators/ruote/templates/libexec/daemon.rb +33 -0
  114. data/vendor/daemon-kit/lib/daemon_kit.rb +54 -0
  115. data/vendor/daemon-kit/lib/daemon_kit/abstract_logger.rb +235 -0
  116. data/vendor/daemon-kit/lib/daemon_kit/amqp.rb +38 -0
  117. data/vendor/daemon-kit/lib/daemon_kit/application.rb +187 -0
  118. data/vendor/daemon-kit/lib/daemon_kit/arguments.rb +165 -0
  119. data/vendor/daemon-kit/lib/daemon_kit/commands/console.rb +38 -0
  120. data/vendor/daemon-kit/lib/daemon_kit/config.rb +108 -0
  121. data/vendor/daemon-kit/lib/daemon_kit/console_daemon.rb +2 -0
  122. data/vendor/daemon-kit/lib/daemon_kit/core_ext.rb +1 -0
  123. data/vendor/daemon-kit/lib/daemon_kit/core_ext/configurable.rb +96 -0
  124. data/vendor/daemon-kit/lib/daemon_kit/core_ext/string.rb +22 -0
  125. data/vendor/daemon-kit/lib/daemon_kit/cron.rb +48 -0
  126. data/vendor/daemon-kit/lib/daemon_kit/cucumber/world.rb +38 -0
  127. data/vendor/daemon-kit/lib/daemon_kit/deployment/capistrano.rb +482 -0
  128. data/vendor/daemon-kit/lib/daemon_kit/em.rb +43 -0
  129. data/vendor/daemon-kit/lib/daemon_kit/error_handlers/base.rb +32 -0
  130. data/vendor/daemon-kit/lib/daemon_kit/error_handlers/hoptoad.rb +61 -0
  131. data/vendor/daemon-kit/lib/daemon_kit/error_handlers/mail.rb +85 -0
  132. data/vendor/daemon-kit/lib/daemon_kit/exceptions.rb +8 -0
  133. data/vendor/daemon-kit/lib/daemon_kit/initializer.rb +438 -0
  134. data/vendor/daemon-kit/lib/daemon_kit/jabber.rb +170 -0
  135. data/vendor/daemon-kit/lib/daemon_kit/nanite.rb +7 -0
  136. data/vendor/daemon-kit/lib/daemon_kit/nanite/agent.rb +56 -0
  137. data/vendor/daemon-kit/lib/daemon_kit/pid_file.rb +61 -0
  138. data/vendor/daemon-kit/lib/daemon_kit/ruote_participants.rb +119 -0
  139. data/vendor/daemon-kit/lib/daemon_kit/ruote_pseudo_participant.rb +68 -0
  140. data/vendor/daemon-kit/lib/daemon_kit/ruote_workitem.rb +169 -0
  141. data/vendor/daemon-kit/lib/daemon_kit/safety.rb +85 -0
  142. data/vendor/daemon-kit/lib/daemon_kit/tasks.rb +2 -0
  143. data/vendor/daemon-kit/lib/daemon_kit/tasks/environment.rake +10 -0
  144. data/vendor/daemon-kit/lib/daemon_kit/tasks/framework.rake +120 -0
  145. data/vendor/daemon-kit/lib/daemon_kit/tasks/god.rake +62 -0
  146. data/vendor/daemon-kit/lib/daemon_kit/tasks/log.rake +8 -0
  147. data/vendor/daemon-kit/lib/daemon_kit/tasks/monit.rake +29 -0
  148. data/vendor/daemon-kit/script/console +10 -0
  149. data/vendor/daemon-kit/script/destroy +14 -0
  150. data/vendor/daemon-kit/script/generate +14 -0
  151. data/vendor/daemon-kit/script/txt2html +71 -0
  152. data/vendor/daemon-kit/spec/abstract_logger_spec.rb +126 -0
  153. data/vendor/daemon-kit/spec/argument_spec.rb +70 -0
  154. data/vendor/daemon-kit/spec/config_spec.rb +79 -0
  155. data/vendor/daemon-kit/spec/configurable_spec.rb +56 -0
  156. data/vendor/daemon-kit/spec/daemon_kit_spec.rb +7 -0
  157. data/vendor/daemon-kit/spec/error_handlers_spec.rb +23 -0
  158. data/vendor/daemon-kit/spec/fixtures/env.yml +15 -0
  159. data/vendor/daemon-kit/spec/fixtures/noenv.yml +4 -0
  160. data/vendor/daemon-kit/spec/initializer_spec.rb +26 -0
  161. data/vendor/daemon-kit/spec/spec.opts +1 -0
  162. data/vendor/daemon-kit/spec/spec_helper.rb +27 -0
  163. data/vendor/daemon-kit/tasks/rspec.rake +21 -0
  164. data/vendor/daemon-kit/templates/god/god.erb +69 -0
  165. data/vendor/daemon-kit/templates/monit/monit.erb +14 -0
  166. data/vendor/daemon-kit/test/test_amqp_generator.rb +48 -0
  167. data/vendor/daemon-kit/test/test_cron_generator.rb +45 -0
  168. data/vendor/daemon-kit/test/test_daemon-kit_generator.rb +84 -0
  169. data/vendor/daemon-kit/test/test_daemon_kit_config.rb +28 -0
  170. data/vendor/daemon-kit/test/test_deploy_capistrano_generator.rb +48 -0
  171. data/vendor/daemon-kit/test/test_generator_helper.rb +29 -0
  172. data/vendor/daemon-kit/test/test_helper.rb +7 -0
  173. data/vendor/daemon-kit/test/test_jabber_generator.rb +49 -0
  174. data/vendor/daemon-kit/test/test_nanite_agent_generator.rb +49 -0
  175. data/vendor/daemon-kit/test/test_ruote_generator.rb +45 -0
  176. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail.rb +5 -0
  177. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/address.rb +426 -0
  178. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/attachments.rb +46 -0
  179. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/base64.rb +46 -0
  180. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/compat.rb +41 -0
  181. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/config.rb +67 -0
  182. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/core_extensions.rb +63 -0
  183. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/encode.rb +581 -0
  184. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/header.rb +960 -0
  185. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/index.rb +9 -0
  186. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/interface.rb +1130 -0
  187. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/loader.rb +3 -0
  188. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/mail.rb +578 -0
  189. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/mailbox.rb +495 -0
  190. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/main.rb +6 -0
  191. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/mbox.rb +3 -0
  192. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/net.rb +248 -0
  193. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/obsolete.rb +132 -0
  194. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/parser.rb +1476 -0
  195. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/port.rb +379 -0
  196. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/quoting.rb +118 -0
  197. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/require_arch.rb +58 -0
  198. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/scanner.rb +49 -0
  199. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/scanner_r.rb +261 -0
  200. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/stringio.rb +280 -0
  201. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/utils.rb +337 -0
  202. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/version.rb +39 -0
  203. data/vendor/daemon-kit/vendor/tmail.rb +13 -0
  204. metadata +281 -0
@@ -0,0 +1,169 @@
1
+ module DaemonKit
2
+
3
+ # Dual purpose class that is a) responsible for parsing incoming workitems and
4
+ # delegating to the correct RuotePseudoParticipant, and b) wrapping the
5
+ # workitem hash into something a bit more digestable.
6
+ class RuoteWorkitem
7
+
8
+ class << self
9
+
10
+ # = Process incoming commands via an AMQP queue
11
+ #
12
+ # Expects a JSON workitem from ruote that has these fields set in
13
+ # attributes key:
14
+ #
15
+ # {
16
+ # 'reply_queue' => 'queue to send replies to',
17
+ # 'params' => {
18
+ # 'command' => '/actor/method'
19
+ # }
20
+ # }
21
+ #
22
+ # == Notes on the command key:
23
+ #
24
+ # It looks like a resource, and will be treated as such. Is should
25
+ # be in the format of +/class/method+, and it will be passed the
26
+ # complete workitem as a hash.
27
+ #
28
+ # == Notes on replies
29
+ #
30
+ # Replies are sent back to the queue specified in the +reply_queue+ key.
31
+ #
32
+ # == Notes on errors
33
+ #
34
+ # Where daemon-kit detects errors in attempting to parse and delegate the
35
+ # workitems, it will reply to the engine and set the following field with
36
+ # the error information:
37
+ #
38
+ # daemon_kit.error
39
+ def process( transport, workitem )
40
+ # keep it singleton
41
+ @instance ||= new
42
+
43
+ work = parse( workitem )
44
+
45
+ DaemonKit.logger.warn "Processing workitem that has timed out!" if work.timed_out?
46
+
47
+ target, method = parse_command( work )
48
+
49
+ if target.nil? || method.nil?
50
+ msg = "Missing target/method in command parameter, or command parameter missing"
51
+ DaemonKit.logger.error( msg )
52
+ work["daemon_kit"] = { "error" => msg }
53
+
54
+ elsif target.public_methods.include?( method )
55
+ target.perform( method, work )
56
+
57
+ else
58
+ msg = "Workitem cannot be processes: #{method} not exposed by #{target.inspect}"
59
+ DaemonKit.logger.error( msg )
60
+ work["daemon_kit"] = { "error" => msg }
61
+ end
62
+
63
+ reply_to_engine( transport, work )
64
+ end
65
+
66
+ # Extract the class and method name from the workitem, then pick the matching
67
+ # class from the registered list of participants
68
+ def parse_command( work )
69
+ return nil if work['params']['command'].nil?
70
+
71
+ _, klass, method = work['params']['command'].split('/')
72
+
73
+ instance = RuoteParticipants.instance.participants[ klass ]
74
+
75
+ if instance.nil?
76
+ msg = "No instance registered for #{klass}"
77
+ DaemonKit.logger.error( msg )
78
+ raise DaemonKit::MissingParticipant, msg
79
+ end
80
+
81
+ return instance, method
82
+ end
83
+
84
+ def reply_to_engine( transport, response )
85
+ send( "reply_via_#{transport}", response )
86
+ end
87
+
88
+ def reply_via_amqp( response )
89
+ DaemonKit.logger.debug("Replying to engine via AMQP with #{response.inspect}")
90
+
91
+ ::MQ.queue( response['reply_queue'] ).publish( response.to_json )
92
+
93
+ response
94
+ end
95
+
96
+ def parse( workitem )
97
+ new( JSON.parse( workitem ) )
98
+ end
99
+ end
100
+
101
+ def initialize( workitem = {} )
102
+ @workitem = workitem
103
+ end
104
+
105
+ def fei
106
+ @workitem['flow_expression_id']
107
+ end
108
+
109
+ def short_fei
110
+ @short_fei ||=
111
+ '(' + [
112
+ 'fei', self.fei['owfe_version'], self.fei['engine_id'],
113
+ self.fei['workflow_definition_url'], self.fei['workflow_definition_name'],
114
+ self.fei['workflow_definition_revision'], self.fei['workflow_instance_id'],
115
+ self.fei['expression_name'], self.fei['expression_id']
116
+ ].join(' ') + ')'
117
+ end
118
+
119
+ def dispatch_time
120
+ @dispath_time ||= Time.parse( @workitem['dispatch_time'] )
121
+ end
122
+
123
+ def last_modified
124
+ @last_modified ||= Time.parse( @workitem['last_modified'] )
125
+ end
126
+
127
+ def participant_name
128
+ @workitem['participant_name']
129
+ end
130
+
131
+ def attributes
132
+ @workitem['attributes']
133
+ end
134
+
135
+ def []( key )
136
+ self.attributes[ key ]
137
+ end
138
+
139
+ def []=( key, value )
140
+ self.attributes[ key ] = value
141
+ end
142
+
143
+ def to_json
144
+ @workitem.to_json
145
+ end
146
+
147
+ # Look at the workitem payload and attempt to determine if this workitem
148
+ # has timed out or not. This method will only ever work if you used the
149
+ # +:timeout: parameter was set for the expression.
150
+ def timed_out?
151
+ key = fei['workflow_instance_id'] + '__' + fei['expression_id']
152
+
153
+ if self.attributes["__timeouts__"] && timeout = self.attributes["__timeouts__"][ key ]
154
+ return Time.at( timeout.last ) < Time.now
155
+ end
156
+
157
+ return false
158
+ end
159
+
160
+ def method_missing( method_name, *args )
161
+ if self.attributes.keys.include?( method_name.to_s )
162
+ return self.attributes[ method_name.to_s ]
163
+ end
164
+
165
+ super
166
+ end
167
+
168
+ end
169
+ end
@@ -0,0 +1,85 @@
1
+ module DaemonKit
2
+ # Provides a wrapper for running code inside a 'safety net' Any
3
+ # exceptions raised inside a safety net is handled and reported via
4
+ # loggers, email or Hoptoad.
5
+ #
6
+ # The safety net can be configured via DaemonKit.config.safety,
7
+ # which holds the only instance of the safety net.
8
+ class Safety
9
+
10
+ # Who get's notified.
11
+ @handler = nil
12
+ attr_accessor :handler
13
+
14
+ # Registered error handlers
15
+ @error_handlers = {}
16
+ attr_reader :error_handlers
17
+
18
+ class << self
19
+
20
+ # Singleton
21
+ @instance = nil
22
+
23
+ def instance
24
+ @instance ||= new
25
+ end
26
+ private :new
27
+
28
+ # Run the provided block inside a safety net.
29
+ def run(&block)
30
+ self.instance.run(&block)
31
+ end
32
+
33
+ def register_error_handler( klass )
34
+ name = klass.to_s.split('::').last.downcase
35
+
36
+ DaemonKit.logger.debug( "Registering error handler '#{name}' (#{klass})" ) if DaemonKit.logger
37
+
38
+ instance.instance_eval( <<-EOF, __FILE__, __LINE__ )
39
+ def #{name}
40
+ @#{name} ||= #{klass}.instance
41
+ end
42
+ EOF
43
+ end
44
+ end
45
+
46
+ # Run the provided block inside a safety net.
47
+ def run(&block)
48
+ begin
49
+ block.call
50
+ rescue => e
51
+ # Log
52
+ DaemonKit.logger.fatal "Safety net caught exception: #{e.message}"
53
+ DaemonKit.logger.fatal "Backtrace: #{e.backtrace.join("\n ")}"
54
+
55
+ get_handler.handle_exception( e ) if get_handler
56
+ end
57
+ end
58
+
59
+ def get_handler
60
+ if @handler && self.respond_to?( @handler )
61
+ h = send( @handler )
62
+ return h if h.class.ancestors.include?( DaemonKit::ErrorHandlers::Base )
63
+ end
64
+
65
+ return nil
66
+ end
67
+ end
68
+ end
69
+
70
+ class Object
71
+ class << self
72
+ def safely(&block)
73
+ DaemonKit::Safety.run(&block)
74
+ end
75
+ end
76
+
77
+ def safely(&block)
78
+ DaemonKit::Safety.run(&block)
79
+ end
80
+ end
81
+
82
+ # Load our error handlers
83
+ require 'daemon_kit/error_handlers/base'
84
+ require 'daemon_kit/error_handlers/mail'
85
+ require 'daemon_kit/error_handlers/hoptoad'
@@ -0,0 +1,2 @@
1
+ # Load all the take tasks in the gem
2
+ Dir[File.join(File.dirname(__FILE__), '**/*.rake')].each { |rake| load rake }
@@ -0,0 +1,10 @@
1
+ task :environment do
2
+ $daemon_kit_rake_task = true
3
+
4
+ require 'config/environment'
5
+ end
6
+
7
+ task "Execute system commands in other tasks with sudo"
8
+ task :sudo do
9
+ $RAKE_USE_SUDO = true
10
+ end
@@ -0,0 +1,120 @@
1
+ namespace :daemon_kit do
2
+ namespace :freeze do
3
+ desc "Lock this application to the current gem (by unpacking it into vendor/daemon_kit)"
4
+ task :gems do
5
+ deps = %w()
6
+ require 'rubygems'
7
+ require 'rubygems/gem_runner'
8
+
9
+ kit = (version = ENV['VERSION']) ?
10
+ Gem.cache.find_name('daemon-kit', "= #{version}").first :
11
+ Gem.cache.find_name('daemon-kit').sort_by { |g| g.version }.last
12
+
13
+ version ||= kit.version
14
+
15
+ unless kit
16
+ puts "No daemon_kit gem #{version} is installed. Do 'gem list daemon_kit' to see what you have available."
17
+ exit
18
+ end
19
+
20
+ puts "Freezing the gem for DaemonKit #{kit.version}"
21
+ rm_rf "vendor/daemon_kit"
22
+ mkdir_p "vendor/daemon_kit"
23
+
24
+ begin
25
+ chdir("vendor/daemon_kit") do
26
+ kit.dependencies.select { |g| deps.include? g.name }.each do |g|
27
+ Gem::GemRunner.new.run(["unpack", g.name, "--version", g.version_requirements.to_s])
28
+ mv(Dir.glob("#{g.name}*").first, g.name)
29
+ end
30
+
31
+ Gem::GemRunner.new.run(["unpack", "daemon-kit", "--version", "=#{version}"])
32
+ FileUtils.mv(Dir.glob("daemon-kit*").first, "daemon-kit")
33
+ end
34
+ rescue Exception
35
+ rm_rf "vendor/daemon_kit"
36
+ raise
37
+ end
38
+ end
39
+
40
+ desc 'Lock to latest edge daemon_kit'
41
+ task :edge do
42
+ require 'open-uri'
43
+ #version = ENV["RELEASE"] || "edge"
44
+ commits = "http://github.com/api/v1/yaml/kennethkalmer/daemon-kit/commits/master"
45
+ url = "http://github.com/kennethkalmer/daemon-kit/zipball/master"
46
+
47
+ rm_rf "vendor/daemon_kit"
48
+ mkdir_p "vendor/daemon_kit"
49
+
50
+ chdir 'vendor/daemon_kit' do
51
+ latest_revision = YAML.load(open(commits))["commits"].first["id"]
52
+
53
+ puts "Downloading DaemonKit from #{url}"
54
+ File.open('daemon-kit.zip', 'wb') do |dst|
55
+ open url do |src|
56
+ while chunk = src.read(4096)
57
+ dst << chunk
58
+ end
59
+ end
60
+ end
61
+
62
+ puts 'Unpacking DaemonKit'
63
+ rm_rf 'daemon-kit'
64
+ `unzip daemon-kit.zip`
65
+ FileUtils.mv(Dir.glob("kennethkalmer-daemon-kit*").first, "daemon-kit")
66
+ %w(daemon-kit.zip).each do |goner|
67
+ rm_f goner
68
+ end
69
+
70
+ touch "REVISION_#{latest_revision}"
71
+ end
72
+ end
73
+
74
+ end
75
+
76
+ desc "Upgrade your local files for a daemon after upgrading daemon-kit"
77
+ task :upgrade => 'environment' do
78
+ # Run these
79
+ %w{ initializers rakefile scripts }.each do |t|
80
+ Rake::Task["daemon_kit:upgrade:#{t}"].invoke
81
+ end
82
+
83
+ puts
84
+ puts "#{DaemonKit.configuration.daemon_name} has been upgraded."
85
+ end
86
+
87
+ namespace :upgrade do
88
+ # Upgrade the initializers
89
+ task :initializers do
90
+ copy_framework_template( 'config', 'boot.rb' )
91
+
92
+ if File.directory?( File.join(DaemonKit.root, 'config', 'initializers') )
93
+ mv File.join(DaemonKit.root, 'config', 'initializers'), File.join(DAEMON_ROOT, 'config', 'pre-daemonize')
94
+ copy_framework_template( 'config', 'pre-daemonize', 'readme' )
95
+ end
96
+
97
+ unless File.directory?( File.join(DAEMON_ROOT, 'config', 'post-daemonize') )
98
+ mkdir_p File.join(DAEMON_ROOT, 'config', 'post-daemonize')
99
+ copy_framework_template( 'config', 'post-daemonize', 'readme' )
100
+ end
101
+ end
102
+
103
+ # Upgrade the Rakefile
104
+ task :rakefile do
105
+ copy_framework_template( 'Rakefile' )
106
+ end
107
+
108
+ # Upgrade the scripts
109
+ task :scripts do
110
+ %w{ console destroy generate }.each do |s|
111
+ copy_framework_template( "script", s )
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ def copy_framework_template( *args )
118
+ src_dir = File.join(DaemonKit.framework_root, 'app_generators', 'daemon_kit', 'templates')
119
+ cp File.join( src_dir, *args ), File.join( DaemonKit.root, *args )
120
+ end
@@ -0,0 +1,62 @@
1
+ require 'erb'
2
+
3
+ namespace :god do
4
+ desc "Generate a stub god config file template for the daemon"
5
+ task :template => 'environment' do
6
+ # Preserve local changes
7
+ if File.exists?( "#{DaemonKit.root}/config/god.erb" ) && ENV['FORCE'].nil?
8
+ puts "Template already exists, use FORCE=1 to overwrite."
9
+ exit 1
10
+ end
11
+
12
+ cp "#{DaemonKit.framework_root}/templates/god/god.erb", "#{DaemonKit.root}/config/god.erb"
13
+ end
14
+
15
+ desc "Parse the god config template into a god config file"
16
+ task :generate => 'environment' do
17
+
18
+ unless File.exists?( "#{DaemonKit.root}/config/god.erb" )
19
+ Rake::Task["god:template"].invoke
20
+ end
21
+
22
+ name = DaemonKit.configuration.daemon_name
23
+
24
+ File.open( "#{DaemonKit.root}/config/#{name}.god", "w+" ) do |f|
25
+ t = File.read( "#{DaemonKit.root}/config/god.erb" )
26
+ f.write( ERB.new( t ).result( binding ) )
27
+ end
28
+
29
+ puts "Monit config generated in config/#{name}.god"
30
+ end
31
+
32
+ desc "Load the god file into god"
33
+ task :load => 'environment' do
34
+ name = DaemonKit.configuration.daemon_name
35
+
36
+ sh "#{$RAKE_USE_SUDO ? 'sudo' : ''} god load #{DaemonKit.root}/config/#{name}.god"
37
+ end
38
+
39
+ desc "Refresh the god config file in the running god"
40
+ task :refresh => 'environment' do
41
+ name = DaemonKit.configuration.daemon_name
42
+
43
+ sh "#{$RAKE_USE_SUDO ? 'sudo' : ''} god unmonitor #{name}"
44
+ sh "#{$RAKE_USE_SUDO ? 'sudo' : ''} god remove #{name}"
45
+ sh "#{$RAKE_USE_SUDO ? 'sudo' : ''} god load #{DaemonKit.root}/config/#{name}.god"
46
+ sh "#{$RAKE_USE_SUDO ? 'sudo' : ''} god monitor #{name}"
47
+ end
48
+
49
+ desc "Start god monitoring of the config file"
50
+ task :monitor => 'environment' do
51
+ name = DaemonKit.configuration.daemon_name
52
+
53
+ sh "#{$RAKE_USE_SUDO ? 'sudo' : ''} god monitor #{name}"
54
+ end
55
+
56
+ desc "Stop god monitoring of the config file"
57
+ task :unmonitor => 'environment' do
58
+ name = DaemonKit.configuration.daemon_name
59
+
60
+ sh "#{$RAKE_USE_SUDO ? 'sudo' : ''} god unmonitor #{name}"
61
+ end
62
+ end