Class: Clockwork::MultipleNodes::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/clockwork/multiple_nodes/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHandler

Returns a new instance of Handler.



9
10
11
# File 'lib/clockwork/multiple_nodes/handler.rb', line 9

def initialize
  log_info("Initializing clockwork multiple nodes handler")
end

Instance Attribute Details

#stateObject

Returns the value of attribute state.



7
8
9
# File 'lib/clockwork/multiple_nodes/handler.rb', line 7

def state
  @state
end

Instance Method Details

#check_activeObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/clockwork/multiple_nodes/handler.rb', line 35

def check_active
  active=false
  if redis.get("#{namespace}:active") == 'true' || redis.setnx("#{namespace}:active", 'true')
    if (existing=redis.get('clockwork:node:id')) == self.node_id || (lock=redis.setnx("#{namespace}:id", self.node_id))
      begin
        log_info("Schedule reserved with [#{self.node_id}]") if lock
        result=redis.multi do
          redis.setex("#{namespace}:name", self.heartbeat_timeout, self.process_name)
          redis.set("#{namespace}:heartbeat", DateTime.now.strftime("%Y-%m-%d %H:%M:%S %Z"))
          redis.expire("#{namespace}:id", self.heartbeat_timeout)
        end
        if result[0] == 'OK'
          self.state=:active
          active=true
        end
      end
    else
      log_info("Schedule already reserved [#{existing}]") unless self.state == :waiting
      self.state=:waiting
    end
  else
    log_info("Schedule is not active") unless self.state == :inactive
    self.state=:inactive
  end
  @proc_name||=$0
  $0="#{@proc_name} - #{self.state} [#{self.node_id}]"
  active
end

#heartbeat_timeoutObject



21
22
23
# File 'lib/clockwork/multiple_nodes/handler.rb', line 21

def heartbeat_timeout
  config[:heartbeat_timeout] || (config[:sleep_timeout] * 10)
end

#log_debug(msg) ⇒ Object



68
69
70
# File 'lib/clockwork/multiple_nodes/handler.rb', line 68

def log_debug(msg)
  config[:logger].debug(msg)
end

#log_error(msg) ⇒ Object



72
73
74
# File 'lib/clockwork/multiple_nodes/handler.rb', line 72

def log_error(msg)
  config[:logger].error(msg)
end

#log_info(msg) ⇒ Object



64
65
66
# File 'lib/clockwork/multiple_nodes/handler.rb', line 64

def log_info(msg)
  config[:logger].info(msg)
end

#node_idObject



13
14
15
# File 'lib/clockwork/multiple_nodes/handler.rb', line 13

def node_id
  @node_id||=SecureRandom.hex(16).downcase
end

#process_nameObject



25
26
27
28
29
30
31
32
33
# File 'lib/clockwork/multiple_nodes/handler.rb', line 25

def process_name
  name = begin
    Socket.gethostbyname(Socket.gethostname).first
  rescue SocketError => why
    Socket.gethostname.underscore.gsub(".", "_")
  end
  #log_debug("Name: " + name)
  "#{name}-p#{Process.pid}-t#{Thread.current.object_id}"
end