Class: KL::Notifier
- Inherits:
-
Object
- Object
- KL::Notifier
- Defined in:
- lib/kldockeragent/notifier.rb
Constant Summary collapse
Instance Attribute Summary collapse
-
#enabled ⇒ Object
readonly
Returns the value of attribute enabled.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#initialize(p = {}) ⇒ Notifier
constructor
A new instance of Notifier.
- #notify_data ⇒ Object
- #register_notifier_thread(mode = nil) ⇒ Object
- #run_notifier ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #wait_for_notifier? ⇒ Boolean
Constructor Details
Instance Attribute Details
#enabled ⇒ Object (readonly)
Returns the value of attribute enabled.
9 10 11 |
# File 'lib/kldockeragent/notifier.rb', line 9 def enabled @enabled end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
9 10 11 |
# File 'lib/kldockeragent/notifier.rb', line 9 def mode @mode end |
#status ⇒ Object (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_data ⇒ Object
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_notifier ⇒ Object
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 |
#start ⇒ Object
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 |
#stop ⇒ Object
20 21 22 |
# File 'lib/kldockeragent/notifier.rb', line 20 def stop @enabled = false end |
#wait_for_notifier? ⇒ 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 |