daemon-ogre 1.4.9 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/Gemfile +1 -8
- data/README.md +7 -8
- data/Rakefile +1 -52
- data/VERSION +1 -1
- data/daemon-ogre.gemspec +21 -55
- data/example/sample_daemon_app.rb +19 -0
- data/files.rb +24 -0
- data/lib/daemon-ogre/app.rb +40 -0
- data/lib/daemon-ogre/daemon.rb +65 -0
- data/lib/daemon-ogre/mpatch.rb +93 -0
- data/lib/daemon-ogre/server.rb +199 -0
- data/lib/daemon-ogre/startup.rb +85 -0
- data/lib/daemon-ogre/support.rb +246 -0
- data/lib/daemon-ogre.rb +6 -711
- data/test/test_daemon-ogre.rb +0 -7
- metadata +20 -80
- data/.idea/.name +0 -1
- data/.idea/.rakeTasks +0 -7
- data/.idea/daemon-ogre.iml +0 -18
- data/.idea/encodings.xml +0 -5
- data/.idea/misc.xml +0 -24
- data/.idea/modules.xml +0 -9
- data/.idea/scopes/scope_settings.xml +0 -5
- data/.idea/vcs.xml +0 -7
- data/.idea/workspace.xml +0 -434
@@ -0,0 +1,85 @@
|
|
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
|
@@ -0,0 +1,246 @@
|
|
1
|
+
#DaemonOgreBody
|
2
|
+
begin
|
3
|
+
module DaemonOgre
|
4
|
+
|
5
|
+
#OgreClassMethods
|
6
|
+
begin
|
7
|
+
class << self
|
8
|
+
#Based on the rb location
|
9
|
+
def load_directory(directory,*args)
|
10
|
+
arg = Hash[*args]
|
11
|
+
|
12
|
+
#directory = File.expand_path(directory)
|
13
|
+
delayed_loads = Array.new
|
14
|
+
|
15
|
+
if !arg[:delayed].nil?
|
16
|
+
raise ArgumentError, "Delayed items must be in an "+\
|
17
|
+
"Array! Example:\n:delayed => ['abc']" if arg[:delayed].class != Array
|
18
|
+
end
|
19
|
+
|
20
|
+
if !arg[:excluded].nil?
|
21
|
+
raise ArgumentError, "Exclude items must be in an "+\
|
22
|
+
"Array! Example:\n:exclude => ['abc']" if arg[:excluded].class != Array
|
23
|
+
end
|
24
|
+
|
25
|
+
arg[:type]= "rb" if arg[:type].nil?
|
26
|
+
arg[:monkey_patch]= 0 if arg[:monkey_patch].nil?
|
27
|
+
|
28
|
+
#=============================================================================================================
|
29
|
+
|
30
|
+
### GET Pre path + validation
|
31
|
+
begin
|
32
|
+
#get method callers path
|
33
|
+
|
34
|
+
pre_path = caller[arg[:monkey_patch].to_i].split('.rb:').first+('.rb')
|
35
|
+
|
36
|
+
if !pre_path.include?('/') && !pre_path.include?('\\')
|
37
|
+
pre_path = File.expand_path(pre_path)
|
38
|
+
end
|
39
|
+
|
40
|
+
separator_symbol= String.new
|
41
|
+
pre_path.include?('/') ? separator_symbol = '/' : separator_symbol = '\\'
|
42
|
+
|
43
|
+
pre_path= ((pre_path.split(separator_symbol))-([pre_path.split(separator_symbol).pop])).join(separator_symbol)
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
puts "prepath: "+pre_path.inspect if $DEBUG
|
48
|
+
|
49
|
+
puts "LOADING_FILES_FROM_"+directory.to_s.split(separator_symbol).last.split('.').first.capitalize if $DEBUG
|
50
|
+
|
51
|
+
puts "Elements found in #{directory}" if $DEBUG
|
52
|
+
puts File.join("#{pre_path}","#{directory}","**","*.#{arg[:type]}") if $DEBUG
|
53
|
+
puts Dir[File.join("#{pre_path}","#{directory}","**","*.#{arg[:type]}")].sort.inspect if $DEBUG
|
54
|
+
|
55
|
+
Dir[File.join("#{pre_path}","#{directory}","**","*.#{arg[:type]}")].sort.each do |file|
|
56
|
+
|
57
|
+
arg[:delayed]= [nil] if arg[:delayed].nil?
|
58
|
+
arg[:excluded]= [nil] if arg[:excluded].nil?
|
59
|
+
|
60
|
+
arg[:excluded].each do |except|
|
61
|
+
if file.split(separator_symbol).last.split('.').first == except.to_s.split('.').first
|
62
|
+
|
63
|
+
puts file.to_s + " cant be loaded because it's an exception" if $DEBUG
|
64
|
+
|
65
|
+
else
|
66
|
+
|
67
|
+
arg[:delayed].each do |delay|
|
68
|
+
|
69
|
+
if file.split(separator_symbol).last.split('.').first == delay.to_s.split('.').first
|
70
|
+
delayed_loads.push(file)
|
71
|
+
else
|
72
|
+
load(file)
|
73
|
+
puts file.to_s if $DEBUG
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
delayed_loads.each do |delayed_load_element|
|
82
|
+
load(delayed_load_element)
|
83
|
+
puts delayed_load_element.to_s if $DEBUG
|
84
|
+
end
|
85
|
+
puts "DONE_LOAD_FILES_FROM_"+directory.to_s.split(separator_symbol).last.split('.').first.capitalize if $DEBUG
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
def get_port(port,max_port=65535 ,host="0.0.0.0")
|
90
|
+
|
91
|
+
require 'socket'
|
92
|
+
port = port.to_i
|
93
|
+
|
94
|
+
begin
|
95
|
+
server = TCPServer.new('0.0.0.0', port)
|
96
|
+
server.close
|
97
|
+
return port
|
98
|
+
rescue Errno::EADDRINUSE
|
99
|
+
port = port.to_i + 1 if port < max_port+1
|
100
|
+
retry
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
def error_logger(error_msg,prefix="",log_file=App.log_path)
|
106
|
+
|
107
|
+
###convert error msg to more human friendly one
|
108
|
+
begin
|
109
|
+
error_msg= error_msg.to_s.gsub('", "','",'+"\n\"")
|
110
|
+
rescue Exception
|
111
|
+
error_msg= error_msg.inspect.gsub('", "','",'+"\n\"")
|
112
|
+
end
|
113
|
+
|
114
|
+
if File.exists?(File.expand_path(log_file))
|
115
|
+
error_log = File.open( File.expand_path(log_file), "a+")
|
116
|
+
error_log << "\n#{Time.now} | #{prefix}#{":" if prefix != ""} #{error_msg}"
|
117
|
+
error_log.close
|
118
|
+
else
|
119
|
+
File.new(File.expand_path(log_file), "w").write(
|
120
|
+
"#{Time.now} | #{prefix}#{":" if prefix != ""} #{error_msg}"
|
121
|
+
)
|
122
|
+
end
|
123
|
+
|
124
|
+
return {:error => error_msg}
|
125
|
+
end
|
126
|
+
|
127
|
+
def load_ymls(directory,*args)
|
128
|
+
arg= Hash[*args]
|
129
|
+
|
130
|
+
require 'yaml'
|
131
|
+
#require "hashie"
|
132
|
+
|
133
|
+
arg[:monkey_patch]= 0 if arg[:monkey_patch].nil?
|
134
|
+
|
135
|
+
|
136
|
+
### GET Pre path + validation
|
137
|
+
begin
|
138
|
+
#get method callers path
|
139
|
+
pre_path = caller[arg[:monkey_patch].to_i].split('.rb:').first+('.rb')
|
140
|
+
if !pre_path.include?('/') && !pre_path.include?('\\')
|
141
|
+
pre_path = File.expand_path(pre_path)
|
142
|
+
end
|
143
|
+
separator_symbol= String.new
|
144
|
+
pre_path.include?('/') ? separator_symbol = '/' : separator_symbol = '\\'
|
145
|
+
pre_path= ((pre_path.split(separator_symbol))-([pre_path.split(separator_symbol).pop])).join(separator_symbol)
|
146
|
+
end
|
147
|
+
|
148
|
+
puts "Elements found in #{directory}" if $DEBUG
|
149
|
+
puts File.join("#{pre_path}","#{directory}","**","*.yml") if $DEBUG
|
150
|
+
puts Dir[File.join("#{pre_path}","#{directory}","**","*.yml")].sort.inspect if $DEBUG
|
151
|
+
|
152
|
+
yaml_files = Dir[File.join("#{pre_path}","#{directory}","**","*.yml")].sort
|
153
|
+
|
154
|
+
puts "\nyaml file found: "+yaml_files.inspect if $DEBUG
|
155
|
+
|
156
|
+
@result_hash = {}
|
157
|
+
yaml_files.each_with_index do |full_path_file_name|
|
158
|
+
|
159
|
+
|
160
|
+
file_name = full_path_file_name.split(separator_symbol).last.split(separator_symbol).first
|
161
|
+
|
162
|
+
hash_key = file_name
|
163
|
+
@result_hash[hash_key] = YAML.load(File.read("#{full_path_file_name}"))
|
164
|
+
|
165
|
+
#@result_hash = @result_hash.merge!(tmp_hash)
|
166
|
+
|
167
|
+
|
168
|
+
puts "==========================================================" if $DEBUG
|
169
|
+
puts "Loading "+file_name.to_s.capitalize+"\n" if $DEBUG
|
170
|
+
puts YAML.load(File.read("#{full_path_file_name}")) if $DEBUG
|
171
|
+
puts "__________________________________________________________" if $DEBUG
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
puts "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-" if $DEBUG
|
176
|
+
puts "The Main Hash: \n"+@result_hash.inspect.to_s if $DEBUG
|
177
|
+
puts "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n" if $DEBUG
|
178
|
+
|
179
|
+
return @result_hash
|
180
|
+
end
|
181
|
+
|
182
|
+
def create_on_filesystem(route_name,optionable_file_mod="w",optionable_data=nil)
|
183
|
+
begin
|
184
|
+
|
185
|
+
#file_name generate
|
186
|
+
if !route_name.to_s.split('/').last.nil? || route_name.to_s.split('/').last != ''
|
187
|
+
file_name = route_name.to_s.split('/').last
|
188
|
+
else
|
189
|
+
file_name = nil?
|
190
|
+
end
|
191
|
+
|
192
|
+
#path_way
|
193
|
+
begin
|
194
|
+
raise ArgumentError, "missing route_name: #{route_name}" if route_name.nil?
|
195
|
+
path = File.expand_path(route_name).to_s.split('/')
|
196
|
+
path = path - [File.expand_path(route_name).to_s.split('/').last]
|
197
|
+
path.shift
|
198
|
+
end
|
199
|
+
|
200
|
+
#job
|
201
|
+
begin
|
202
|
+
if !Dir.exists?('/'+path.join('/'))
|
203
|
+
|
204
|
+
at_now = '/'
|
205
|
+
path.each do |dir_to_be_checked|
|
206
|
+
|
207
|
+
at_now += "#{dir_to_be_checked}/"
|
208
|
+
Dir.mkdir(at_now) if !Dir.exists?(at_now)
|
209
|
+
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
#file_create
|
215
|
+
File.new("/#{path.join('/')}/#{file_name}", optionable_file_mod ).write optionable_data
|
216
|
+
|
217
|
+
rescue Exception => ex
|
218
|
+
puts ex
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
def process_running?(input)
|
223
|
+
begin
|
224
|
+
Process.getpgid input.chomp.to_i
|
225
|
+
return true
|
226
|
+
rescue Exception
|
227
|
+
return false
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
def clone_mpath(original_path,new_name)
|
232
|
+
log_path = File.expand_path(original_path)
|
233
|
+
log_path = log_path.split('/')
|
234
|
+
log_path.pop
|
235
|
+
log_path.push(new_name)
|
236
|
+
log_path = log_path.join('/')
|
237
|
+
|
238
|
+
return log_path
|
239
|
+
end
|
240
|
+
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
|
245
|
+
end
|
246
|
+
end
|