daemon-ogre 1.5.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/.gitignore +18 -0
- data/Gemfile.lock +12 -17
- data/README.md +14 -213
- data/VERSION +1 -1
- data/daemon-ogre.gemspec +10 -16
- data/example/daemonizable.rb +16 -0
- data/lib/daemon-ogre/argv.rb +29 -0
- data/lib/daemon-ogre/daemon.rb +159 -51
- data/lib/daemon-ogre/opts.rb +34 -0
- data/lib/daemon-ogre.rb +7 -6
- data/test/{test_daemon-ogre.rb → example.rb} +1 -1
- data/test/test.rb +3 -0
- metadata +76 -28
- data/README.rdoc +0 -247
- data/example/sample_daemon_app.rb +0 -19
- data/files.rb +0 -24
- data/lib/daemon-ogre/app.rb +0 -40
- data/lib/daemon-ogre/mpatch.rb +0 -93
- data/lib/daemon-ogre/server.rb +0 -199
- data/lib/daemon-ogre/startup.rb +0 -85
- data/lib/daemon-ogre/support.rb +0 -246
- data/test/helper.rb +0 -18
@@ -1,19 +0,0 @@
|
|
1
|
-
require_relative File.join '..','lib','daemon-ogre.rb'
|
2
|
-
|
3
|
-
# start as
|
4
|
-
# ruby sample_daemon_app.rb start daemon
|
5
|
-
#
|
6
|
-
# to check it's running
|
7
|
-
# ps aux | grep MySuperAppName
|
8
|
-
|
9
|
-
DaemonOgre.start :name => "MySuperAppName",
|
10
|
-
:log_path => File.join('.','var','log','log_file_name'),
|
11
|
-
:pid_path => File.join('.','var','pid','pid_file_name'),
|
12
|
-
:terminate => true
|
13
|
-
|
14
|
-
10.times do
|
15
|
-
|
16
|
-
puts Time.now
|
17
|
-
sleep 1
|
18
|
-
|
19
|
-
end
|
data/files.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
### Get Files from dir
|
2
|
-
begin
|
3
|
-
|
4
|
-
files_to_be_loaded = %w[version.rb]
|
5
|
-
|
6
|
-
SpecFiles= Array.new
|
7
|
-
|
8
|
-
Dir[File.expand_path(File.join(File.dirname(__FILE__),"**","*"))].sort.uniq.each do |one_file_name|
|
9
|
-
one_file_name = File.expand_path one_file_name
|
10
|
-
file_name = one_file_name[(File.expand_path(File.dirname(__FILE__)).to_s.length+1)..(one_file_name.length-1)]
|
11
|
-
|
12
|
-
if !one_file_name.include?("pkg")
|
13
|
-
if !File.directory? file_name
|
14
|
-
SpecFiles.push file_name
|
15
|
-
STDOUT.puts file_name if $DEBUG
|
16
|
-
if files_to_be_loaded.include? one_file_name.split(File::SEPARATOR).last
|
17
|
-
load one_file_name
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
data/lib/daemon-ogre/app.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
#DaemonOgreBody
|
2
|
-
begin
|
3
|
-
module DaemonOgre
|
4
|
-
|
5
|
-
#config
|
6
|
-
begin
|
7
|
-
class App
|
8
|
-
class << self
|
9
|
-
attr_accessor :log_path,
|
10
|
-
:pid_path,
|
11
|
-
:daemon_stderr,
|
12
|
-
:exceptions,
|
13
|
-
:exlogger,
|
14
|
-
:app_name,
|
15
|
-
:port,
|
16
|
-
:terminate
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
#default
|
22
|
-
begin
|
23
|
-
App.log_path = "./var/log/logfile.log"
|
24
|
-
App.pid_path = "./var/pid/pidfile.pid"
|
25
|
-
App.terminate = false
|
26
|
-
App.port = 80
|
27
|
-
App.app_name = $0
|
28
|
-
|
29
|
-
begin
|
30
|
-
['daemon_stderr','exceptions','exlogger'].each do |one_log|
|
31
|
-
|
32
|
-
App.__send__(one_log+"=",clone_mpath(App.log_path,one_log+".log"))
|
33
|
-
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
end
|
data/lib/daemon-ogre/mpatch.rb
DELETED
@@ -1,93 +0,0 @@
|
|
1
|
-
#monkey patch
|
2
|
-
begin
|
3
|
-
def process_running?(input)
|
4
|
-
DaemonOgre.process_running?(input)
|
5
|
-
end
|
6
|
-
def require_directory(directory,*args)
|
7
|
-
DaemonOgre.load_directory(directory,{:monkey_patch => 1},*args)
|
8
|
-
end
|
9
|
-
def require_ymls(directory)
|
10
|
-
DaemonOgre.load_ymls(
|
11
|
-
directory,
|
12
|
-
{:monkey_patch => 1}
|
13
|
-
)
|
14
|
-
end
|
15
|
-
def get_port(port,max_port=65535 ,host="0.0.0.0")
|
16
|
-
DaemonOgre.get_port(port,max_port,host)
|
17
|
-
end
|
18
|
-
def exlogger(error_msg,*args)
|
19
|
-
arg=Hash[*args]
|
20
|
-
arg[:prefix] = String.new if arg[:prefix].nil?
|
21
|
-
arg[:path] = DaemonOgre::App.exlogger if arg[:path].nil?
|
22
|
-
DaemonOgre.create_on_filesystem arg[:path],
|
23
|
-
'a+'
|
24
|
-
DaemonOgre.error_logger(error_msg,arg[:prefix],arg[:path])
|
25
|
-
end
|
26
|
-
|
27
|
-
|
28
|
-
class Exception
|
29
|
-
def logger
|
30
|
-
DaemonOgre.create_on_filesystem DaemonOgre::App.exceptions,
|
31
|
-
'a+'
|
32
|
-
DaemonOgre.error_logger(self.backtrace,self,DaemonOgre::App.exceptions)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
class File
|
36
|
-
def self.create!(file,optionable_data=nil,optionable_file_mod="w")
|
37
|
-
DaemonOgre.create_on_filesystem file,
|
38
|
-
optionable_file_mod,
|
39
|
-
optionable_data
|
40
|
-
end
|
41
|
-
end
|
42
|
-
class Class
|
43
|
-
def class_methods
|
44
|
-
self.methods - Object.methods
|
45
|
-
end
|
46
|
-
def self.class_methods
|
47
|
-
self.methods - Object.methods
|
48
|
-
end
|
49
|
-
end
|
50
|
-
class Rnd
|
51
|
-
class << self
|
52
|
-
def string(length,amount=1)
|
53
|
-
mrg = String.new
|
54
|
-
first_string = true
|
55
|
-
amount.times do
|
56
|
-
a_string = Random.rand(length)
|
57
|
-
a_string == 0 ? a_string += 1 : a_string
|
58
|
-
mrg_prt = (0...a_string).map{ ('a'..'z').to_a[rand(26)] }.join
|
59
|
-
first_string ? mrg += mrg_prt : mrg+= " #{mrg_prt}"
|
60
|
-
first_string = false
|
61
|
-
end
|
62
|
-
return mrg
|
63
|
-
end
|
64
|
-
def integer(length)
|
65
|
-
Random.rand(length)
|
66
|
-
end
|
67
|
-
def boolean
|
68
|
-
rand(2) == 1
|
69
|
-
end
|
70
|
-
def date from = Time.at(1114924812), to = Time.now
|
71
|
-
rand(from..to)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
class Array
|
76
|
-
def index_of(target_element)
|
77
|
-
array = self
|
78
|
-
hash = Hash[array.map.with_index.to_a]
|
79
|
-
return hash[target_element]
|
80
|
-
end
|
81
|
-
end
|
82
|
-
class Hash
|
83
|
-
#pass single or array of keys, which will be removed, returning the remaining hash
|
84
|
-
def remove!(*keys)
|
85
|
-
keys.each{|key| self.delete(key) }
|
86
|
-
self
|
87
|
-
end
|
88
|
-
#non-destructive version
|
89
|
-
def remove(*keys)
|
90
|
-
self.dup.remove!(*keys)
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
data/lib/daemon-ogre/server.rb
DELETED
@@ -1,199 +0,0 @@
|
|
1
|
-
#DaemonOgreBody
|
2
|
-
begin
|
3
|
-
module DaemonOgre
|
4
|
-
|
5
|
-
#server_model
|
6
|
-
begin
|
7
|
-
class Server
|
8
|
-
@@startup = false
|
9
|
-
class << self
|
10
|
-
|
11
|
-
def daemon
|
12
|
-
puts "#{$0} daemon is watching you..."
|
13
|
-
DaemonOgre.create_on_filesystem DaemonOgre::App.pid_path,
|
14
|
-
'a+'
|
15
|
-
DaemonOgre.create_on_filesystem DaemonOgre::App.log_path,
|
16
|
-
'a+'
|
17
|
-
DaemonOgre.create_on_filesystem DaemonOgre::App.daemon_stderr,
|
18
|
-
'a+'
|
19
|
-
|
20
|
-
|
21
|
-
DaemonOgre::Daemon.start fork,
|
22
|
-
DaemonOgre::App.pid_path,
|
23
|
-
DaemonOgre::App.log_path,
|
24
|
-
DaemonOgre::App.daemon_stderr
|
25
|
-
end
|
26
|
-
|
27
|
-
def debug
|
28
|
-
$DEBUG = true
|
29
|
-
end
|
30
|
-
|
31
|
-
def help
|
32
|
-
puts "\nyou can use one of these commands: "+\
|
33
|
-
"\nstart daemon -d stop restart log -l pid -p port -tcp status reset help"+\
|
34
|
-
"\n==============================================================================DESC======>>"
|
35
|
-
puts "start\t\t\t\t=> this will start the #{$0}"+\
|
36
|
-
"\ndaemon\t\t\t\t=> this will make is daemon process"+\
|
37
|
-
"\nstop\t\t\t\t=> this will kill the last process with pidfile"+\
|
38
|
-
"\nrestart\t\tor reset\t=> hard restart"+\
|
39
|
-
"\nlog\t\tor -l\t\t=> logfile place"+\
|
40
|
-
"\npid\t\tor -p\t\t=> pid file place (if you want set the filename as well, put .pid or .txt in the end)"+\
|
41
|
-
"\nport\t\tor -tcp\t\t=> user definiated port"+\
|
42
|
-
"\nstatus\t\t\t\t=> last process alive?"+\
|
43
|
-
"\ndebug\t\tor -d\t\t=> show debug log (you can make your own by using 'puts 'xy' if $DEBUG' "+\
|
44
|
-
"\nhelp\t\t\t\tgive you this msg :)\n"
|
45
|
-
DaemonOgre::App.terminate=true
|
46
|
-
end
|
47
|
-
|
48
|
-
def start
|
49
|
-
if File.exist?(File.expand_path(App.pid_path))
|
50
|
-
text = File.open(File.expand_path(App.pid_path)).read
|
51
|
-
terminate_on_command = Array.new
|
52
|
-
text.each_line do |line|
|
53
|
-
terminate_on_command.push DaemonOgre.process_running?(line)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
if !terminate_on_command.nil?
|
58
|
-
if !terminate_on_command.include?(true)
|
59
|
-
@@startup = true
|
60
|
-
else
|
61
|
-
puts "sorry but process is already running in this specification"
|
62
|
-
Process.exit
|
63
|
-
end
|
64
|
-
else
|
65
|
-
@@startup = true
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def stop
|
70
|
-
puts "#{$0} is going to be FacePalmed..."
|
71
|
-
Daemon.kill DaemonOgre::App.pid_path
|
72
|
-
kill_with_pid
|
73
|
-
File.open(DaemonOgre::App.pid_path, "w").write("")
|
74
|
-
DaemonOgre::App.terminate=true
|
75
|
-
|
76
|
-
end
|
77
|
-
|
78
|
-
def restart
|
79
|
-
|
80
|
-
Daemon.kill DaemonOgre::App.pid_path
|
81
|
-
kill_with_pid
|
82
|
-
File.open(DaemonOgre::App.pid_path, "w").write("")
|
83
|
-
start
|
84
|
-
|
85
|
-
end
|
86
|
-
|
87
|
-
def kill_with_pid
|
88
|
-
begin
|
89
|
-
if File.exists?(DaemonOgre::App.pid_path)
|
90
|
-
puts "PidFile found, processing..." if $DEBUG
|
91
|
-
File.open(DaemonOgre::App.pid_path).each_line do |row|
|
92
|
-
begin
|
93
|
-
Process.kill 'TERM', row.to_i
|
94
|
-
puts "terminated process at: #{row}" if $DEBUG
|
95
|
-
rescue Exception => ex
|
96
|
-
puts "At process: #{row}, #{ex}" if $DEBUG
|
97
|
-
end
|
98
|
-
end
|
99
|
-
else
|
100
|
-
system "ps -ef | grep #{$0}"
|
101
|
-
#system "netstat --listen"
|
102
|
-
#puts "\nLepton is around 10300-10399"
|
103
|
-
end
|
104
|
-
rescue Exception => ex
|
105
|
-
puts "Exception has occured: #{ex}"
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
def continue?
|
110
|
-
Process.exit if !@@startup
|
111
|
-
end
|
112
|
-
|
113
|
-
def set_log(param)
|
114
|
-
ARGV.each do |one_parameter|
|
115
|
-
if one_parameter == param
|
116
|
-
DaemonOgre::App.log_path= ARGV[ARGV.index(one_parameter)+1]
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
def set_pid(param)
|
122
|
-
ARGV.each do |one_parameter|
|
123
|
-
if one_parameter == param
|
124
|
-
if ARGV[ARGV.index(one_parameter)+1].to_s.include?(".pid") ||\
|
125
|
-
ARGV[ARGV.index(one_parameter)+1].to_s.include?(".txt")
|
126
|
-
DaemonOgre::App.pid_path= ARGV[ARGV.index(one_parameter)+1]
|
127
|
-
else
|
128
|
-
DaemonOgre::App.pid_path= ARGV[ARGV.index(one_parameter)+1].to_s+"lepton.pid"
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
def set_port(param)
|
135
|
-
ARGV.each do |one_parameter|
|
136
|
-
if one_parameter == param
|
137
|
-
DaemonOgre::App.port= ARGV[ARGV.index(one_parameter)+1]
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
def pid
|
143
|
-
begin
|
144
|
-
if !DaemonOgre::App.pid_path.nil?
|
145
|
-
DaemonOgre::App.pid_path+="lepton.pid" if !DaemonOgre::App.pid_path.include?(".pid")
|
146
|
-
pre_path = File.dirname "#{File.dirname(__FILE__)}/#{DaemonOgre::App.pid_path}"
|
147
|
-
pre_path_array = pre_path.split('/') - [ pre_path[0].to_s ]
|
148
|
-
if !Dir.exists?(pre_path)
|
149
|
-
at_now = String.new
|
150
|
-
pre_path_array.each do |one_dir_to_be_made|
|
151
|
-
#at_now show the place where we are now
|
152
|
-
at_now == "" ? at_now += one_dir_to_be_made : at_now += "/"+one_dir_to_be_made
|
153
|
-
if !Dir.exists?("#{File.dirname(__FILE__)}/#{at_now}")
|
154
|
-
Dir.mkdir("#{File.dirname(__FILE__)}/#{at_now}")
|
155
|
-
end
|
156
|
-
end
|
157
|
-
end
|
158
|
-
File.new("#{File.dirname(__FILE__)}/#{DaemonOgre::App.pid_path}", "a+").write Process.pid.to_s+"\n"
|
159
|
-
end
|
160
|
-
rescue Exception => ex
|
161
|
-
ex.logger
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
def pid_check
|
166
|
-
if File.exist?(File.expand_path(App.pid_path))
|
167
|
-
puts "checking pidfile:"
|
168
|
-
text = File.open(File.expand_path(App.pid_path)).read
|
169
|
-
text.each_line do |line|
|
170
|
-
puts "#{line.chomp}:\t#{DaemonOgre.process_running?(line)}"
|
171
|
-
end
|
172
|
-
else
|
173
|
-
puts "missing pid file (with default path) "+\
|
174
|
-
"\nif you specificated one manualy pls type"+\
|
175
|
-
" the path first in with '-p xy/xzt.pid'"
|
176
|
-
end
|
177
|
-
DaemonOgre::App.terminate=true
|
178
|
-
end
|
179
|
-
|
180
|
-
def clear
|
181
|
-
|
182
|
-
logs = ['loh_path','daemon_stderr','exceptions','exlogger']
|
183
|
-
|
184
|
-
logs.each do |logname|
|
185
|
-
begin
|
186
|
-
File.delete DaemonOgre::App.__send__(logname)
|
187
|
-
rescue Exception => ex
|
188
|
-
puts ex if $DEBUG
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
end
|
193
|
-
|
194
|
-
end
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
end
|
199
|
-
end
|
data/lib/daemon-ogre/startup.rb
DELETED
@@ -1,85 +0,0 @@
|
|
1
|
-
module DaemonOgre
|
2
|
-
def self.start(*args)
|
3
|
-
arg = Hash[*args]
|
4
|
-
|
5
|
-
##defaults:
|
6
|
-
#arg[:log_path]
|
7
|
-
#arg[:pid_path]
|
8
|
-
|
9
|
-
#arg[:name]
|
10
|
-
#arg[:port]
|
11
|
-
|
12
|
-
#arg[:terminate]
|
13
|
-
#arg[:clear]
|
14
|
-
|
15
|
-
begin
|
16
|
-
|
17
|
-
begin
|
18
|
-
|
19
|
-
App.pid_path = arg[:pid_path] if !arg[:pid_path].nil?
|
20
|
-
App.log_path = arg[:log_path] if !arg[:log_path].nil?
|
21
|
-
|
22
|
-
$0 = arg[:name] if !arg[:name].nil?
|
23
|
-
App.port = arg[:port] if !arg[:port].nil?
|
24
|
-
|
25
|
-
App.terminate = arg[:terminate] if !arg[:terminate].nil?
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
|
30
|
-
if ARGV.nil?
|
31
|
-
puts "No server task has been given!"
|
32
|
-
DaemonOgre::Server.help
|
33
|
-
end
|
34
|
-
serv_load = Array.new
|
35
|
-
ARGV.each do |one_argv_parameter|
|
36
|
-
case one_argv_parameter.downcase
|
37
|
-
when "start" then serv_load.push "start"
|
38
|
-
when "daemon" then serv_load.push "daemon"
|
39
|
-
when "-d" then DaemonOgre::Server.debug
|
40
|
-
when "stop" then serv_load.push "stop"
|
41
|
-
when "restart" then serv_load.push "restart"
|
42
|
-
when "reset" then serv_load.push "restart"
|
43
|
-
when "debugger" then serv_load.push "debugger"
|
44
|
-
when "log" then DaemonOgre::Server.set_log "log"
|
45
|
-
when "pid" then DaemonOgre::Server.set_pid "pid"
|
46
|
-
when "-l" then DaemonOgre::Server.set_log "-l"
|
47
|
-
when "-p" then DaemonOgre::Server.set_pid "-p"
|
48
|
-
when "port" then DaemonOgre::Server.set_port "port"
|
49
|
-
when "-tcp" then DaemonOgre::Server.set_port "-tcp"
|
50
|
-
when "status" then DaemonOgre::Server.pid_check
|
51
|
-
when "-s" then DaemonOgre::Server.pid_check
|
52
|
-
when "help" then DaemonOgre::Server.help
|
53
|
-
when "debug" then DaemonOgre::Server.debug
|
54
|
-
when "clear" then serv_load.push "clear"
|
55
|
-
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
#server_TODO
|
60
|
-
begin
|
61
|
-
DaemonOgre::Server.restart if serv_load.include? "restart"
|
62
|
-
DaemonOgre::Server.start if serv_load.include? "start"
|
63
|
-
DaemonOgre::Server.stop if serv_load.include? "stop"
|
64
|
-
DaemonOgre::Server.clear if serv_load.include? "clear"
|
65
|
-
DaemonOgre::Server.daemon if serv_load.include? "daemon"
|
66
|
-
end
|
67
|
-
|
68
|
-
#Continue our program ? : )
|
69
|
-
DaemonOgre::Server.continue? if DaemonOgre::App.terminate
|
70
|
-
|
71
|
-
begin
|
72
|
-
require "debugger" ;debugger if serv_load.include? "debugger"
|
73
|
-
rescue Exception => ex
|
74
|
-
puts "you need to install debugger gem => gem install debugger\n#{ex}"
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
78
|
-
end
|
79
|
-
def self.help
|
80
|
-
puts "\n##defaults:\narg[:log_path]\tlog path and"+\
|
81
|
-
" file name\narg[:pid_path]\tpid path and file n"+\
|
82
|
-
"ame\narg[:terminate]\tstart command required to"+\
|
83
|
-
" continue? 'ruby xy.rb start'\narg[:name]\tapplication names as daemon process"
|
84
|
-
end
|
85
|
-
end
|