djinn 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +54 -28
- data/VERSION +1 -1
- data/djinn.gemspec +2 -2
- data/lib/djinn/base/dsl.rb +12 -17
- data/lib/djinn/rails/handlers.rb +30 -32
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -65,6 +65,10 @@ called _file_djinn.rb_
|
|
65
65
|
end
|
66
66
|
end
|
67
67
|
end
|
68
|
+
|
69
|
+
stop do
|
70
|
+
log "Djinn is stopping.."
|
71
|
+
end
|
68
72
|
|
69
73
|
exit do
|
70
74
|
if @file
|
@@ -165,53 +169,68 @@ There are a few options available to us if we want to pass configuration informa
|
|
165
169
|
to our Djinn.
|
166
170
|
|
167
171
|
First, we can do it using command line switches, which we can define as part of our
|
168
|
-
Djinn definition block.
|
172
|
+
Djinn definition block. Taken from the example above:
|
169
173
|
|
170
174
|
djinn do
|
171
175
|
|
172
176
|
configure do
|
173
|
-
|
174
|
-
|
175
|
-
|
177
|
+
add :output_file do
|
178
|
+
short_switch "-o"
|
179
|
+
long_switch "--output-file"
|
180
|
+
description "File to output stuff to"
|
181
|
+
required true
|
182
|
+
end
|
183
|
+
|
184
|
+
add_flag :create_file do
|
185
|
+
short_switch "-c"
|
186
|
+
long_switch "--create-file"
|
187
|
+
description "Create output file if it doesn't exist"
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
176
191
|
end
|
177
|
-
|
178
|
-
|
179
|
-
= OMG I LOST INTEREST IN WRITING THIS
|
180
192
|
|
181
|
-
|
182
|
-
|
193
|
+
This is pretty straight-forward. The nice thing about doing it like this is that
|
194
|
+
these options will show up in the usage banner if you pass the "--help" command line
|
195
|
+
argument, or screw something up.
|
196
|
+
|
197
|
+
By default the daemon will look for a config YAML file in same directory as you executed
|
198
|
+
it from, named the same as the Djinn class, so in this case *my_djinn.yml*. It will by
|
199
|
+
default create the pid and log files in the same way. You can change this by
|
200
|
+
putting it in the config file or supplying an options hash:
|
183
201
|
|
184
202
|
options = {
|
185
203
|
'pid_file_path' => 'path/to/pid/file',
|
186
204
|
'log_file_path' => 'path/to/log/file'
|
187
205
|
}
|
188
206
|
|
189
|
-
|
207
|
+
MyDjinn.djinnify(options)
|
190
208
|
|
191
|
-
|
209
|
+
This also illustrates that you can pass config to the *djinnify* method as well, so now
|
210
|
+
you have two additional ways to set configuration values.
|
192
211
|
|
193
|
-
The actions are executed in the context of the Djinn itself, so you can
|
212
|
+
The actions (start, run, etc) are executed in the context of the Djinn itself, so you can
|
194
213
|
get at the config without having to pass it around:
|
195
214
|
|
196
215
|
djinn do
|
197
|
-
|
216
|
+
start do
|
198
217
|
my_setting = config[:omghax]
|
199
218
|
end
|
200
219
|
end
|
201
220
|
|
202
221
|
...
|
203
222
|
|
204
|
-
djinn.
|
223
|
+
djinn.djinnify(:omghax => "Groovy, baby")
|
205
224
|
|
206
225
|
You can also give it a block to work with:
|
207
226
|
|
208
|
-
djinn.
|
227
|
+
djinn.djinnify do
|
209
228
|
puts "This will happen before calling the :start action"
|
210
229
|
end
|
211
230
|
|
212
231
|
If you need to man-handle the internals and stuff, it yields itself:
|
213
232
|
|
214
|
-
djinn.
|
233
|
+
djinn.djinnify do |djinn|
|
215
234
|
djinn.config[:omghax] = "Groovy, baby"
|
216
235
|
end
|
217
236
|
|
@@ -223,7 +242,7 @@ you check the code out of git, but here's the gist of it. Assumes a scenario
|
|
223
242
|
where you have a Book model that keeps a count of how many times a book has
|
224
243
|
been read..
|
225
244
|
|
226
|
-
Create a file in RAILS_ROOT/lib or somewhere
|
245
|
+
Create a file in RAILS_ROOT/lib or somewhere similarly convenient:
|
227
246
|
|
228
247
|
require 'djinn/rails'
|
229
248
|
require 'eventmachine'
|
@@ -236,7 +255,7 @@ Create a file in RAILS_ROOT/lib or somewhere similar in your load path:
|
|
236
255
|
|
237
256
|
djinn do
|
238
257
|
|
239
|
-
|
258
|
+
start do
|
240
259
|
EM.run do
|
241
260
|
log "Workers will run every #{BOOK_WORKER_INTERVAL} secs"
|
242
261
|
EM::PeriodicTimer.new(BOOK_WORKER_INTERVAL) do
|
@@ -247,7 +266,7 @@ Create a file in RAILS_ROOT/lib or somewhere similar in your load path:
|
|
247
266
|
end
|
248
267
|
end
|
249
268
|
|
250
|
-
|
269
|
+
exit do
|
251
270
|
EM.stop
|
252
271
|
end
|
253
272
|
|
@@ -255,20 +274,19 @@ Create a file in RAILS_ROOT/lib or somewhere similar in your load path:
|
|
255
274
|
|
256
275
|
end
|
257
276
|
|
258
|
-
Right, now you need to start it somehow. The easiest way is to create a file
|
259
|
-
in RAILS_ROOT/scripts and pop this in it:
|
277
|
+
Right, now you need to start it somehow. The easiest way is to create a file, lets
|
278
|
+
call it _book_djinn_, in RAILS_ROOT/scripts and pop this in it:
|
260
279
|
|
261
280
|
#!/usr/bin/env ruby
|
262
281
|
require 'rubygems'
|
263
282
|
require File.join(File.dirname(__FILE__), '../lib/book_djinn')
|
264
|
-
BookDjinn.
|
265
|
-
|
283
|
+
BookDjinn.djinnify_rails
|
284
|
+
|
266
285
|
Righto, now start it from RAILS_ROOT:
|
267
286
|
|
268
287
|
ruby script/book_djinn
|
269
288
|
|
270
|
-
|
271
|
-
uses the rails development environment by default. Try this:
|
289
|
+
This functions exactly like the non-Rails Djinn did. Try this:
|
272
290
|
|
273
291
|
ruby script/book_djinn --help
|
274
292
|
|
@@ -276,16 +294,24 @@ That should give you a better idea of what's going on, then try this:
|
|
276
294
|
|
277
295
|
ruby script/book_djinn start -e production
|
278
296
|
|
279
|
-
Yay, we have a daemon running in the background!
|
297
|
+
Yay, we have a daemon running in the background! As you can see above, the Rails
|
298
|
+
implementation automatically adds a switch for the environment for you - this will
|
299
|
+
default to _development_ if you don't supply a value for it.
|
280
300
|
|
281
|
-
|
301
|
+
To stop the Djinn:
|
302
|
+
|
303
|
+
ruby script/book_djinn --stop
|
282
304
|
|
283
305
|
That gives you more-or-less everything you need to build something basic
|
284
306
|
and monitor it with god or a similar process monitor.
|
285
307
|
|
308
|
+
Rails Djinns look for their configuration in a different location - the RAILS_ROOT/config
|
309
|
+
folder. Similarly they write their logs to your RAILS_ROOT/log folder, rather than
|
310
|
+
to the Djinn's home folder.
|
311
|
+
|
286
312
|
== TODO
|
287
313
|
|
288
|
-
Update this documentation.
|
314
|
+
Update this documentation. Make the code cooler.
|
289
315
|
|
290
316
|
== Copyright
|
291
317
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/djinn.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{djinn}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Craig Paterson"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-08-24}
|
13
13
|
s.description = %q{Helper for creating simple custom daemons}
|
14
14
|
s.email = %q{darksavant@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/djinn/base/dsl.rb
CHANGED
@@ -37,11 +37,11 @@ module Djinn
|
|
37
37
|
# Used internally to read configuration blocks
|
38
38
|
class ConfigHelper
|
39
39
|
|
40
|
-
attr_accessor :config
|
40
|
+
attr_accessor :config, :config_items
|
41
41
|
|
42
42
|
def initialize &block
|
43
43
|
@config_items = []
|
44
|
-
instance_eval(&block)
|
44
|
+
instance_eval(&block) if block_given?
|
45
45
|
end
|
46
46
|
|
47
47
|
# Add a posix-style switch to your Djinn
|
@@ -56,8 +56,8 @@ module Djinn
|
|
56
56
|
|
57
57
|
# Parses the configuration items created by executing the
|
58
58
|
# configuration block
|
59
|
-
def parse_config!(config)
|
60
|
-
|
59
|
+
def parse_config!(config, &block)
|
60
|
+
options = OptionParser.new do |opts|
|
61
61
|
opts.banner = "Usage: djinn_file [OPTIONS]"
|
62
62
|
opts.on("--no-daemon", "Don't run in the background") do
|
63
63
|
config[:__daemonize] = false
|
@@ -71,7 +71,8 @@ module Djinn
|
|
71
71
|
end
|
72
72
|
@config_items.each { |c| c.parse!(opts, config) }
|
73
73
|
end
|
74
|
-
|
74
|
+
yield options if block_given?
|
75
|
+
options.parse!
|
75
76
|
@config_items.each do |ci|
|
76
77
|
if ci.required?
|
77
78
|
puts "Missing argument: #{ci.key}\n\n#{opts}"
|
@@ -92,7 +93,7 @@ module Djinn
|
|
92
93
|
|
93
94
|
def initialize t, key, &block
|
94
95
|
@type, @key = t, key
|
95
|
-
instance_eval(&block)
|
96
|
+
instance_eval(&block) if block_given?
|
96
97
|
end
|
97
98
|
|
98
99
|
# Short configuration switch
|
@@ -143,7 +144,9 @@ module Djinn
|
|
143
144
|
|
144
145
|
def initialize &block
|
145
146
|
@actions = {}
|
146
|
-
instance_eval(&block)
|
147
|
+
instance_eval(&block) if block_given?
|
148
|
+
# create in case there was no configuration block
|
149
|
+
@config_helper = ConfigHelper.new unless defined?(@config_helper)
|
147
150
|
end
|
148
151
|
|
149
152
|
# Define configuration for a Djinn, adding ARGV switches that
|
@@ -151,15 +154,7 @@ module Djinn
|
|
151
154
|
def configure &block
|
152
155
|
@config_helper = ConfigHelper.new(&block)
|
153
156
|
end
|
154
|
-
|
155
|
-
# Define an action that will be performed by a Djinn
|
156
|
-
def on action, &block
|
157
|
-
acceptable_actions = %w(start stop exit)
|
158
|
-
raise DjinnActionError.new("\"#{action}\" is unrecognized, please use one of: #{acceptable_actions.join(', ')}") \
|
159
|
-
unless acceptable_actions.include?(action.to_s)
|
160
|
-
@actions[action] = block
|
161
|
-
end
|
162
|
-
|
157
|
+
|
163
158
|
# Runs when the Djinn starts
|
164
159
|
def start &block
|
165
160
|
@actions[:start] = block
|
@@ -175,7 +170,7 @@ module Djinn
|
|
175
170
|
@actions[:stop] = block
|
176
171
|
end
|
177
172
|
|
178
|
-
#
|
173
|
+
# Prepare the Djinn configuration based on informations passed in
|
179
174
|
# in the configuration block
|
180
175
|
def prepare(config)
|
181
176
|
@config_helper.parse_config!(config)
|
data/lib/djinn/rails/handlers.rb
CHANGED
@@ -2,19 +2,38 @@ module Djinn
|
|
2
2
|
module Rails
|
3
3
|
module Handlers
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
# Create an instance of your Djinn, interpret any command line switches
|
6
|
+
# defined in your configuration block, loads Rails environment and starts
|
7
|
+
# the Djinn in the background
|
8
|
+
def djinnify_rails config={}
|
9
|
+
djinn = new
|
10
|
+
|
11
|
+
# handle rails environment
|
12
|
+
env = Djinn::Base::Dsl::ConfigItem.new(:posix, :rails_env)
|
13
|
+
env.short_switch "-e"
|
14
|
+
env.long_switch "--environment"
|
15
|
+
env.description "Run in specific Rails environment"
|
16
|
+
@definition.config_helper.config_items << env
|
17
|
+
|
18
|
+
@definition.prepare(djinn.config.update(config))
|
19
|
+
|
20
|
+
unless djinn.config[:__stop]
|
21
|
+
if djinn.config[:__daemonize]
|
22
|
+
djinn.start do
|
23
|
+
ENV["RAILS_ENV"] = djinn.config[:rails_env] || "development"
|
24
|
+
load_rails
|
25
|
+
end
|
26
|
+
else
|
27
|
+
djinn.run do
|
28
|
+
ENV["RAILS_ENV"] = djinn.config[:rails_env] || "development"
|
29
|
+
load_rails
|
30
|
+
end
|
31
|
+
end
|
32
|
+
else
|
33
|
+
djinn.stop
|
11
34
|
end
|
12
35
|
end
|
13
|
-
|
14
|
-
def go args=[]
|
15
|
-
djinnify_rails(args)
|
16
|
-
end
|
17
|
-
|
36
|
+
|
18
37
|
private
|
19
38
|
|
20
39
|
def load_rails
|
@@ -22,27 +41,6 @@ module Djinn
|
|
22
41
|
require File.join(RAILS_ROOT, 'config', 'environment')
|
23
42
|
end
|
24
43
|
|
25
|
-
def parse_args args
|
26
|
-
action = "run" # this is the default
|
27
|
-
environment = ENV["RAILS_ENV"] || "development"
|
28
|
-
opts = OptionParser.new do |opts|
|
29
|
-
opts.banner = "Usage: djinn_name [options] {start|stop|restart|run}"
|
30
|
-
opts.on("-e", "--environment ENV", "Run in specific Rails environment") do |e|
|
31
|
-
environment = e
|
32
|
-
end
|
33
|
-
opts.on_tail("-h", "--help", "Show this message") do
|
34
|
-
puts opts
|
35
|
-
exit
|
36
|
-
end
|
37
|
-
action = opts.permute!(args)
|
38
|
-
action = action.first || "run"
|
39
|
-
(puts opts; exit(1)) unless %w(start stop restart run).include?(action)
|
40
|
-
end
|
41
|
-
opts.parse!(args)
|
42
|
-
ENV["RAILS_ENV"] = environment
|
43
|
-
action
|
44
|
-
end
|
45
|
-
|
46
44
|
end
|
47
45
|
end
|
48
46
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: djinn
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Craig Paterson
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-08-24 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|