foreverb 0.1.5 → 0.1.6
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.md +2 -2
- data/lib/forever.rb +1 -0
- data/lib/forever/base.rb +1 -126
- data/lib/forever/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Small daemon framework **for ruby**, with logging, error handler watcher and much more.
|
4
4
|
|
5
|
-
|
5
|
+
My inspiration was [forever for node.js](https://raw.github.com/indexzero/forever) written by Charlie Robbins.
|
6
6
|
|
7
7
|
Please don't consider this gem as a port because is not.
|
8
8
|
|
@@ -29,7 +29,7 @@ So, if you have my needs, **Forever** can be the right choice for you.
|
|
29
29
|
## Install:
|
30
30
|
|
31
31
|
``` sh
|
32
|
-
$ gem install
|
32
|
+
$ gem install foreverb
|
33
33
|
```
|
34
34
|
|
35
35
|
## Deamon Example:
|
data/lib/forever.rb
CHANGED
data/lib/forever/base.rb
CHANGED
@@ -17,132 +17,7 @@ module Forever
|
|
17
17
|
|
18
18
|
fork do
|
19
19
|
$0 = "Forever: #{$0}"
|
20
|
-
puts "=> Process demonized with pid #{Process.pid}"
|
21
|
-
|
22
|
-
%w(INT TERM KILL).each { |signal| trap(signal) { stop! } }
|
23
|
-
|
24
|
-
File.open(pid, "w") { |f| f.write(Process.pid.to_s) } if pid
|
25
|
-
|
26
|
-
stream = log ? File.new(log, "w") : File.open('/dev/null', 'w')
|
27
|
-
stream.sync = true
|
28
|
-
|
29
|
-
STDOUT.reopen(stream)
|
30
|
-
STDERR.reopen(STDOUT)
|
31
|
-
|
32
|
-
begin
|
33
|
-
on_ready.call
|
34
|
-
rescue Exception => e
|
35
|
-
Thread.list.reject { |t| t==Thread.current }.map(&:kill)
|
36
|
-
on_error[e] if on_error
|
37
|
-
stream.print "\n\n%s\n %s\n\n" % [e.message, e.backtrace.join("\n ")]
|
38
|
-
sleep 30
|
39
|
-
retry
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
##
|
45
|
-
# Caller file
|
46
|
-
#
|
47
|
-
def file(value=nil)
|
48
|
-
value ? @_file = value : @_file
|
49
|
-
end
|
50
|
-
|
51
|
-
##
|
52
|
-
# Base working Directory
|
53
|
-
#
|
54
|
-
def dir(value=nil)
|
55
|
-
value ? @_dir = value : @_dir
|
56
|
-
end
|
57
|
-
|
58
|
-
##
|
59
|
-
# File were we redirect STOUT and STDERR, can be false.
|
60
|
-
#
|
61
|
-
# Default: dir + 'log/[process_name].log'
|
62
|
-
#
|
63
|
-
def log(value=nil)
|
64
|
-
@_log ||= File.join(dir, "log/#{File.basename(file)}.log") if exists?(dir, file)
|
65
|
-
value ? @_log = value : @_log
|
66
|
-
end
|
67
|
-
|
68
|
-
##
|
69
|
-
# File were we store pid
|
70
|
-
#
|
71
|
-
# Default: dir + 'tmp/[process_name].pid'
|
72
|
-
#
|
73
|
-
def pid(value=nil)
|
74
|
-
@_pid ||= File.join(dir, "tmp/#{File.basename(file)}.pid") if exists?(dir, file)
|
75
|
-
value ? @_pid = value : @_pid
|
76
|
-
end
|
77
|
-
|
78
|
-
##
|
79
|
-
# Search if there is a running process and stop it
|
80
|
-
#
|
81
|
-
def stop!(kill=true)
|
82
|
-
if exists?(pid)
|
83
|
-
_pid = File.read(pid).to_i
|
84
|
-
puts "=> Found pid #{_pid}..."
|
85
|
-
FileUtils.rm_f(pid)
|
86
|
-
begin
|
87
|
-
puts "=> Killing process #{_pid}..."
|
88
|
-
Process.kill(:KILL, _pid)
|
89
|
-
rescue Errno::ESRCH => e
|
90
|
-
puts "=> #{e.message}"
|
91
|
-
end
|
92
|
-
else
|
93
|
-
puts "=> Pid not found, process seems don't exist!"
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
##
|
98
|
-
# Callback raised when an error occour
|
99
|
-
#
|
100
|
-
def on_error(&block)
|
101
|
-
block_given? ? @_on_error = block : @_on_error
|
102
|
-
end
|
103
|
-
|
104
|
-
##
|
105
|
-
# Callback to fire when the daemon start
|
106
|
-
#
|
107
|
-
def on_ready(&block)
|
108
|
-
block_given? ? @_on_error = block : @_on_error
|
109
|
-
end
|
110
|
-
|
111
|
-
def to_s
|
112
|
-
"#<Forever dir:#{dir}, file:#{file}, log:#{log}, pid:#{pid}>"
|
113
|
-
end
|
114
|
-
alias :inspect :to_s
|
115
|
-
|
116
|
-
def config
|
117
|
-
{ :dir => dir, :file => file, :log => log, :pid => pid }.to_yaml
|
118
|
-
end
|
119
|
-
alias :to_yaml :config
|
120
|
-
|
121
|
-
private
|
122
|
-
def exists?(*values)
|
123
|
-
values.all? { |value| value && File.exist?(value) }
|
124
|
-
end
|
125
|
-
end # Base
|
126
|
-
end # Foreverrequire 'fileutils'
|
127
|
-
|
128
|
-
module Forever
|
129
|
-
class Base
|
130
|
-
def initialize(options={}, &block)
|
131
|
-
options.each { |k,v| send(k, v) }
|
132
|
-
|
133
|
-
instance_eval(&block)
|
134
|
-
|
135
|
-
Dir.chdir(dir) if exists?(dir)
|
136
|
-
Dir.mkdir(File.dirname(log)) if log && !File.exist?(File.dirname(log))
|
137
|
-
Dir.mkdir(File.dirname(pid)) if pid && !File.exist?(File.dirname(pid))
|
138
|
-
|
139
|
-
stop!
|
140
|
-
|
141
|
-
return if ARGV[0] == "stop" || on_ready.nil?
|
142
|
-
|
143
|
-
fork do
|
144
|
-
$0 = "Forever: #{$0}"
|
145
|
-
puts "=> Process demonized with pid #{Process.pid}"
|
20
|
+
puts "=> Process demonized with pid #{Process.pid} with Forever v.#{Forever::VERSION}"
|
146
21
|
|
147
22
|
%w(INT TERM KILL).each { |signal| trap(signal) { stop! } }
|
148
23
|
|
data/lib/forever/version.rb
CHANGED