procemon 0.0.1
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/Gemfile +13 -0
- data/Gemfile.lock +72 -0
- data/LICENSE.txt +20 -0
- data/README.md +4 -0
- data/README.rdoc +19 -0
- data/Rakefile +170 -0
- data/VERSION +1 -0
- data/dump/macaddr.rb +95 -0
- data/dump/uuid.rb +324 -0
- data/lib/procemon.rb +55 -0
- data/lib/procemon/function/application.rb +20 -0
- data/lib/procemon/function/argv.rb +84 -0
- data/lib/procemon/function/daemon.rb +188 -0
- data/lib/procemon/function/documentation.rb +10 -0
- data/lib/procemon/function/eval.rb +76 -0
- data/lib/procemon/function/meta/inject_methods.rb +74 -0
- data/lib/procemon/function/name.rb +13 -0
- data/lib/procemon/function/port.rb +35 -0
- data/lib/procemon/function/require.rb +241 -0
- data/lib/procemon/function/str2duck.rb +85 -0
- data/lib/procemon/function/systemu.rb +360 -0
- data/lib/procemon/function/tmp_dir.rb +20 -0
- data/lib/procemon/mpatch/array.rb +59 -0
- data/lib/procemon/mpatch/class.rb +85 -0
- data/lib/procemon/mpatch/exception.rb +0 -0
- data/lib/procemon/mpatch/file.rb +48 -0
- data/lib/procemon/mpatch/hash.rb +82 -0
- data/lib/procemon/mpatch/kernel.rb +9 -0
- data/lib/procemon/mpatch/object.rb +172 -0
- data/lib/procemon/mpatch/process.rb +14 -0
- data/lib/procemon/mpatch/random.rb +43 -0
- data/lib/procemon/mpatch/string.rb +73 -0
- data/lib/procemon/mpatch/yml.rb +11 -0
- data/procemon.gemspec +58 -0
- data/test/test.rb +17 -0
- metadata +86 -0
data/lib/procemon.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
module Procemon
|
3
|
+
|
4
|
+
# load up helpers
|
5
|
+
|
6
|
+
Dir.glob(\
|
7
|
+
File.join(\
|
8
|
+
File.dirname(__FILE__),\
|
9
|
+
__FILE__.to_s.split(File::Separator).last.split('.')[0],
|
10
|
+
'**',"*.{rb,ru}"\
|
11
|
+
)\
|
12
|
+
).uniq.sort.each do |one_helper_file|
|
13
|
+
load one_helper_file
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
def self.init_all
|
18
|
+
|
19
|
+
# process the ARGV parameters
|
20
|
+
process_parameters
|
21
|
+
|
22
|
+
# project name
|
23
|
+
set_app_name_by_root_folder
|
24
|
+
|
25
|
+
# init temporarily directory
|
26
|
+
tmpdir_init
|
27
|
+
|
28
|
+
# create config singleton
|
29
|
+
generate_config
|
30
|
+
|
31
|
+
# load meta-s
|
32
|
+
meta_load
|
33
|
+
|
34
|
+
# mount libs
|
35
|
+
mount_libs
|
36
|
+
|
37
|
+
# mount offline modules
|
38
|
+
mount_modules
|
39
|
+
|
40
|
+
# garbage collect
|
41
|
+
ObjectSpace.garbage_collect
|
42
|
+
|
43
|
+
# documentation generate
|
44
|
+
generate_documentation(Application.create_documentation)
|
45
|
+
|
46
|
+
# Daemonize
|
47
|
+
Daemon.init
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
### Load the requirements in to the general Module
|
52
|
+
#load File.expand_path(File.join(File.dirname(__FILE__),'procemon'
|
53
|
+
|
54
|
+
|
55
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Application
|
2
|
+
class << self
|
3
|
+
attr_accessor :config,
|
4
|
+
:environment,
|
5
|
+
:tmpdir,
|
6
|
+
:daemon_stderr,
|
7
|
+
:log,
|
8
|
+
:pid,
|
9
|
+
:name,
|
10
|
+
:db_init,
|
11
|
+
:db_drop,
|
12
|
+
:daemon,
|
13
|
+
:config_file,
|
14
|
+
:create_documentation
|
15
|
+
end
|
16
|
+
self.config= Hash.new()
|
17
|
+
self.environment= String.new()
|
18
|
+
end
|
19
|
+
|
20
|
+
App= Application unless defined?(App)
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module Procemon
|
2
|
+
class << self
|
3
|
+
|
4
|
+
|
5
|
+
def process_parameters
|
6
|
+
ARGV.each do |one_param|
|
7
|
+
|
8
|
+
case one_param.downcase
|
9
|
+
|
10
|
+
when "--debug","-deb"
|
11
|
+
$DEBUG= true
|
12
|
+
|
13
|
+
when "--test","-test"
|
14
|
+
$TEST= true
|
15
|
+
|
16
|
+
when "--database_init","-init"
|
17
|
+
Application.db_init= true
|
18
|
+
|
19
|
+
when "--daemon","-d"
|
20
|
+
Application.daemon= "start"
|
21
|
+
|
22
|
+
when "--kill","-kill"
|
23
|
+
Application.daemon= "stop"
|
24
|
+
|
25
|
+
when "--debuger","-bugger"
|
26
|
+
require "debugger"
|
27
|
+
debugger
|
28
|
+
|
29
|
+
when "--config","-c"
|
30
|
+
Application.config_file= ARGV[(ARGV.index(one_param)+1)]
|
31
|
+
|
32
|
+
when "--environment","-e"
|
33
|
+
Application.environment= ARGV[(ARGV.index(one_param)+1)]
|
34
|
+
|
35
|
+
when "--documentation","--generate_documentation","-doc"
|
36
|
+
Application.create_documentation= true
|
37
|
+
|
38
|
+
when "--db_drop","--drop_database","-dbdrop"
|
39
|
+
Application.db_drop= true
|
40
|
+
|
41
|
+
when "-log","--log"
|
42
|
+
Application.log= ARGV[(ARGV.index(one_param)+1)]
|
43
|
+
Application.daemon_stderr= ARGV[(ARGV.index(one_param)+1)]+"_stderr"
|
44
|
+
|
45
|
+
when "-pid","--pid"
|
46
|
+
Application.log= ARGV[(ARGV.index(one_param)+1)]
|
47
|
+
|
48
|
+
when "--help","-h","-help","help"
|
49
|
+
puts "",["This are trigger a documentation generation and than exit the application:",
|
50
|
+
"--documentation",
|
51
|
+
"--generate_documentation",
|
52
|
+
"-doc","",
|
53
|
+
"This is for set target ENV to the Application by name",
|
54
|
+
"--environment","-e","",
|
55
|
+
"this is for target a config file:",
|
56
|
+
"--config","-c","",
|
57
|
+
"This is for use debugger (development tool)",
|
58
|
+
"--debug","-bug","",
|
59
|
+
"This is for start application as a forked background process",
|
60
|
+
"--daemon","-d","",
|
61
|
+
"This is for drop database data",
|
62
|
+
"--db_drop","--drop_database","-dbdrop","",
|
63
|
+
"This is for send init command at start up for database",
|
64
|
+
"--database_init","-init","",
|
65
|
+
"This is for start in debug mode",
|
66
|
+
"--debug","-deb","",
|
67
|
+
"This is for set pid file location by path",
|
68
|
+
"-pid","--pid","",
|
69
|
+
"This is for set log file location by path",
|
70
|
+
"-log","--log","",
|
71
|
+
"This is for to stop daemonized application",
|
72
|
+
"--kill","-kill",""
|
73
|
+
|
74
|
+
].each{|element| element.include?('--') ? element.gsub!('--',"\t--") : element.gsub!('-',"\t -")}.join("\n"),""
|
75
|
+
Process.exit!
|
76
|
+
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,188 @@
|
|
1
|
+
# use Daemon.daemonize or Process.daemonize
|
2
|
+
class Daemon
|
3
|
+
|
4
|
+
# Checks to see if the current process is the child process and if not
|
5
|
+
# will update the pid file with the child pid.
|
6
|
+
def self.start pid, pidfile, outfile, errfile
|
7
|
+
unless pid.nil?
|
8
|
+
raise "Fork failed" if pid == -1
|
9
|
+
write pid, pidfile #if kill pidfile
|
10
|
+
exit
|
11
|
+
else
|
12
|
+
redirect outfile, errfile
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Attempts to write the pid of the forked process to the pid file.
|
17
|
+
def self.write pid, pidfile
|
18
|
+
File.create(pidfile,"w","") if File.exists?(Application.pid)
|
19
|
+
File.open pidfile, "a+" do |new_line|
|
20
|
+
new_line.write "#{pid}\n"
|
21
|
+
end
|
22
|
+
rescue ::Exception => e
|
23
|
+
$stderr.puts "While writing the PID to file, unexpected #{e.class}: #{e}"
|
24
|
+
Process.kill "HUP", pid
|
25
|
+
end
|
26
|
+
|
27
|
+
# Try and read the existing pid from the pid file and signal the
|
28
|
+
# process. Returns true for a non blocking status.
|
29
|
+
def self.kill(pidfile)
|
30
|
+
opid = File.open(File.join".",pidfile).read.strip.to_i
|
31
|
+
Process.kill 'HUP', opid.to_i
|
32
|
+
true
|
33
|
+
rescue Errno::ENOENT
|
34
|
+
$stdout.puts "#{pidfile} did not exist: Errno::ENOENT" if $DEBUG
|
35
|
+
true
|
36
|
+
rescue Errno::ESRCH
|
37
|
+
$stdout.puts "The process #{opid} did not exist: Errno::ESRCH" if $DEBUG
|
38
|
+
true
|
39
|
+
rescue Errno::EPERM
|
40
|
+
$stderr.puts "Lack of privileges to manage the process #{opid}: Errno::EPERM" if $DEBUG
|
41
|
+
false
|
42
|
+
rescue ::Exception => e
|
43
|
+
$stderr.puts "While signaling the PID, unexpected #{e.class}: #{e}" if $DEBUG
|
44
|
+
false
|
45
|
+
end
|
46
|
+
|
47
|
+
# Send stdout and stderr to log files for the child process
|
48
|
+
def self.redirect outfile, errfile
|
49
|
+
$stdin.reopen File.join('','dev','null')
|
50
|
+
out = File.new outfile, "a"
|
51
|
+
err = File.new errfile, "a"
|
52
|
+
$stdout.reopen out
|
53
|
+
$stderr.reopen err
|
54
|
+
$stdout.sync = $stderr.sync = true
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.daemonize
|
58
|
+
|
59
|
+
pid_path= nil
|
60
|
+
log_path= nil
|
61
|
+
daemon_stderr= nil
|
62
|
+
|
63
|
+
begin
|
64
|
+
File.create Application.pid,'a+'
|
65
|
+
File.create Application.log,'a+'
|
66
|
+
File.create Application.daemon_stderr,'a+'
|
67
|
+
|
68
|
+
pid_path= Application.pid
|
69
|
+
log_path= Application.log
|
70
|
+
daemon_stderr= Application.daemon_stderr
|
71
|
+
rescue Exception
|
72
|
+
File.create File.join(Dir.pwd, "pid", "pidfile" ),'a+'
|
73
|
+
File.create File.join(Dir.pwd, "log", "logfile" ),'a+'
|
74
|
+
File.create File.join(Dir.pwd, "log", "daemon_stderr" ),'a+'
|
75
|
+
|
76
|
+
pid_path= File.join(Dir.pwd, "pid", "pidfile" )
|
77
|
+
log_path= File.join(Dir.pwd, "log", "logfile" )
|
78
|
+
daemon_stderr= File.join(Dir.pwd, "log", "daemon_stderr" )
|
79
|
+
end
|
80
|
+
|
81
|
+
Daemon.start(fork,pid_path,log_path,daemon_stderr)
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.kill_with_pid
|
86
|
+
begin
|
87
|
+
if File.exists?(Application.pid)
|
88
|
+
puts "PidFile found, processing..." if $DEBUG
|
89
|
+
File.open(Application.pid).each_line do |row|
|
90
|
+
begin
|
91
|
+
Process.kill 'TERM', row.chomp.to_i
|
92
|
+
puts "terminated process at: #{row.chomp}" if $DEBUG
|
93
|
+
rescue Exception => ex
|
94
|
+
puts "At process: #{row.chomp}, #{ex}" if $DEBUG
|
95
|
+
end
|
96
|
+
end
|
97
|
+
else
|
98
|
+
system "ps -ef | grep #{$0}"
|
99
|
+
#system "netstat --listen"
|
100
|
+
#puts "\nLepton is around 10300-10399"
|
101
|
+
end
|
102
|
+
rescue Exception => ex
|
103
|
+
puts "Exception has occured: #{ex}"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def self.terminate
|
108
|
+
Daemon.kill Application.pid
|
109
|
+
Daemon.kill_with_pid
|
110
|
+
end
|
111
|
+
|
112
|
+
def self.kill_by_name(*args)
|
113
|
+
|
114
|
+
target_name= nil
|
115
|
+
debug_mod = false
|
116
|
+
|
117
|
+
args.each do |one_element|
|
118
|
+
if one_element.class == String
|
119
|
+
target_name = one_element
|
120
|
+
elsif one_element.class == TrueClass || one_element.class == FalseClass
|
121
|
+
debug_mod = one_element
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
# name switch
|
126
|
+
begin
|
127
|
+
target_name ||= $0
|
128
|
+
$0 = "ruby_tmp_process"
|
129
|
+
end
|
130
|
+
|
131
|
+
start_time= Time.now
|
132
|
+
while `ps aux | grep #{target_name}`.split(' ')[1] != "" ||(Time.now - start_time) < 6
|
133
|
+
|
134
|
+
begin
|
135
|
+
|
136
|
+
Process.kill "TERM",`ps aux | grep #{target_name}`.split(' ')[1].to_i
|
137
|
+
|
138
|
+
rescue Errno::ESRCH
|
139
|
+
$stdout.puts "The process #{target_name} did not exist: Errno::ESRCH" if debug_mod
|
140
|
+
break
|
141
|
+
rescue Errno::EPERM
|
142
|
+
$stderr.puts "Lack of privileges to manage the process #{target_name}: Errno::EPERM" if debug_mod
|
143
|
+
break
|
144
|
+
rescue ::Exception => e
|
145
|
+
$stderr.puts "While signaling the PID, unexpected #{e.class}: #{e}" if debug_mod
|
146
|
+
break
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
|
151
|
+
# name switch back
|
152
|
+
begin
|
153
|
+
$0 = target_name
|
154
|
+
end
|
155
|
+
|
156
|
+
return nil
|
157
|
+
end
|
158
|
+
|
159
|
+
def self.stop
|
160
|
+
|
161
|
+
# kill methods
|
162
|
+
begin
|
163
|
+
self.kill_by_name true
|
164
|
+
self.terminate
|
165
|
+
end
|
166
|
+
|
167
|
+
pid_path= nil
|
168
|
+
begin
|
169
|
+
pid_path= Application.pid
|
170
|
+
rescue Exception
|
171
|
+
pid_path= File.join(Dir.pwd, "pid", "pidfile" )
|
172
|
+
end
|
173
|
+
|
174
|
+
File.open(pid_path, "w").write("")
|
175
|
+
Process.exit!
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
def self.init
|
180
|
+
case Application.daemon.to_s.downcase
|
181
|
+
when "true","start"
|
182
|
+
self.daemonize
|
183
|
+
when "stop"
|
184
|
+
self.stop
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module Kernel
|
2
|
+
|
3
|
+
# safe_eval(string [, binding [, filename [,lineno]]] *allowed_class/module_names ) -> obj
|
4
|
+
#
|
5
|
+
# Evaluates the Ruby expression(s) in <em>string</em>. If
|
6
|
+
# <em>binding</em> is given, which must be a <code>Binding</code>
|
7
|
+
# object, the evaluation is performed in its context. If the
|
8
|
+
# optional <em>filename</em> and <em>lineno</em> parameters are
|
9
|
+
# present, they will be used when reporting syntax errors.
|
10
|
+
#
|
11
|
+
# def get_binding(str)
|
12
|
+
# return binding
|
13
|
+
# end
|
14
|
+
# str = "hello"
|
15
|
+
# safe_eval "str + ' Fred'" ,Kernel #=> "hello Fred"
|
16
|
+
# safe_eval "str + ' Fred'", get_binding("bye") ,Kernel #=> "bye Fred"
|
17
|
+
def safe_eval(*args)
|
18
|
+
|
19
|
+
# defaults
|
20
|
+
begin
|
21
|
+
allowed= Array.new
|
22
|
+
eval_exception= String.new
|
23
|
+
tmp_array= nil
|
24
|
+
end
|
25
|
+
|
26
|
+
# separate allowed names
|
27
|
+
begin
|
28
|
+
tmp_array= Array.new
|
29
|
+
args.each do |argument|
|
30
|
+
case argument.class.to_s.downcase
|
31
|
+
|
32
|
+
when "class","module"
|
33
|
+
begin
|
34
|
+
allowed.push argument
|
35
|
+
end
|
36
|
+
|
37
|
+
else
|
38
|
+
begin
|
39
|
+
tmp_array.push argument
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
args= tmp_array
|
45
|
+
end
|
46
|
+
|
47
|
+
# build exception list to eval
|
48
|
+
begin
|
49
|
+
allowed.each do |one_name|
|
50
|
+
eval_exception += "|"+one_name.to_s
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# trim un wanted elements from string
|
55
|
+
begin
|
56
|
+
args.each do |argument|
|
57
|
+
case argument.class.to_s.downcase
|
58
|
+
|
59
|
+
when "string"
|
60
|
+
begin
|
61
|
+
#TODO new regex! # /(\b|\.|\{|\>)[A-Z]\w*/
|
62
|
+
#args[args.index(argument)]= argument.gsub( /(\b|\.|\{|\>)[A-Z]\w*/ ,'')
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# do save eval
|
70
|
+
begin
|
71
|
+
eval(*args)
|
72
|
+
end
|
73
|
+
|
74
|
+
end if $DEBUG == true
|
75
|
+
|
76
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
class Class
|
2
|
+
|
3
|
+
# this will inject a code block to a target instance method
|
4
|
+
# by default the before or after sym is not required
|
5
|
+
# default => before
|
6
|
+
#
|
7
|
+
# Test.inject_singleton_method :hello do |*args|
|
8
|
+
# puts "singleton extra, so #{args[0]}"
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
def inject_singleton_method(method,options=:before,&block)
|
12
|
+
|
13
|
+
original_method= self.method(method).clone
|
14
|
+
#Singleton.methods[self.object_id]= self.method(method)
|
15
|
+
self.singleton_class.__send__ :undef_method, method
|
16
|
+
self.class_eval do
|
17
|
+
define_singleton_method method do |*arguments|
|
18
|
+
case true
|
19
|
+
|
20
|
+
when options == :before
|
21
|
+
begin
|
22
|
+
block.call *arguments
|
23
|
+
original_method.call *arguments
|
24
|
+
end
|
25
|
+
|
26
|
+
when options == :after
|
27
|
+
begin
|
28
|
+
original_method.call *arguments
|
29
|
+
block.call *arguments
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
# this will inject a code block to a target singleton method
|
39
|
+
# by default the before or after sym is not required
|
40
|
+
# default => before
|
41
|
+
#
|
42
|
+
# Test.inject_instance_method :hello, :before do |*args|
|
43
|
+
# puts "singleton on a instance method and "+args[0]
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
def inject_instance_method(method,options=:before,&block)
|
47
|
+
self.class_eval do
|
48
|
+
alias_method :"old_#{method.to_s}", method
|
49
|
+
end
|
50
|
+
extended_process = Proc.new do |*args|
|
51
|
+
|
52
|
+
case true
|
53
|
+
|
54
|
+
when options == :before
|
55
|
+
begin
|
56
|
+
block.call *args
|
57
|
+
self.__send__ :"old_#{method.to_s}", *args
|
58
|
+
end
|
59
|
+
|
60
|
+
when options == :after
|
61
|
+
begin
|
62
|
+
self.__send__ :"old_#{method.to_s}", *args
|
63
|
+
block.call *args
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
self.class_eval do
|
70
|
+
define_method method, extended_process#Proc call
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|