Class: KL::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/kldockeragent/notifier.rb

Constant Summary collapse

SleepTime =
10
NotifierLockFile =
"#{KL.home}/notifier.lock.#{Time.now.to_i}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p = {}) ⇒ Notifier

Returns a new instance of Notifier.



11
12
13
14
15
16
17
18
# File 'lib/kldockeragent/notifier.rb', line 11

def initialize(p={})
	@lock = Mutex.new
	@enabled = false
	@status = :stopped
	@index = 0
	config = KL.config
	@rate = config['notifier']['rate']
end

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



9
10
11
# File 'lib/kldockeragent/notifier.rb', line 9

def enabled
  @enabled
end

#modeObject (readonly)

Returns the value of attribute mode.



9
10
11
# File 'lib/kldockeragent/notifier.rb', line 9

def mode
  @mode
end

#statusObject (readonly)

Returns the value of attribute status.



9
10
11
# File 'lib/kldockeragent/notifier.rb', line 9

def status
  @status
end

Instance Method Details

#notify_dataObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/kldockeragent/notifier.rb', line 88

def notify_data
	KL.logger.info "[heartbeat]"
	if KL.server_registered
		if (@index <= (@rate/SleepTime))
			@index = @index + 1
		else
			@index = 0
			KL.logger.info "[notifier] Executing notifier..."
			KL.Command_notifyStatus
			KL.logger.info "[notifier] Notifier executed."
		end
	else
		KL.logger.info "[notifier] Executing notifier..."
		KL.Command_registerNewAgent
		KL.logger.info "[notifier] Notifier executed."
	end
end

#register_notifier_thread(mode = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/kldockeragent/notifier.rb', line 68

def register_notifier_thread(mode=nil)
	File.open(NotifierLockFile, File::RDWR|File::CREAT, 0644) { |f|
		f.flock(File::LOCK_EX)
		value = (mode == :reset ? 0 : (f.read.to_i + 1))
		f.rewind
		f.write(value.to_s)
		f.flush
		f.truncate(f.pos)
	}
end

#run_notifierObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kldockeragent/notifier.rb', line 51

def run_notifier
	KL.logger.info "[notifier] Executing notifier engine every #{@rate} seconds"
	while @enabled
		begin
			wait_for_notifier?

			notify_data

			sleep SleepTime
			
		rescue Exception => e
			KL.logger.error "Error on executing notifier \n#{e}\n#{e.backtrace.join("\n")}"
			sleep SleepTime
		end
	end
end

#startObject



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
# File 'lib/kldockeragent/notifier.rb', line 24

def start
	@enabled = true
	@lock.synchronize {
		return if @status == :running
		@status = :running
	}
	Thread.new {
		begin
			register_notifier_thread(:reset)

			system("rm -f #{KL.home}/operator.*.lock")
			system("rm -f #{KL.home}/notifier.lock.*")

			KL.logger.info "[notifier] Notifier engine is running"

			self.run_notifier

			File.delete(NotifierLockFile) if File.exist?(NotifierLockFile)
			KL.logger.info "[notifier] Notifier engine has stopped."
		
		rescue Exception => exp
			KL.logger.error "Cannot start notifier engine => #{exp}\n#{exp.backtrace.join("\n")}"
		end
		@status = :stopped
	}
end

#stopObject



20
21
22
# File 'lib/kldockeragent/notifier.rb', line 20

def stop
	@enabled = false
end

#wait_for_notifier?Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
# File 'lib/kldockeragent/notifier.rb', line 79

def wait_for_notifier?
	total_notifier = 1
	loop do
		total_notifier = (File.exist?(NotifierLockFile) ? File.read(NotifierLockFile).to_i : 0)
		return if total_notifier <= 0 or not @enabled
		sleep 1
	end
end