procemon 1.2.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,38 +0,0 @@
1
- require_relative "../lib/procemon.rb"
2
-
3
- class TestT
4
-
5
- def self.test
6
- puts self
7
- end
8
-
9
- def test string
10
- puts self,string
11
- end
12
-
13
- end
14
-
15
- TestT.inject_instance_method :test do |str|
16
-
17
- puts "hello world! instance "+str
18
-
19
- end
20
-
21
- TestT.inject_singleton_method :test, add: "after" do
22
- puts "hello world! singleton"
23
- end
24
-
25
- puts "---\nafter,singleton case:"
26
- TestT.test
27
-
28
- puts "---\nbefore,instance case:"
29
- TestT.new.test "boogie man"
30
-
31
-
32
- #after,singleton case:
33
- #TestT
34
- #hello world! singleton
35
- #
36
- #before,instance case:
37
- #hello world! instance
38
- ##<TestT:0x00000000d4a008>
@@ -1,3 +0,0 @@
1
- require_relative "../lib/procemon.rb"
2
-
3
- require_relative_directory "foldername_path_from_this_file"
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
@@ -1,30 +0,0 @@
1
- module Kernel
2
-
3
- def sender
4
- sender_properties= Hash.new
5
- # File system
6
- begin
7
- # folder create from caller
8
- begin
9
- folder= caller[0].split(".{rb,ru}:").first.split(File::SEPARATOR)
10
- sender_properties[:file]= folder[(folder.count-1)].split(':')[0]
11
- folder= folder[0..(folder.count-2)]
12
- end
13
- # after formatting
14
- begin
15
-
16
- if !File.directory?(folder.join(File::SEPARATOR))
17
- folder.pop
18
- end
19
- folder= File.join(folder.join(File::SEPARATOR))
20
- if folder != File.expand_path(folder)
21
- folder= File.expand_path(folder)
22
- end
23
-
24
- end
25
- sender_properties[:folder]= folder
26
- end
27
- return sender_properties
28
- end
29
-
30
- end
@@ -1,7 +0,0 @@
1
- begin
2
-
3
- require File.join 'active_support','time'
4
- require File.join 'mpatch','active_support','str2duck'
5
-
6
- rescue LoadError
7
- end
@@ -1,27 +0,0 @@
1
- module Application
2
-
3
- class << self
4
- attr_accessor :config,
5
- :environment,
6
- :tmpdir,
7
- :daemon_stderr,
8
- :log,
9
- :pid,
10
- :name,
11
- :db_init,
12
- :db_drop,
13
- :daemon,
14
- :config_file,
15
- :doc,
16
- :client
17
- end
18
-
19
- self.client ||= Hash.new()
20
- self.config ||= Hash.new()
21
- self.environment ||= String.new()
22
-
23
- self.doc ||= false
24
-
25
- end
26
-
27
- App= Application unless defined?(App)
@@ -1,78 +0,0 @@
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 "--config","-c"
26
- Application.config_file= ARGV[(ARGV.index(one_param)+1)]
27
-
28
- when "--environment","-e"
29
- Application.environment= ARGV[(ARGV.index(one_param)+1)]
30
-
31
- when "--documentation","--generate_documentation","-doc"
32
- Application.doc= true
33
-
34
- when "--db_drop","--drop_database","-dbdrop"
35
- Application.db_drop= true
36
-
37
- when "-log","--log"
38
- Application.log= ARGV[(ARGV.index(one_param)+1)]
39
- Application.daemon_stderr= ARGV[(ARGV.index(one_param)+1)]+"_stderr"
40
-
41
- when "-pid","--pid"
42
- Application.log= ARGV[(ARGV.index(one_param)+1)]
43
-
44
- when "--help","-h","-help","help"
45
- puts "",["This are trigger a documentation generation and than exit the application:",
46
- "--documentation",
47
- "--generate_documentation",
48
- "-doc","",
49
- "This is for set target ENV to the Application by name",
50
- "--environment","-e","",
51
- "this is for target a config file:",
52
- "--config","-c","",
53
- "This is for start application as a forked background process",
54
- "--daemon","-d","",
55
- "This is for drop database data",
56
- "--db_drop","--drop_database","-dbdrop","",
57
- "This is for send init command at start up for database",
58
- "--database_init","-init","",
59
- "This is for start in debug mode",
60
- "--debug","-deb","",
61
- "This is for set pid file location by path",
62
- "-pid","--pid","",
63
- "This is for set log file location by path",
64
- "-log","--log","",
65
- "This is for to stop daemonized application",
66
- "--kill","-kill",""
67
-
68
- ].each{|element| element.include?('--') ? element.gsub!('--',"\t--") : element.gsub!('-',"\t -")}.join("\n"),""
69
- Process.exit!
70
-
71
-
72
- end
73
-
74
- end
75
-
76
- end
77
- end
78
- end
@@ -1,188 +0,0 @@
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
@@ -1,14 +0,0 @@
1
- class String
2
-
3
-
4
-
5
- def find_doc_part(oth_str)
6
- self.each_line do |target_line|
7
- puts target_line[0..(oth_str.length-1)]
8
- if target_line[0..(oth_str.length-1)] == oth_str
9
- return target_line[(oth_str.length)..(target_line.length)]
10
- end
11
- end
12
- end
13
-
14
- end
@@ -1,67 +0,0 @@
1
- class File
2
-
3
- # create a file, if not exsist create file, and dir if needed
4
- def self.create(route_name ,filemod="w",string_data= ::String.new )
5
-
6
- #file_name generate
7
- if !route_name.to_s.split(File::SEPARATOR).last.nil? || route_name.to_s.split(File::SEPARATOR).last != ''
8
- file_name = route_name.to_s.split(File::SEPARATOR).last
9
- else
10
- file_name = nil?
11
- end
12
-
13
- #path_way
14
- begin
15
- raise ::ArgumentError, "missing route_name: #{route_name}" if route_name.nil?
16
- path = File.expand_path(route_name).to_s.split(File::SEPARATOR)
17
- path = path - [File.expand_path(route_name).to_s.split(File::SEPARATOR).last]
18
- path.shift
19
- end
20
-
21
- #job
22
- begin
23
- if !::Dir.exists?(File::SEPARATOR+path.join(File::SEPARATOR))
24
-
25
- at_now = File::SEPARATOR
26
- path.each do |dir_to_be_checked|
27
-
28
- at_now += "#{dir_to_be_checked+File::SEPARATOR}"
29
- ::Dir.mkdir(at_now) if !::Dir.exists?(at_now)
30
-
31
- end
32
- end
33
- end
34
-
35
- # write data
36
- begin
37
- full_path = "#{File::SEPARATOR+path.join(File::SEPARATOR)+File::SEPARATOR}#{file_name}"
38
- if File.exist? full_path
39
- File.open(full_path,filemod).write string_data
40
- else
41
- File.new(full_path,filemod).write string_data
42
- end
43
- end
44
-
45
- end
46
-
47
- # start read the file object on each line
48
- # optionable an integer value to start read line at
49
- # compatible with mac (i am linux user, so not tested)
50
- def each_line_from(start_at=1,&block)
51
- unless [::Integer,Fixnum,Bignum].include?(start_at.class)
52
- raise ::ArgumentError, "invalid line index"
53
- end
54
- begin
55
- line_num= 1
56
- text= self.read
57
- text.gsub!(/\r\n?/, "\n")
58
- text.each_line do |*line|
59
- if line_num >= start_at
60
- block.call #*line
61
- end
62
- line_num += 1
63
- end
64
- end
65
- end
66
-
67
- end