Module: HeartBeat

Included in:
ManqodServer
Defined in:
lib/HeartBeat.rb

Overview

this file is part of manqod manqod is distributed under the CDDL licence the author of manqod is Dobai-Pataky Balint([email protected])

Instance Method Summary collapse

Instance Method Details

#init_HeartBeatObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/HeartBeat.rb', line 6

def init_HeartBeat
	@heartbeat_thread=Thread.new{
		lalive_clients=-1
		lalive_client_lists=-1
		loop do
			begin
				sleep 10
				to_remove=Array.new
				alive_clients=0
				@connected_clients.delete_if{|client_id,cdb|
					begin
						Timeout.timeout(30){
							#ecode("#{cc}.alive?:#{cc.alive?}")
							cdb[:object].alive?
							alive_clients+=1
							false
						}
					rescue Timeout::Error
						ewarn("client timed out, removing a dead client #{client_id}")
						true
					rescue => err
						ewarn("removing a dead client #{client_id}")
						@dbs.each_value{|db|
							db.remove_client(client_id) unless db.serving?
						}
						true
					end
				}

				alive_client_lists=0
				@dbs.each_value{|db|
					next unless db.serving?
					#keep the conenction to the cache alive
					db.cache.set("alive",Time.new)
					#check for alive client list subscriptions
					alive_client_lists+=db.remove_dead_clients
				}
				einfo("#{alive_clients} clients, #{alive_client_lists} subscriptions") unless alive_clients==lalive_clients && alive_client_lists==lalive_client_lists

				lalive_clients=alive_clients
				lalive_client_lists=alive_client_lists
			rescue ECONNREFUSED => err
				eerror("HeartBeat: #{err}")
			rescue =>err
				eexception(err)
			end
		end
	}
	einfo("HeartBeat started")
end

#stop_HeartBeatObject



56
57
58
59
# File 'lib/HeartBeat.rb', line 56

def stop_HeartBeat
	@heartbeat_thread.kill if @heartbeat_thread
	einfo("HeartBeat stopped")
end