Module: Glider::ProcessManager

Defined in:
lib/glider/process_manager.rb

Class Method Summary collapse

Class Method Details

.childrenObject

tracks forks/threads started as workers



10
11
12
# File 'lib/glider/process_manager.rb', line 10

def children
	@children ||= []
end

.kill_childrenObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/glider/process_manager.rb', line 14

def kill_children
	if $leader_pid == Process.pid
		@end_monit = true
		children.each do |child|
			begin 
				pid, worker_proc = child
				Process.kill('USR1', pid)
			rescue Errno::ESRCH => e
				# process already killed
			end
		end
	end
end

.monitor_childrenObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/glider/process_manager.rb', line 33

def monitor_children
	loop do
		break if @end_monit
		children.each_with_index do |child, index|
			pid, worker_proc = child
			begin
				Process.kill 0, pid
			rescue Errno::ESRCH
				children.delete_at index
				$logger.info "Restarting worker...."
				start worker_proc
			end
		end
		sleep 1
	end
	Glider::ProcessManager.kill_children
end

.register_worker(worker_proc) ⇒ Object



51
52
53
# File 'lib/glider/process_manager.rb', line 51

def register_worker(worker_proc)
	workers << worker_proc
end

.start(worker_proc) ⇒ Object



55
56
57
58
59
60
# File 'lib/glider/process_manager.rb', line 55

def start(worker_proc)
	pid = fork do
		worker_proc.call
	end
	children << [pid, worker_proc]
end

.start_workersObject



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/glider/process_manager.rb', line 62

def start_workers
	$leader_pid ||= Process.pid
	Signal.trap('TERM') {Glider::ProcessManager.kill_children}
	Signal.trap('INT') {Glider::ProcessManager.kill_children}
	# todo start workers as forks
	@workers.each do |worker_proc|
		start worker_proc
	end
	Thread.new do
		monitor_children
	end
	Process.waitall
end

.workersObject



28
29
30
# File 'lib/glider/process_manager.rb', line 28

def workers
	@workers ||= []
end