djinn 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/README.rdoc +67 -3
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/djinn.gemspec +3 -3
- data/lib/djinn.rb +7 -0
- data/lib/djinn/base.rb +12 -8
- data/lib/djinn/dsl.rb +36 -0
- data/lib/djinn/rails.rb +8 -13
- metadata +6 -5
data/.gitignore
CHANGED
data/README.rdoc
CHANGED
@@ -2,7 +2,11 @@
|
|
2
2
|
|
3
3
|
Djinn is a very basic helper for building simple daemons.
|
4
4
|
|
5
|
-
==
|
5
|
+
== Documentation
|
6
|
+
|
7
|
+
http://rdoc.info/projects/craigp/djinn
|
8
|
+
|
9
|
+
== Non-Rails Example (old non-DSL way, will be deprecated soon-ish)
|
6
10
|
|
7
11
|
#!/usr/bin/env ruby
|
8
12
|
|
@@ -60,9 +64,69 @@ These options will also be passed to your *perform* method, so you can include
|
|
60
64
|
anything you need in the hash as well, or in the YAML file for that matter.
|
61
65
|
|
62
66
|
It might seem ugly, but the solution is minimal, and so remains flexible I think.
|
63
|
-
The
|
67
|
+
The Rails daemon helpers are an implementation on top of this illustrating how it
|
64
68
|
can be tailored to include some option parsing and so forth, and so do a little
|
65
69
|
more for you.
|
70
|
+
|
71
|
+
== Non-Rails Example (new DSL awesomesauce)
|
72
|
+
|
73
|
+
Using the same silly example, you can do this:
|
74
|
+
|
75
|
+
#!/usr/bin/env ruby
|
76
|
+
|
77
|
+
require 'rubygems'
|
78
|
+
require 'djinn'
|
79
|
+
|
80
|
+
class Basic
|
81
|
+
|
82
|
+
include Djinn
|
83
|
+
|
84
|
+
djinn do
|
85
|
+
on :start do
|
86
|
+
log "ZOMG! A Djinn?"
|
87
|
+
end
|
88
|
+
|
89
|
+
on :exit do
|
90
|
+
log "Handling a nice graceful exit.."
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
Much cleaner and prettier, and no horrible *super* required. Available
|
97
|
+
actions to the *on* method are *:start*, *:stop* and *:exit*
|
98
|
+
|
99
|
+
Run it in the foreground in the same way:
|
100
|
+
|
101
|
+
djinn = Basic.new
|
102
|
+
djinn.run
|
103
|
+
|
104
|
+
The actions are executed in the context of the Djinn itself, so you can
|
105
|
+
get at the config without having to pass it around:
|
106
|
+
|
107
|
+
djinn do
|
108
|
+
on :start do
|
109
|
+
my_setting = config[:omghax]
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
...
|
114
|
+
|
115
|
+
djinn.run { :omghax => "Groovy, baby" }
|
116
|
+
|
117
|
+
You can also give it a block to work with:
|
118
|
+
|
119
|
+
djinn.run do
|
120
|
+
puts "This will happen before calling the :start action"
|
121
|
+
end
|
122
|
+
|
123
|
+
If you need to man-handle the internals and stuff, it yields itself:
|
124
|
+
|
125
|
+
djinn.run do |djinn|
|
126
|
+
djinn.config[:omghax] = "Groovy, baby"
|
127
|
+
end
|
128
|
+
|
129
|
+
The Rails Djinn's can be built in exactly the same way as this.
|
66
130
|
|
67
131
|
== Rails Example
|
68
132
|
|
@@ -130,7 +194,7 @@ and monitor it with god or a similar process monitor.
|
|
130
194
|
|
131
195
|
== TODO
|
132
196
|
|
133
|
-
Lots.
|
197
|
+
Lots.
|
134
198
|
|
135
199
|
== Copyright
|
136
200
|
|
data/Rakefile
CHANGED
@@ -13,8 +13,8 @@ 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
|
+
# gem.has_rdoc = true
|
17
|
+
# gem.rubyforge_project = 'djinn'
|
18
18
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
19
19
|
end
|
20
20
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.9
|
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.0.
|
8
|
+
s.version = "0.0.9"
|
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-07-
|
12
|
+
s.date = %q{2010-07-28}
|
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 = [
|
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
|
|
26
26
|
"djinn.gemspec",
|
27
27
|
"lib/djinn.rb",
|
28
28
|
"lib/djinn/base.rb",
|
29
|
+
"lib/djinn/dsl.rb",
|
29
30
|
"lib/djinn/logging.rb",
|
30
31
|
"lib/djinn/pid_file.rb",
|
31
32
|
"lib/djinn/rails.rb",
|
@@ -37,7 +38,6 @@ Gem::Specification.new do |s|
|
|
37
38
|
s.homepage = %q{http://github.com/craigp/djinn}
|
38
39
|
s.rdoc_options = ["--charset=UTF-8"]
|
39
40
|
s.require_paths = ["lib"]
|
40
|
-
s.rubyforge_project = %q{djinn}
|
41
41
|
s.rubygems_version = %q{1.3.7}
|
42
42
|
s.summary = %q{Helper for creating simple custom daemons}
|
43
43
|
s.test_files = [
|
data/lib/djinn.rb
CHANGED
@@ -1,11 +1,18 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__))
|
2
|
+
|
1
3
|
require 'yaml'
|
2
4
|
require 'djinn/base'
|
5
|
+
require 'djinn/dsl'
|
3
6
|
|
4
7
|
# This is a base implementation which handles looking for config
|
5
8
|
# files and sets up the default locations for pid and log files
|
6
9
|
module Djinn
|
7
10
|
|
8
11
|
include Djinn::Base
|
12
|
+
|
13
|
+
def self.included(base)
|
14
|
+
base.extend Djinn::Dsl
|
15
|
+
end
|
9
16
|
|
10
17
|
private
|
11
18
|
|
data/lib/djinn/base.rb
CHANGED
@@ -7,10 +7,10 @@ require 'logging'
|
|
7
7
|
module Djinn
|
8
8
|
# The base class from which all Djinn spring forth
|
9
9
|
module Base
|
10
|
-
|
10
|
+
|
11
11
|
include Djinn::Tonic
|
12
12
|
include Djinn::Logging
|
13
|
-
|
13
|
+
|
14
14
|
attr_reader :config
|
15
15
|
|
16
16
|
# Base implementation does nothing worthwhile, you should override this
|
@@ -18,8 +18,8 @@ module Djinn
|
|
18
18
|
def perform config={}
|
19
19
|
trap('TERM') { handle_exit }
|
20
20
|
trap('INT') { handle_exit }
|
21
|
-
while
|
22
|
-
log("[#{name}] Djinn is running..")
|
21
|
+
while
|
22
|
+
log("[#{name}] Djinn is running.. and doing nothing worthwhile.")
|
23
23
|
sleep(5)
|
24
24
|
end
|
25
25
|
end
|
@@ -27,6 +27,7 @@ module Djinn
|
|
27
27
|
# Override this with useful exit code if you need to, but remember to
|
28
28
|
# call *super* or call *exit* yourself, or your Djinn will be immortal
|
29
29
|
def handle_exit
|
30
|
+
__exit! if respond_to?(:__exit!)
|
30
31
|
exit(0)
|
31
32
|
end
|
32
33
|
|
@@ -36,10 +37,10 @@ module Djinn
|
|
36
37
|
log "Starting #{name} in the background.."
|
37
38
|
logfile = get_logfile(config)
|
38
39
|
daemonize(logfile, get_pidfile(config)) do
|
39
|
-
yield if block_given?
|
40
|
+
yield(self) if block_given?
|
40
41
|
trap('TERM') { handle_exit }
|
41
42
|
trap('INT') { handle_exit }
|
42
|
-
perform(@config)
|
43
|
+
(respond_to?(:__start!)) ? __start! : perform(@config)
|
43
44
|
end
|
44
45
|
end
|
45
46
|
|
@@ -50,8 +51,8 @@ module Djinn
|
|
50
51
|
log "Starting #{name} in the foreground.."
|
51
52
|
trap('TERM') { handle_exit }
|
52
53
|
trap('INT') { handle_exit }
|
53
|
-
yield if block_given?
|
54
|
-
perform(@config)
|
54
|
+
yield(self) if block_given?
|
55
|
+
(respond_to?(:__start!)) ? __start! : perform(@config)
|
55
56
|
end
|
56
57
|
|
57
58
|
# Convenience method, really just calls *stop* and then *start* for you :P
|
@@ -64,6 +65,8 @@ module Djinn
|
|
64
65
|
# which case its all about you and the *kill* command
|
65
66
|
def stop config={}
|
66
67
|
@config = (config.empty?) ? load_config : config
|
68
|
+
yield(self) if block_given?
|
69
|
+
__stop! if respond_to?(:__stop!)
|
67
70
|
pidfile = get_pidfile(@config)
|
68
71
|
log 'No such process' and exit unless pidfile.pid
|
69
72
|
begin
|
@@ -74,6 +77,7 @@ module Djinn
|
|
74
77
|
ensure
|
75
78
|
pidfile.remove
|
76
79
|
end
|
80
|
+
|
77
81
|
end
|
78
82
|
|
79
83
|
protected
|
data/lib/djinn/dsl.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Djinn
|
2
|
+
module Dsl
|
3
|
+
|
4
|
+
def djinn &block
|
5
|
+
@dsl_helper = DslHelper.new(&block)
|
6
|
+
@dsl_helper.actions.each do |action, proc|
|
7
|
+
module_eval do
|
8
|
+
define_method "__#{action}!".intern do |*args|
|
9
|
+
instance_exec(&proc)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class DslHelper
|
16
|
+
|
17
|
+
attr_accessor :actions
|
18
|
+
|
19
|
+
def initialize &block
|
20
|
+
@actions = {}
|
21
|
+
instance_eval(&block)
|
22
|
+
end
|
23
|
+
|
24
|
+
def on action, &block
|
25
|
+
acceptable_actions = %w(start stop exit)
|
26
|
+
raise DslActionError.new("\"#{action}\" is unrecognized, please use one of: #{acceptable_actions.join(', ')}") \
|
27
|
+
unless acceptable_actions.include?(action.to_s)
|
28
|
+
@actions[action] = block
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
class DslActionError < Exception; end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
data/lib/djinn/rails.rb
CHANGED
@@ -1,27 +1,22 @@
|
|
1
1
|
$:.unshift(File.join(File.dirname(__FILE__)))
|
2
2
|
|
3
3
|
require 'yaml'
|
4
|
+
|
5
|
+
require 'base'
|
4
6
|
require 'rails/handlers'
|
7
|
+
require 'dsl'
|
5
8
|
|
6
9
|
module Djinn
|
7
10
|
module Rails
|
8
11
|
|
9
|
-
require 'base'
|
10
|
-
require 'logging'
|
11
|
-
require 'pid_file'
|
12
|
-
require 'tonic'
|
13
|
-
|
14
12
|
include Djinn::Base
|
15
|
-
|
16
|
-
RAILS_ROOT = File.expand_path(Dir.pwd) unless defined?(RAILS_ROOT)
|
17
13
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
base.__send__(:extend, Djinn::Rails::Handlers)
|
22
|
-
end
|
23
|
-
|
14
|
+
def self.included(base)
|
15
|
+
base.__send__(:extend, Djinn::Rails::Handlers)
|
16
|
+
base.__send__(:extend, Djinn::Dsl)
|
24
17
|
end
|
18
|
+
|
19
|
+
RAILS_ROOT = File.expand_path(Dir.pwd) unless defined?(RAILS_ROOT)
|
25
20
|
|
26
21
|
private
|
27
22
|
|
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: 13
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 9
|
10
|
+
version: 0.0.9
|
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-07-
|
18
|
+
date: 2010-07-28 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -53,6 +53,7 @@ files:
|
|
53
53
|
- djinn.gemspec
|
54
54
|
- lib/djinn.rb
|
55
55
|
- lib/djinn/base.rb
|
56
|
+
- lib/djinn/dsl.rb
|
56
57
|
- lib/djinn/logging.rb
|
57
58
|
- lib/djinn/pid_file.rb
|
58
59
|
- lib/djinn/rails.rb
|
@@ -89,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
90
|
version: "0"
|
90
91
|
requirements: []
|
91
92
|
|
92
|
-
rubyforge_project:
|
93
|
+
rubyforge_project:
|
93
94
|
rubygems_version: 1.3.7
|
94
95
|
signing_key:
|
95
96
|
specification_version: 3
|