Class: WebappWorker::System

Inherits:
Object
  • Object
show all
Defined in:
lib/webapp_worker/system.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_supplied_hash = {}) ⇒ System

Returns a new instance of System.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/webapp_worker/system.rb', line 10

def initialize(user_supplied_hash={})
	standard_hash = { user:"nobody", tmp_dir:"/tmp/webapp_worker/", pid_file:"/tmp/webapp_worker/waw.pid", ipc_file:"/tmp/webapp_worker/waw", logger:"" }

	user_supplied_hash = {} unless user_supplied_hash
	user_supplied_hash = standard_hash.merge(user_supplied_hash)

	user_supplied_hash.each do |key,value|
		self.instance_variable_set("@#{key}", value)
		self.class.send(:define_method, key, proc{self.instance_variable_get("@#{key}")})
		self.class.send(:define_method, "#{key}=", proc{|x| self.instance_variable_set("@#{key}", x)})
	end
end

Instance Attribute Details

#ipc_fileObject

Returns the value of attribute ipc_file.



8
9
10
# File 'lib/webapp_worker/system.rb', line 8

def ipc_file
  @ipc_file
end

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/webapp_worker/system.rb', line 8

def logger
  @logger
end

#pid_fileObject

Returns the value of attribute pid_file.



8
9
10
# File 'lib/webapp_worker/system.rb', line 8

def pid_file
  @pid_file
end

#tmp_dirObject

Returns the value of attribute tmp_dir.



8
9
10
# File 'lib/webapp_worker/system.rb', line 8

def tmp_dir
  @tmp_dir
end

#userObject

Returns the value of attribute user.



8
9
10
# File 'lib/webapp_worker/system.rb', line 8

def user
  @user
end

Class Method Details

.process_alive?(pid) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
# File 'lib/webapp_worker/system.rb', line 23

def self.process_alive?(pid)
  begin
    Process.kill(0, pid.to_i)
    true
  rescue Errno::ESRCH
    false
  end
end

Instance Method Details

#check_for_directoryObject



55
56
57
58
59
60
# File 'lib/webapp_worker/system.rb', line 55

def check_for_directory
	if Dir.exists?(@tmp_dir)
	else
		Dir.mkdir(@tmp_dir, 0700)
	end
end

#check_for_processObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/webapp_worker/system.rb', line 100

def check_for_process
	if File.exists?(@pid_file)
		possible_pid = ""
		pf = File.open(@pid_file, 'r').each { |f| possible_pid+= f }
		pf.close

		if WebappWorker::System.process_alive?(possible_pid)
        version = self.check_process_version
        my_version = WebappWorker::VERSION

			if version.to_s == my_version.to_s
				puts "Already found webapp_worker running, pid is: #{possible_pid}, exiting..."
				@logger.fatal "Found webapp_worker already running with pid: #{possible_pid}, Pid File: #{@pid_file} exiting..."
				exit 1
			else
				puts "Found old version of webapp_worker #{version}, running with pid: #{possible_pid}, asking it to terminate since my version is: #{my_version}"
				@logger.fatal "Found old version of webapp_worker #{version}, running with pid: #{possible_pid}, asking it to terminate since my version is: #{my_version}"

				Process.kill("INT",possible_pid.to_i)

				while WebappWorker::System.process_alive?(possible_pid)
					sleep 120

					begin
              Process.kill("KILL",possible_pid.to_i)
					rescue => error
					end

            self.create_pid
				end
			end
		else
			@logger.warn "Found pid file, but no process running"
			self.create_pid
		end
	else
		@logger.info "Did not find a pid file"
		self.create_pid
	end

	@logger.info "Starting Webapp Worker"
end

#check_process_versionObject



151
152
153
154
155
156
157
158
159
# File 'lib/webapp_worker/system.rb', line 151

def check_process_version
	@logger.info "Asking the processes version from IPC: #{@ipc_file}"

	DRb.start_service
	version = DRbObject.new_with("drbunix:#{@ipc_file}", nil)

	@logger.info "The process' version is: #{version}"
	return version
end

#create_logger(debug = nil, verbose = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/webapp_worker/system.rb', line 62

def create_logger(debug=nil,verbose=nil)
  @logger = Logger.new("#{@tmp_dir}waw.log", 5, 5242880)

  if debug
    @logger.level = Logger::DEBUG
  elsif verbose
    @logger.level = Logger::INFO
  else
    @logger.level = Logger::WARN
  end
end

#create_pidObject



92
93
94
95
96
97
98
# File 'lib/webapp_worker/system.rb', line 92

def create_pid
    self.delete_files

	@logger.info "Creating Pid File at #{@pid_file}"
	File.open(@pid_file, 'w') { |f| f.write(Process.pid) }
	@logger.info "Pid File created: #{Process.pid} at #{@pid_file}"
end

#delete_filesObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/webapp_worker/system.rb', line 74

def delete_files
  @logger.fatal "Deleting both PID and IPC files"

  begin
File.delete(@pid_file)
    @logger.fatal "Deleted PID File: #{@pid_file}"
  rescue => error
    @logger.fatal "Error at Deleting PID File: #{@pid_file}: #{error}"
  end

  begin
File.delete(@ipc_file)
    @logger.fatal "Deleted IPC File: #{@ipc_file}"
  rescue => error
    @logger.fatal "Error at Deleting IPC File: #{@ipc_file}: #{error}"
  end
end

#graceful_termination(threads, command_processes) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/webapp_worker/system.rb', line 161

def graceful_termination(threads,command_processes)
	begin
		puts
		puts "Graceful Termination started, waiting 60 seconds before KILL signal send"
		@logger.info "Graceful Termination started, waiting 60 seconds before KILL signal send"

		Timeout::timeout(60) do
			command_processes.each do |pid,command|
				@logger.debug "Sending INT Signal to #{command} Process with PID: #{pid}"

				if WebappWorker::System.process_alive?(pid)
					Process.kill("INT",pid.to_i)
				end
			end

			threads.each do |thread,command|
				thread.join
			end
		end
	rescue Timeout::Error
		puts "Graceful Termination bypassed, killing processes and threads"
		@logger.info "Graceful Termination bypassed, killing processes and threads"

		command_processes.each do |pid,command|
			@logger.debug "Killing #{command} Process with PID: #{pid}"

        if WebappWorker::System.process_alive?(pid)
          Process.kill("KILL",pid.to_i)
        end
		end

		threads.each do |thread,command|
			@logger.debug "Killing Command Thread: #{command}"
			Thread.kill(thread)
		end
	end

	puts "Stopping Webapp Worker"
	@logger.info "Stopping Webapp Worker"

    self.delete_files
	exit 0
end

#setup(debug = nil, verbose = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/webapp_worker/system.rb', line 32

def setup(debug=nil,verbose=nil)
  self.check_for_directory
  self.create_logger(debug,verbose)
  self.check_for_process
  self.start_listening

  Signal.trap('HUP', 'IGNORE')

  Signal.trap('USR1') do
    version = WebappWorker::VERSION
    puts
    puts "Webapp Worker Version: #{version}"
    logger.info "Received USR1 signal, sent version: #{version}"
  end

  Signal.trap('USR2') do
    logger.level = Logger::DEBUG
    puts
    puts "Changed logger level to Debug"
    logger.info "Changed logger level to Debug"
  end
end

#start_listeningObject



143
144
145
146
147
148
149
# File 'lib/webapp_worker/system.rb', line 143

def start_listening
	@logger.info "Starting to listen on IPC: #{@ipc_file}"

	DRb.start_service("drbunix:#{@ipc_file}", WebappWorker::VERSION)

	@logger.info "Now listenting on IPC: #{@ipc_file}"
end