djinn 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +37 -14
- data/Rakefile +5 -0
- data/VERSION +1 -1
- data/djinn.gemspec +2 -1
- data/lib/djinn.rb +2 -0
- data/lib/djinn/base.rb +19 -6
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
=
|
1
|
+
= Djinn
|
2
2
|
|
3
|
-
Djinn is a very basic helper for building simple daemons
|
3
|
+
Djinn is a very basic helper for building simple daemons.
|
4
4
|
|
5
5
|
== Non-Rails Example
|
6
6
|
|
7
|
-
#!/usr/bin/env ruby
|
7
|
+
#!/usr/bin/env ruby
|
8
8
|
|
9
9
|
require 'rubygems'
|
10
10
|
require 'djinn'
|
@@ -41,9 +41,32 @@ this is the general idea:
|
|
41
41
|
sleep(10)
|
42
42
|
djinn.stop
|
43
43
|
|
44
|
+
Assuming you didn't sleep there your script would end and the daemon would
|
45
|
+
detach and run in the background until it dies or gets killed. You can wrap
|
46
|
+
argument parsing around that if you want, or do it in any other way. By default
|
47
|
+
the daemon will look for a config YAML file in same directory as you executed it
|
48
|
+
from, named the same as the Djinn class, so in this case _basic.yml_. It will by
|
49
|
+
default create the pid and log files in the same way. You can change this by
|
50
|
+
putting it in the config file or supplying an options hash:
|
51
|
+
|
52
|
+
options = {
|
53
|
+
'pid_file_path' => 'path/to/pid/file',
|
54
|
+
'log_file_path' => 'path/to/log/file'
|
55
|
+
}
|
56
|
+
|
57
|
+
djinn.start(options)
|
58
|
+
|
59
|
+
These options will also be passed to your *perform* method, so you can include
|
60
|
+
anything you need in the hash as well, or in the YAML file for that matter.
|
61
|
+
|
62
|
+
It might seem ugly, but the solution is minimal, and so remains flexible I think.
|
63
|
+
The rails daemon helpers are an implementation on top of this illustrating how it
|
64
|
+
can be tailored to include some option parsing and so forth, and so do a little
|
65
|
+
more for you.
|
66
|
+
|
44
67
|
== Rails Example
|
45
68
|
|
46
|
-
There's a
|
69
|
+
There's a simple example in the example directory if you check the code out, but
|
47
70
|
here's the gist of it.
|
48
71
|
|
49
72
|
Assumes a scenario where you have a Book model that keeps a count of how many
|
@@ -51,12 +74,12 @@ times a book has been read.
|
|
51
74
|
|
52
75
|
Create a file in RAILS_ROOT/lib or somewhere similar:
|
53
76
|
|
54
|
-
|
77
|
+
require 'djinn/rails'
|
55
78
|
|
56
79
|
class BookDjinn
|
57
|
-
|
58
|
-
|
59
|
-
|
80
|
+
|
81
|
+
BOOK_WORKER_INTERVAL = 5
|
82
|
+
|
60
83
|
include Djinn::Rails
|
61
84
|
|
62
85
|
def perform config
|
@@ -80,16 +103,16 @@ Create a file in RAILS_ROOT/lib or somewhere similar:
|
|
80
103
|
Right, now you need to start it somehow. The easiest way is to create a file
|
81
104
|
in RAILS_ROOT/scripts and pop this in it:
|
82
105
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
106
|
+
#!/usr/bin/env ruby
|
107
|
+
require 'rubygems'
|
108
|
+
require File.join(File.dirname(__FILE__), '../lib/book_djinn')
|
109
|
+
BookDjinn.go ARGV
|
87
110
|
|
88
111
|
Righto, now start it from RAILS_ROOT:
|
89
112
|
|
90
113
|
ruby script/book_djinn
|
91
114
|
|
92
|
-
Okay, but that defaults to
|
115
|
+
Okay, but that defaults to _run_, which starts it in the foreground and also
|
93
116
|
uses the rails development environment by default. Try this:
|
94
117
|
|
95
118
|
ruby script/book_djinn --help
|
@@ -107,7 +130,7 @@ and monitor it with god or a similar process monitor.
|
|
107
130
|
|
108
131
|
== TODO
|
109
132
|
|
110
|
-
|
133
|
+
Lots. Keep 'em peeled.
|
111
134
|
|
112
135
|
== Copyright
|
113
136
|
|
data/Rakefile
CHANGED
@@ -13,9 +13,14 @@ begin
|
|
13
13
|
gem.add_development_dependency "shoulda", ">= 2.11.1"
|
14
14
|
gem.files.exclude 'example/**/*'
|
15
15
|
gem.test_files.exclude 'example/**/*'
|
16
|
+
gem.has_rdoc = true
|
17
|
+
gem.rubyforge_project = 'djinn'
|
16
18
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
19
|
end
|
18
20
|
Jeweler::GemcutterTasks.new
|
21
|
+
Jeweler::RubyforgeTasks.new do |rubyforge|
|
22
|
+
rubyforge.doc_task = "rdoc"
|
23
|
+
end
|
19
24
|
rescue LoadError
|
20
25
|
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
21
26
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.6
|
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.0.
|
8
|
+
s.version = "0.0.6"
|
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"]
|
@@ -37,6 +37,7 @@ Gem::Specification.new do |s|
|
|
37
37
|
s.homepage = %q{http://github.com/craigp/djinn}
|
38
38
|
s.rdoc_options = ["--charset=UTF-8"]
|
39
39
|
s.require_paths = ["lib"]
|
40
|
+
s.rubyforge_project = %q{djinn}
|
40
41
|
s.rubygems_version = %q{1.3.7}
|
41
42
|
s.summary = %q{Helper for creating simple custom daemons}
|
42
43
|
s.test_files = [
|
data/lib/djinn.rb
CHANGED
data/lib/djinn/base.rb
CHANGED
@@ -5,6 +5,7 @@ require 'pid_file'
|
|
5
5
|
require 'logging'
|
6
6
|
|
7
7
|
module Djinn
|
8
|
+
# The base class from which all Djinn spring forth
|
8
9
|
module Base
|
9
10
|
|
10
11
|
include Djinn::Tonic
|
@@ -12,8 +13,9 @@ module Djinn
|
|
12
13
|
|
13
14
|
attr_reader :config
|
14
15
|
|
16
|
+
# Base implementation does nothing worthwhile, you should override this
|
17
|
+
# in your own implementation
|
15
18
|
def perform config={}
|
16
|
-
# base implementation does nothing worthwhile
|
17
19
|
trap('TERM') { handle_exit }
|
18
20
|
trap('INT') { handle_exit }
|
19
21
|
while true
|
@@ -22,11 +24,13 @@ module Djinn
|
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
27
|
+
# Override this with useful exit code if you need to, but remember to
|
28
|
+
# call *super* or call *exit* yourself, or your Djinn will be immortal
|
25
29
|
def handle_exit
|
26
|
-
# override this with useful exit code if you need it
|
27
30
|
exit(0)
|
28
31
|
end
|
29
32
|
|
33
|
+
# Starts the Djinn in the background
|
30
34
|
def start config={}
|
31
35
|
@config = (config.empty?) ? load_config : config.empty?
|
32
36
|
log "Starting #{name} in the background.."
|
@@ -38,6 +42,8 @@ module Djinn
|
|
38
42
|
end
|
39
43
|
end
|
40
44
|
|
45
|
+
# Starts the Djinn in the foreground, which is often useful for
|
46
|
+
# testing or other noble pursuits
|
41
47
|
def run config={}
|
42
48
|
@config = (config.empty?) ? load_config : config.empty?
|
43
49
|
log "Starting #{name} in the foreground.."
|
@@ -46,13 +52,17 @@ module Djinn
|
|
46
52
|
perform(config)
|
47
53
|
end
|
48
54
|
|
49
|
-
|
55
|
+
# Convenience method, really just calls *stop* and then *start* for you :P
|
56
|
+
def restart config={}
|
50
57
|
stop
|
51
|
-
start
|
58
|
+
start(config)
|
52
59
|
end
|
53
60
|
|
54
|
-
|
55
|
-
|
61
|
+
# Stops the Djinn, unless you change the location of the pid file, in
|
62
|
+
# which case its all about you and the *kill* command
|
63
|
+
def stop config={}
|
64
|
+
@config = (config.empty?) ? load_config : config.empty?
|
65
|
+
pidfile = get_pidfile(@config)
|
56
66
|
log 'No such process' and exit unless pidfile.pid
|
57
67
|
begin
|
58
68
|
log "Sending TERM signal to process #{pidfile.pid}"
|
@@ -66,12 +76,15 @@ module Djinn
|
|
66
76
|
|
67
77
|
protected
|
68
78
|
|
79
|
+
# The name used to identify the Djinn, for pid and log file
|
80
|
+
# naming, as well as finding default config files
|
69
81
|
def name
|
70
82
|
underscore(self.class.name)
|
71
83
|
end
|
72
84
|
|
73
85
|
private
|
74
86
|
|
87
|
+
# Shamelessly stolen from the Rails source
|
75
88
|
def underscore(camel_cased_word)
|
76
89
|
camel_cased_word.to_s.gsub(/::/, '/').
|
77
90
|
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
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: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Craig Paterson
|
@@ -89,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
89
|
version: "0"
|
90
90
|
requirements: []
|
91
91
|
|
92
|
-
rubyforge_project:
|
92
|
+
rubyforge_project: djinn
|
93
93
|
rubygems_version: 1.3.7
|
94
94
|
signing_key:
|
95
95
|
specification_version: 3
|