apphunkd 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +22 -0
- data/LICENSE +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +78 -0
- data/TODO +1 -0
- data/VERSION +1 -0
- data/apphunkd.gemspec +258 -0
- data/bin/apphunkd +4 -0
- data/config/arguments.rb +12 -0
- data/config/boot.rb +68 -0
- data/config/environment.rb +23 -0
- data/config/environments/development.rb +2 -0
- data/config/environments/production.rb +2 -0
- data/config/environments/test.rb +2 -0
- data/config/post-daemonize/readme +5 -0
- data/config/pre-daemonize/readme +12 -0
- data/config/pre-daemonize/requires.rb +2 -0
- data/lib/apphunkd.rb +25 -0
- data/lib/apphunkd/api.rb +5 -0
- data/lib/apphunkd/api/service.rb +27 -0
- data/lib/apphunkd/queue.rb +96 -0
- data/lib/apphunkd/remote.rb +29 -0
- data/lib/apphunkd/remote/result.rb +15 -0
- data/libexec/apphunkd-daemon.rb +13 -0
- data/script/console +3 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/lib/apphunkd/api/service_spec.rb +47 -0
- data/spec/lib/apphunkd/queue_spec.rb +129 -0
- data/spec/lib/apphunkd/remote_spec.rb +61 -0
- data/spec/lib/apphunkd_spec.rb +50 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +13 -0
- data/support/apphunkd.initd +47 -0
- data/support/apphunkd.monitrc +3 -0
- data/tasks/rspec.rake +21 -0
- data/vendor/daemon-kit/Configuration.txt +102 -0
- data/vendor/daemon-kit/Deployment.txt +113 -0
- data/vendor/daemon-kit/History.txt +97 -0
- data/vendor/daemon-kit/Logging.txt +92 -0
- data/vendor/daemon-kit/Manifest.txt +166 -0
- data/vendor/daemon-kit/PostInstall.txt +6 -0
- data/vendor/daemon-kit/README.rdoc +130 -0
- data/vendor/daemon-kit/Rakefile +37 -0
- data/vendor/daemon-kit/RuoteParticipants.txt +113 -0
- data/vendor/daemon-kit/TODO.txt +37 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/USAGE +7 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/daemon_kit_generator.rb +161 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/templates/README +48 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/templates/Rakefile +6 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/templates/bin/daemon.erb +7 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/arguments.rb +12 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/boot.rb +68 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/environment.rb +23 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/environments/development.rb +2 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/environments/production.rb +2 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/environments/test.rb +2 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/post-daemonize/readme +5 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/pre-daemonize/readme +12 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/templates/lib/daemon.rb +2 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/templates/libexec/daemon.erb +18 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/templates/script/console +3 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/templates/script/destroy +14 -0
- data/vendor/daemon-kit/app_generators/daemon_kit/templates/script/generate +14 -0
- data/vendor/daemon-kit/bin/daemon_kit +18 -0
- data/vendor/daemon-kit/daemon_generators/amqp/USAGE +5 -0
- data/vendor/daemon-kit/daemon_generators/amqp/amqp_generator.rb +65 -0
- data/vendor/daemon-kit/daemon_generators/amqp/templates/config/amqp.yml +28 -0
- data/vendor/daemon-kit/daemon_generators/amqp/templates/config/initializers/amqp.rb +7 -0
- data/vendor/daemon-kit/daemon_generators/amqp/templates/libexec/daemon.rb +37 -0
- data/vendor/daemon-kit/daemon_generators/cron/USAGE +5 -0
- data/vendor/daemon-kit/daemon_generators/cron/cron_generator.rb +64 -0
- data/vendor/daemon-kit/daemon_generators/cron/templates/config/initializers/cron.rb +11 -0
- data/vendor/daemon-kit/daemon_generators/cron/templates/libexec/daemon.rb +43 -0
- data/vendor/daemon-kit/daemon_generators/cucumber/USAGE +11 -0
- data/vendor/daemon-kit/daemon_generators/cucumber/cucumber_generator.rb +38 -0
- data/vendor/daemon-kit/daemon_generators/cucumber/templates/cucumber +8 -0
- data/vendor/daemon-kit/daemon_generators/cucumber/templates/cucumber.rake +13 -0
- data/vendor/daemon-kit/daemon_generators/cucumber/templates/cucumber_environment.rb +2 -0
- data/vendor/daemon-kit/daemon_generators/cucumber/templates/env.rb +7 -0
- data/vendor/daemon-kit/daemon_generators/deploy_capistrano/deploy_capistrano_generator.rb +35 -0
- data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/Capfile +10 -0
- data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/USAGE +10 -0
- data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/config/deploy.rb +53 -0
- data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/config/deploy/production.rb +6 -0
- data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/config/deploy/staging.rb +6 -0
- data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/config/environments/staging.rb +0 -0
- data/vendor/daemon-kit/daemon_generators/jabber/USAGE +5 -0
- data/vendor/daemon-kit/daemon_generators/jabber/jabber_generator.rb +65 -0
- data/vendor/daemon-kit/daemon_generators/jabber/templates/config/initializers/jabber.rb +7 -0
- data/vendor/daemon-kit/daemon_generators/jabber/templates/config/jabber.yml +26 -0
- data/vendor/daemon-kit/daemon_generators/jabber/templates/libexec/daemon.rb +27 -0
- data/vendor/daemon-kit/daemon_generators/nanite_agent/USAGE +5 -0
- data/vendor/daemon-kit/daemon_generators/nanite_agent/nanite_agent_generator.rb +68 -0
- data/vendor/daemon-kit/daemon_generators/nanite_agent/templates/config/initializers/nanite_agent.rb +6 -0
- data/vendor/daemon-kit/daemon_generators/nanite_agent/templates/config/nanite.yml +35 -0
- data/vendor/daemon-kit/daemon_generators/nanite_agent/templates/lib/actors/sample.rb +11 -0
- data/vendor/daemon-kit/daemon_generators/nanite_agent/templates/libexec/daemon.rb +31 -0
- data/vendor/daemon-kit/daemon_generators/rspec/USAGE +5 -0
- data/vendor/daemon-kit/daemon_generators/rspec/rspec_generator.rb +55 -0
- data/vendor/daemon-kit/daemon_generators/rspec/templates/spec.rb +11 -0
- data/vendor/daemon-kit/daemon_generators/rspec/templates/spec/spec.opts +1 -0
- data/vendor/daemon-kit/daemon_generators/rspec/templates/spec/spec_helper.rb +21 -0
- data/vendor/daemon-kit/daemon_generators/rspec/templates/tasks/rspec.rake +21 -0
- data/vendor/daemon-kit/daemon_generators/ruote/USAGE +5 -0
- data/vendor/daemon-kit/daemon_generators/ruote/ruote_generator.rb +67 -0
- data/vendor/daemon-kit/daemon_generators/ruote/templates/config/amqp.yml +30 -0
- data/vendor/daemon-kit/daemon_generators/ruote/templates/config/initializers/ruote.rb +13 -0
- data/vendor/daemon-kit/daemon_generators/ruote/templates/config/ruote.yml +23 -0
- data/vendor/daemon-kit/daemon_generators/ruote/templates/lib/daemon.rb +4 -0
- data/vendor/daemon-kit/daemon_generators/ruote/templates/lib/sample.rb +26 -0
- data/vendor/daemon-kit/daemon_generators/ruote/templates/libexec/daemon.rb +33 -0
- data/vendor/daemon-kit/lib/daemon_kit.rb +54 -0
- data/vendor/daemon-kit/lib/daemon_kit/abstract_logger.rb +235 -0
- data/vendor/daemon-kit/lib/daemon_kit/amqp.rb +38 -0
- data/vendor/daemon-kit/lib/daemon_kit/application.rb +187 -0
- data/vendor/daemon-kit/lib/daemon_kit/arguments.rb +165 -0
- data/vendor/daemon-kit/lib/daemon_kit/commands/console.rb +38 -0
- data/vendor/daemon-kit/lib/daemon_kit/config.rb +108 -0
- data/vendor/daemon-kit/lib/daemon_kit/console_daemon.rb +2 -0
- data/vendor/daemon-kit/lib/daemon_kit/core_ext.rb +1 -0
- data/vendor/daemon-kit/lib/daemon_kit/core_ext/configurable.rb +96 -0
- data/vendor/daemon-kit/lib/daemon_kit/core_ext/string.rb +22 -0
- data/vendor/daemon-kit/lib/daemon_kit/cron.rb +48 -0
- data/vendor/daemon-kit/lib/daemon_kit/cucumber/world.rb +38 -0
- data/vendor/daemon-kit/lib/daemon_kit/deployment/capistrano.rb +482 -0
- data/vendor/daemon-kit/lib/daemon_kit/em.rb +43 -0
- data/vendor/daemon-kit/lib/daemon_kit/error_handlers/base.rb +32 -0
- data/vendor/daemon-kit/lib/daemon_kit/error_handlers/hoptoad.rb +61 -0
- data/vendor/daemon-kit/lib/daemon_kit/error_handlers/mail.rb +85 -0
- data/vendor/daemon-kit/lib/daemon_kit/exceptions.rb +8 -0
- data/vendor/daemon-kit/lib/daemon_kit/initializer.rb +438 -0
- data/vendor/daemon-kit/lib/daemon_kit/jabber.rb +170 -0
- data/vendor/daemon-kit/lib/daemon_kit/nanite.rb +7 -0
- data/vendor/daemon-kit/lib/daemon_kit/nanite/agent.rb +56 -0
- data/vendor/daemon-kit/lib/daemon_kit/pid_file.rb +61 -0
- data/vendor/daemon-kit/lib/daemon_kit/ruote_participants.rb +119 -0
- data/vendor/daemon-kit/lib/daemon_kit/ruote_pseudo_participant.rb +68 -0
- data/vendor/daemon-kit/lib/daemon_kit/ruote_workitem.rb +169 -0
- data/vendor/daemon-kit/lib/daemon_kit/safety.rb +85 -0
- data/vendor/daemon-kit/lib/daemon_kit/tasks.rb +2 -0
- data/vendor/daemon-kit/lib/daemon_kit/tasks/environment.rake +10 -0
- data/vendor/daemon-kit/lib/daemon_kit/tasks/framework.rake +120 -0
- data/vendor/daemon-kit/lib/daemon_kit/tasks/god.rake +62 -0
- data/vendor/daemon-kit/lib/daemon_kit/tasks/log.rake +8 -0
- data/vendor/daemon-kit/lib/daemon_kit/tasks/monit.rake +29 -0
- data/vendor/daemon-kit/script/console +10 -0
- data/vendor/daemon-kit/script/destroy +14 -0
- data/vendor/daemon-kit/script/generate +14 -0
- data/vendor/daemon-kit/script/txt2html +71 -0
- data/vendor/daemon-kit/spec/abstract_logger_spec.rb +126 -0
- data/vendor/daemon-kit/spec/argument_spec.rb +70 -0
- data/vendor/daemon-kit/spec/config_spec.rb +79 -0
- data/vendor/daemon-kit/spec/configurable_spec.rb +56 -0
- data/vendor/daemon-kit/spec/daemon_kit_spec.rb +7 -0
- data/vendor/daemon-kit/spec/error_handlers_spec.rb +23 -0
- data/vendor/daemon-kit/spec/fixtures/env.yml +15 -0
- data/vendor/daemon-kit/spec/fixtures/noenv.yml +4 -0
- data/vendor/daemon-kit/spec/initializer_spec.rb +26 -0
- data/vendor/daemon-kit/spec/spec.opts +1 -0
- data/vendor/daemon-kit/spec/spec_helper.rb +27 -0
- data/vendor/daemon-kit/tasks/rspec.rake +21 -0
- data/vendor/daemon-kit/templates/god/god.erb +69 -0
- data/vendor/daemon-kit/templates/monit/monit.erb +14 -0
- data/vendor/daemon-kit/test/test_amqp_generator.rb +48 -0
- data/vendor/daemon-kit/test/test_cron_generator.rb +45 -0
- data/vendor/daemon-kit/test/test_daemon-kit_generator.rb +84 -0
- data/vendor/daemon-kit/test/test_daemon_kit_config.rb +28 -0
- data/vendor/daemon-kit/test/test_deploy_capistrano_generator.rb +48 -0
- data/vendor/daemon-kit/test/test_generator_helper.rb +29 -0
- data/vendor/daemon-kit/test/test_helper.rb +7 -0
- data/vendor/daemon-kit/test/test_jabber_generator.rb +49 -0
- data/vendor/daemon-kit/test/test_nanite_agent_generator.rb +49 -0
- data/vendor/daemon-kit/test/test_ruote_generator.rb +45 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail.rb +5 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/address.rb +426 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/attachments.rb +46 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/base64.rb +46 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/compat.rb +41 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/config.rb +67 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/core_extensions.rb +63 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/encode.rb +581 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/header.rb +960 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/index.rb +9 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/interface.rb +1130 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/loader.rb +3 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/mail.rb +578 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/mailbox.rb +495 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/main.rb +6 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/mbox.rb +3 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/net.rb +248 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/obsolete.rb +132 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/parser.rb +1476 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/port.rb +379 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/quoting.rb +118 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/require_arch.rb +58 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/scanner.rb +49 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/scanner_r.rb +261 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/stringio.rb +280 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/utils.rb +337 -0
- data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/version.rb +39 -0
- data/vendor/daemon-kit/vendor/tmail.rb +13 -0
- 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,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
|