djinn 0.1.6 → 0.1.7
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/VERSION +1 -1
- data/djinn.gemspec +2 -4
- data/lib/djinn/rails/handlers.rb +1 -0
- metadata +3 -5
- data/README.rdoc.old +0 -251
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.7
|
data/djinn.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
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.7"
|
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"]
|
@@ -14,14 +14,12 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.email = %q{darksavant@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
|
-
"README.rdoc"
|
18
|
-
"README.rdoc.old"
|
17
|
+
"README.rdoc"
|
19
18
|
]
|
20
19
|
s.files = [
|
21
20
|
".document",
|
22
21
|
"LICENSE",
|
23
22
|
"README.rdoc",
|
24
|
-
"README.rdoc.old",
|
25
23
|
"Rakefile",
|
26
24
|
"VERSION",
|
27
25
|
"djinn.gemspec",
|
data/lib/djinn/rails/handlers.rb
CHANGED
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: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 7
|
10
|
+
version: 0.1.7
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Craig Paterson
|
@@ -43,12 +43,10 @@ extensions: []
|
|
43
43
|
extra_rdoc_files:
|
44
44
|
- LICENSE
|
45
45
|
- README.rdoc
|
46
|
-
- README.rdoc.old
|
47
46
|
files:
|
48
47
|
- .document
|
49
48
|
- LICENSE
|
50
49
|
- README.rdoc
|
51
|
-
- README.rdoc.old
|
52
50
|
- Rakefile
|
53
51
|
- VERSION
|
54
52
|
- djinn.gemspec
|
data/README.rdoc.old
DELETED
@@ -1,251 +0,0 @@
|
|
1
|
-
= Djinn
|
2
|
-
|
3
|
-
Djinn is a very basic helper for building simple daemons.
|
4
|
-
|
5
|
-
In Arabian mythology a Djinn is a supernatural creature which occupies a
|
6
|
-
parallel world to that of mankind.
|
7
|
-
|
8
|
-
== Documentation
|
9
|
-
|
10
|
-
http://rdoc.info/projects/craigp/djinn
|
11
|
-
|
12
|
-
== Installation
|
13
|
-
|
14
|
-
gem install djinn
|
15
|
-
|
16
|
-
== Non-Rails Example (old non-DSL way, will be deprecated soon-ish)
|
17
|
-
|
18
|
-
#!/usr/bin/env ruby
|
19
|
-
|
20
|
-
require 'rubygems'
|
21
|
-
require 'djinn'
|
22
|
-
|
23
|
-
class Basic
|
24
|
-
|
25
|
-
include Djinn
|
26
|
-
|
27
|
-
# Not providing a "perform" method falls back to the base method
|
28
|
-
# in Djinn, which does nothing useful. Make sure your method accepts
|
29
|
-
# a config hash, even if it doesn't use it.
|
30
|
-
def perform options
|
31
|
-
while
|
32
|
-
log "ZOMG! A Djinn?"
|
33
|
-
sleep(5)
|
34
|
-
do
|
35
|
-
end
|
36
|
-
|
37
|
-
# Strictly optional, lets you do stuff when the Djinn daemon stops.
|
38
|
-
# The call to "super" is required, or your daemon will never die
|
39
|
-
def handle_exit
|
40
|
-
log "Handling a nice graceful exit.."
|
41
|
-
super
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|
45
|
-
|
46
|
-
Run it in the foreground like this:
|
47
|
-
|
48
|
-
djinn = Basic.new
|
49
|
-
djinn.run
|
50
|
-
|
51
|
-
But running it in the background is sort of the point. A bit contrived, but
|
52
|
-
this is the general idea:
|
53
|
-
|
54
|
-
djinn.start
|
55
|
-
sleep(10)
|
56
|
-
djinn.stop
|
57
|
-
|
58
|
-
Assuming you didn't sleep there your script would end and the daemon would
|
59
|
-
detach and run in the background until it dies or gets killed. You can wrap
|
60
|
-
argument parsing around that if you want, or do it in any other way. By default
|
61
|
-
the daemon will look for a config YAML file in same directory as you executed it
|
62
|
-
from, named the same as the Djinn class, so in this case *basic.yml*. It will by
|
63
|
-
default create the pid and log files in the same way. You can change this by
|
64
|
-
putting it in the config file or supplying an options hash:
|
65
|
-
|
66
|
-
options = {
|
67
|
-
'pid_file_path' => 'path/to/pid/file',
|
68
|
-
'log_file_path' => 'path/to/log/file'
|
69
|
-
}
|
70
|
-
|
71
|
-
djinn.start(options)
|
72
|
-
|
73
|
-
These options will also be passed to your *perform* method, so you can include
|
74
|
-
anything you need in the hash as well, or in the YAML file for that matter.
|
75
|
-
|
76
|
-
It might seem ugly, but the solution is minimal, and so remains flexible I think.
|
77
|
-
The Rails daemon helpers are an implementation on top of this illustrating how it
|
78
|
-
can be tailored to include some option parsing and so forth, and so do a little
|
79
|
-
more for you.
|
80
|
-
|
81
|
-
== Non-Rails Example (new DSL awesomesauce)
|
82
|
-
|
83
|
-
Using the same silly example, you can do this:
|
84
|
-
|
85
|
-
#!/usr/bin/env ruby
|
86
|
-
|
87
|
-
require 'rubygems'
|
88
|
-
require 'djinn'
|
89
|
-
|
90
|
-
class Basic
|
91
|
-
|
92
|
-
include Djinn
|
93
|
-
|
94
|
-
djinn do
|
95
|
-
on :start do
|
96
|
-
while
|
97
|
-
log "ZOMG! A Djinn?"
|
98
|
-
sleep(5)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
on :exit do
|
103
|
-
log "Handling a nice graceful exit.."
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
end
|
108
|
-
|
109
|
-
Much cleaner and prettier, and no horrible *super* required. Available
|
110
|
-
actions to the *on* method are :start, :stop and :exit
|
111
|
-
|
112
|
-
Run it in the foreground in the same way:
|
113
|
-
|
114
|
-
djinn = Basic.new
|
115
|
-
djinn.run
|
116
|
-
|
117
|
-
The actions are executed in the context of the Djinn itself, so you can
|
118
|
-
get at the config without having to pass it around:
|
119
|
-
|
120
|
-
djinn do
|
121
|
-
on :start do
|
122
|
-
my_setting = config[:omghax]
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
...
|
127
|
-
|
128
|
-
djinn.run { :omghax => "Groovy, baby" }
|
129
|
-
|
130
|
-
You can also give it a block to work with:
|
131
|
-
|
132
|
-
djinn.run do
|
133
|
-
puts "This will happen before calling the :start action"
|
134
|
-
end
|
135
|
-
|
136
|
-
If you need to man-handle the internals and stuff, it yields itself:
|
137
|
-
|
138
|
-
djinn.run do |djinn|
|
139
|
-
djinn.config[:omghax] = "Groovy, baby"
|
140
|
-
end
|
141
|
-
|
142
|
-
Starting in the background is the same as before, call *start* instead of *run*:
|
143
|
-
|
144
|
-
djinn.start
|
145
|
-
|
146
|
-
The Rails Djinns can be built in exactly the same way as this.
|
147
|
-
|
148
|
-
== Rails Example
|
149
|
-
|
150
|
-
There's a simple example in the example directory if you check the code out, but
|
151
|
-
here's the gist of it.
|
152
|
-
|
153
|
-
Assumes a scenario where you have a Book model that keeps a count of how many
|
154
|
-
times a book has been read.
|
155
|
-
|
156
|
-
Create a file in RAILS_ROOT/lib or somewhere similar:
|
157
|
-
|
158
|
-
require 'djinn/rails'
|
159
|
-
require 'eventmachine'
|
160
|
-
|
161
|
-
class BookDjinn
|
162
|
-
|
163
|
-
BOOK_WORKER_INTERVAL = 5
|
164
|
-
|
165
|
-
include Djinn::Rails
|
166
|
-
|
167
|
-
def perform config
|
168
|
-
EM.run do
|
169
|
-
log "Workers will run every #{BOOK_WORKER_INTERVAL} secs"
|
170
|
-
EM::PeriodicTimer.new(BOOK_WORKER_INTERVAL) do
|
171
|
-
log "There are #{Book.count} book(s) in the database"
|
172
|
-
log "Updating read counts for all books.."
|
173
|
-
Book.all.each &:read!
|
174
|
-
end
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
def handle_exit
|
179
|
-
EM.stop
|
180
|
-
super
|
181
|
-
end
|
182
|
-
|
183
|
-
end
|
184
|
-
|
185
|
-
(And, the new more awesome way:)
|
186
|
-
|
187
|
-
require 'djinn/rails'
|
188
|
-
require 'eventmachine'
|
189
|
-
|
190
|
-
class BookDjinn
|
191
|
-
|
192
|
-
BOOK_WORKER_INTERVAL = 5
|
193
|
-
|
194
|
-
include Djinn::Rails
|
195
|
-
|
196
|
-
djinn do
|
197
|
-
|
198
|
-
on :start do
|
199
|
-
EM.run do
|
200
|
-
log "Workers will run every #{BOOK_WORKER_INTERVAL} secs"
|
201
|
-
EM::PeriodicTimer.new(BOOK_WORKER_INTERVAL) do
|
202
|
-
log "There are #{Book.count} book(s) in the database"
|
203
|
-
log "Updating read counts for all books.."
|
204
|
-
Book.all.each &:read!
|
205
|
-
end
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
on :exit do
|
210
|
-
EM.stop
|
211
|
-
end
|
212
|
-
|
213
|
-
end
|
214
|
-
|
215
|
-
end
|
216
|
-
|
217
|
-
Right, now you need to start it somehow. The easiest way is to create a file
|
218
|
-
in RAILS_ROOT/scripts and pop this in it:
|
219
|
-
|
220
|
-
#!/usr/bin/env ruby
|
221
|
-
require 'rubygems'
|
222
|
-
require File.join(File.dirname(__FILE__), '../lib/book_djinn')
|
223
|
-
BookDjinn.go ARGV
|
224
|
-
|
225
|
-
Righto, now start it from RAILS_ROOT:
|
226
|
-
|
227
|
-
ruby script/book_djinn
|
228
|
-
|
229
|
-
Okay, but that defaults to _run_, which starts it in the foreground and also
|
230
|
-
uses the rails development environment by default. Try this:
|
231
|
-
|
232
|
-
ruby script/book_djinn --help
|
233
|
-
|
234
|
-
That should give you a better idea of what's going on, then try this:
|
235
|
-
|
236
|
-
ruby script/book_djinn start -e production
|
237
|
-
|
238
|
-
Yay, we have a daemon running in the background! To stop it:
|
239
|
-
|
240
|
-
ruby script/book_djinn stop
|
241
|
-
|
242
|
-
That gives you more-or-less everything you need to build something basic
|
243
|
-
and monitor it with god or a similar process monitor.
|
244
|
-
|
245
|
-
== TODO
|
246
|
-
|
247
|
-
Lots.
|
248
|
-
|
249
|
-
== Copyright
|
250
|
-
|
251
|
-
Copyright (c) 2010 Craig Paterson. See LICENSE for details.
|