Module: ControlHelper
Instance Method Summary collapse
- #app_not_running?(options) ⇒ Boolean
- #app_running?(pid) ⇒ Boolean
- #check_for_success_in_starting_new_process!(options) ⇒ Object
- #delete_file(file_name) ⇒ Object
- #exit_if_not_running!(options) ⇒ Object
- #exit_if_old_process_is_already_running!(options) ⇒ Object
- #find_app_pid(options) ⇒ Object
- #find_pid_with_ps(search_by_string) ⇒ Object
- #get_pid_from_file(pid_filename) ⇒ Object
- #http_server?(options) ⇒ Boolean
- #is_pid_alive?(process_id) ⇒ Boolean
- #kill_the_old_process_if_needed(options) ⇒ Object
- #kill_the_process!(options) ⇒ Object
- #kill_with_retries!(options) ⇒ Object
- #make_sure_pid_is_real!(pid, pid_filename) ⇒ Object
- #print_workers_and_delete_files!(type, extension) ⇒ Object
-
#print_workers_started_and_stopped(options) ⇒ Object
TODO add option to overwrite the file names.
-
#restart_the_app!(options) ⇒ Object
Note , only valid for http servers.
- #retrieve_search_string(options) ⇒ Object
- #skip_workers_message?(options) ⇒ Boolean
- #start_a_new_process!(options) ⇒ Object
Instance Method Details
#app_not_running?(options) ⇒ Boolean
129 130 131 132 133 134 135 136 137 |
# File 'lib/control_helper.rb', line 129 def app_not_running?() pid_filename = .fetch(Control_P::OPTIONS_ATTRIBUTES[:pid_filename], nil) raise "no pid filename found in #{}" if pid_filename.nil? p "working directory is #{Dir.pwd} , trying to cat #{pid_filename} , of type #{pid_filename.class}" res = get_pid_from_file(pid_filename) p "result from get_pid_from_file is #{res} , type #{res.class}" res = make_sure_pid_is_real!(res, pid_filename) unless res.nil? res.nil? end |
#app_running?(pid) ⇒ Boolean
73 74 75 |
# File 'lib/control_helper.rb', line 73 def app_running?(pid) !pid.nil? end |
#check_for_success_in_starting_new_process!(options) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/control_helper.rb', line 175 def check_for_success_in_starting_new_process!() prefix = "#{Control_P::HOSTNAME}" pid = find_app_pid() if pid p "#{prefix} Ok, Restarted. new pid #{pid}" if http_server?() && !() if Dir[Control_P::WORKERS_STARTED_EXTENSION].length < 1 p 'no workers has seemed to be started, check it out.' exit(1) end print_workers_started_and_stopped() end exit(0) else p "#{prefix} problem restarting. Check your code. #{pid}" exit(1) end end |
#delete_file(file_name) ⇒ Object
151 152 153 154 155 |
# File 'lib/control_helper.rb', line 151 def delete_file(file_name) res = File.delete(file_name) rescue => e p "failed to delete #{file_name} , #{e.inspect}" end |
#exit_if_not_running!(options) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/control_helper.rb', line 61 def exit_if_not_running!() old_pid = find_app_pid() unless app_running?(old_pid) app_name = .fetch(Control_P::OPTIONS_ATTRIBUTES[:app_name], '') p "app #{app_name} is already NOT running" exit(1) end old_pid end |
#exit_if_old_process_is_already_running!(options) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/control_helper.rb', line 93 def exit_if_old_process_is_already_running!() p 'checking if there\'s a running process' old_pid = find_app_pid() if old_pid app_name = .fetch(Control_P::OPTIONS_ATTRIBUTES[:app_name], '') p "#{app_name} is already running. old_pid is #{old_pid} exiting" exit(1) end end |
#find_app_pid(options) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/control_helper.rb', line 4 def find_app_pid() pid_filename = .fetch(Control_P::OPTIONS_ATTRIBUTES[:pid_filename], nil) if pid_filename get_pid_from_file(pid_filename) else search_by_string = retrieve_search_string() find_pid_with_ps(search_by_string) end end |
#find_pid_with_ps(search_by_string) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/control_helper.rb', line 33 def find_pid_with_ps(search_by_string) search_string = "ps aux | grep #{search_by_string} | grep -v grep" pid = nil p "searching by #{search_string}" procs = `#{search_string}` # todo add grep procs.each_line do |proc| if proc.include?(search_by_string) p "found #{proc}" res = proc.split(' ') old_pid = res[1] return old_pid.to_i end end pid end |
#get_pid_from_file(pid_filename) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/control_helper.rb', line 25 def get_pid_from_file(pid_filename) return nil unless File.exists?(pid_filename) File.open(pid_filename, &:readline).strip rescue return nil end |
#http_server?(options) ⇒ Boolean
210 211 212 |
# File 'lib/control_helper.rb', line 210 def http_server?() true == .fetch(Control_P::OPTIONS_ATTRIBUTES[:http_server], false) end |
#is_pid_alive?(process_id) ⇒ Boolean
52 53 54 55 56 57 58 59 |
# File 'lib/control_helper.rb', line 52 def is_pid_alive?(process_id) search_string = "ps -p #{process_id} -o comm=" p "searching by #{search_string}" procs = `#{search_string}` # todo add grep p "found procs #{procs}" procs != '' end |
#kill_the_old_process_if_needed(options) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/control_helper.rb', line 214 def kill_the_old_process_if_needed() old_pid = find_app_pid() if app_running?(old_pid) # TODO should wait ?? kill_with_retries!() else app_name = .fetch(Control_P::OPTIONS_ATTRIBUTES[:app_name], '') p "There's no app up to restart (#{app_name}), Trying to start a new one.." true end end |
#kill_the_process!(options) ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/control_helper.rb', line 103 def kill_the_process!() kill_command = .fetch(Control_P::OPTIONS_ATTRIBUTES[:kill_command]) `#{kill_command}` false rescue Errno::ESRCH p 'no such process returning true for kill_the_process!' true rescue => e p "error in killing process #{e.inspect}" end |
#kill_with_retries!(options) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/control_helper.rb', line 157 def kill_with_retries!() num_tries = 4 (1..num_tries).to_a.each do |try| res = kill_the_process!() return true if res sleep 5 old_pid = find_app_pid() app_name = .fetch(Control_P::OPTIONS_ATTRIBUTES[:app_name], '') if old_pid p "error in kill process #{app_name}. found pid #{old_pid} try number #{try}" exit(1) if try == num_tries else p "#{app_name}, it's dead." return true end end end |
#make_sure_pid_is_real!(pid, pid_filename) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/control_helper.rb', line 139 def make_sure_pid_is_real!(pid, pid_filename) # TODO - fix this . don't validate by name but by pid (ps -p 'pid' -o comm=) # http://superuser.com/questions/632979/if-i-know-the-pid-number-of-a-process-how-can-i-get-its-name if is_pid_alive?(pid) pid else p "didn't really find pid running, deleting the file #{pid_filename}" delete_file(pid_filename) nil end end |
#print_workers_and_delete_files!(type, extension) ⇒ Object
205 206 207 208 |
# File 'lib/control_helper.rb', line 205 def print_workers_and_delete_files!(type, extension) p "#{Dir[extension].length.to_s} #{type}" Dir.glob(extension).each { |f| File.delete(f) } end |
#print_workers_started_and_stopped(options) ⇒ Object
TODO add option to overwrite the file names
196 197 198 199 |
# File 'lib/control_helper.rb', line 196 def print_workers_started_and_stopped() print_workers_and_delete_files!('workers started', Control_P::WORKERS_STARTED_EXTENSION) print_workers_and_delete_files!('workers closed', Control_P::WORKERS_CLOSED_EXTENSION) end |
#restart_the_app!(options) ⇒ Object
Note , only valid for http servers
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/control_helper.rb', line 115 def restart_the_app!() if app_not_running?() p 'app not running, starting a new process' start_a_new_process!() else restart_command = .fetch(Control_P::OPTIONS_ATTRIBUTES[:restart_command]) p "app running, restarting using #{restart_command}" `#{restart_command}` sleep(5) end end |
#retrieve_search_string(options) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/control_helper.rb', line 15 def retrieve_search_string() attributes = Control_P::OPTIONS_ATTRIBUTES find_by = .fetch(attributes[:find_pid_by], Control_P::FIND_BY_OPTIONS[:app_filename]) #TODO validate find_by is in Control_P::FIND_BY_OPTIONS search_string = .fetch(attributes[find_by], nil) raise "no idea how to search for old pid. find_by is #{find_by}" if search_string.nil? search_string end |
#skip_workers_message?(options) ⇒ Boolean
201 202 203 |
# File 'lib/control_helper.rb', line 201 def () .fetch(Control_P::OPTIONS_ATTRIBUTES[:skip_workers_message], false) end |
#start_a_new_process!(options) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/control_helper.rb', line 77 def start_a_new_process!() attributes = Control_P::OPTIONS_ATTRIBUTES start_command = .fetch(attributes[:start_command]) p "dir is #{Dir.pwd}" p "trying to start a new using start_command: #{start_command}" pid = spawn(start_command) if pid Process.detach(pid) p "detached pid #{pid} from the main program" end sleep 5 # give the new app enough time to crash find_app_pid() end |