Class: VCAP::Services::Base::Base

Inherits:
Object
  • Object
show all
Includes:
Error
Defined in:
lib/base/base.rb,
lib/base/base.rb

Direct Known Subclasses

Node, Provisioner

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Error

#failure, #internal_fail, #parse_msg, #success, #timeout_fail

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.



32
33
34
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
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/base/base.rb', line 32

def initialize(options)
  @logger = options[:logger]
  @options = options
  @local_ip = VCAP.local_ip(options[:ip_route])
  @logger.info("#{service_description}: Initializing")
  @closing = false

  @node_nats = nil
  if options[:mbus]
    NATS.on_error do |e|
      # p $!
      # puts $!.backtrace.join("\n")
      @logger.error("Exiting due to NATS error: #{e}")
      shutdown
      exit
    end
    @logger.debug("Connecting with NATS")
    @node_nats = NATS.connect(:uri => options[:mbus]) do
      status_port = status_user = status_password = nil
      if not options[:status].nil?
        status_port = options[:status][:port]
        status_user = options[:status][:user]
        status_password = options[:status][:password]
      end

      @logger.debug("Registering with NATS")
      VCAP::Component.register(
        :nats => @node_nats,
        :type => service_description,
        :host => @local_ip,
        :index => options[:index] || 0,
        :config => options,
        :port => status_port,
        :user => status_user,
        :password => status_password
      )
      on_connect_node
    end
  else
    @logger.info("NATS is disabled")
  end

  @max_nats_payload = options[:max_nats_payload] || 1024 * 1024
end

Instance Attribute Details

#closingObject (readonly)

Returns the value of attribute closing.



30
31
32
# File 'lib/base/base.rb', line 30

def closing
  @closing
end

Instance Method Details

#group_handles_in_json(instances_list, bindings_list, size_limit) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/base/base.rb', line 101

def group_handles_in_json(instances_list, bindings_list, size_limit)
  while instances_list.count > 0 or bindings_list.count > 0
    ins_list = []
    bind_list = []
    send_len = 0
    idx_ins = 0
    idx_bind = 0

    instances_list.each do |ins|
      len = ins.to_json.size + 1
      if send_len + len < size_limit
        send_len += len
        idx_ins += 1
        ins_list << ins
      else
        break
      end
    end
    instances_list.slice!(0, idx_ins) if idx_ins > 0

    bindings_list.each do |bind|
      len = bind.to_json.size + 1
      if send_len + len < size_limit
        send_len += len
        idx_bind += 1
        bind_list << bind
      else
        break
      end
    end
    bindings_list.slice!(0, idx_bind) if idx_bind > 0

    # Generally, the size_limit is far more bigger than the length
    # of any handles. If there's a huge handle or the size_limit is too
    # small that the size_limit can't contain one handle the in one batch,
    # we have to break the loop if no handle can be stuffed into batch.
    if ins_list.count == 0 and bind_list.count == 0
      @logger.warn("NATS message limit #{size_limit} is too small.")
      break
    else
      yield ins_list, bind_list
    end
  end
end

#publish(reply, msg) ⇒ Object



81
82
83
84
85
86
# File 'lib/base/base.rb', line 81

def publish(reply, msg)
  # nats publish are only allowed to be called in reactor thread.
  EM.schedule do
    @node_nats.publish(reply, msg) if @node_nats
  end
end

#service_descriptionObject



77
78
79
# File 'lib/base/base.rb', line 77

def service_description()
  return "#{service_name}-#{flavor}"
end

#shutdownObject



95
96
97
98
99
# File 'lib/base/base.rb', line 95

def shutdown()
  @closing = true
  @logger.info("#{service_description}: Shutting down")
  @node_nats.close if @node_nats
end

#update_varzObject



88
89
90
91
92
93
# File 'lib/base/base.rb', line 88

def update_varz()
  vz = varz_details
  vz.each { |k,v|
    VCAP::Component.varz[k] = v
  } if vz
end