Class: Synapse::Easy::Service

Inherits:
Object
  • Object
show all
Includes:
Fake::Hash
Defined in:
lib/synapse/easy/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Service

Returns a new instance of Service.



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
# File 'lib/synapse/easy/service.rb', line 16

def initialize params={}
  @default_servers ||= []
  @discovery       ||= {}
  @haproxy         ||= {}
  @port              = params[:port]
  @port            ||= Freeport.port
  @hosts             = params[:hosts]
  @hosts           ||= Resolv::Consul.services(:zookeeper).collect{|x|"#{x.address}:#{x.port}"}
  @mode              = params[:mode]
  @mode            ||= :tcp
  @uri               = params[:uri]
  @uri             ||= "/"
  @inter             = params[:inter]
  @inter           ||= 2000
  @rise              = params[:rise]
  @rise            ||= 2
  @fall              = params[:fall]
  @fall            ||= 3
  @method          ||= "zookeeper"
  @registry          = params[:registry]
  @registry        ||= "nerve"
  @customer          = params[:customer]
  @customer        ||= "global"
  @application       = params[:application]
  @application     ||= "generic"
  @function          = params[:function]
  @function        ||= "common"
  @extension         = params[:extension]
  @extension       ||= "services"
  @path              = params[:path]
  @path            ||= Pathname.new("/#{@registry}").join(@customer.to_s,@application.to_s,@function.to_s,@extension.to_s).to_s
  @name              = params[:name]
  @name            ||= [@customer,@application,@function].join("_")
  @active          ||= false
  @enable_frontend   = params[:enable_frontend]
  @enable_frontend ||= true if @enable_frontend.nil?
  @service           = params[:service]
end

Instance Attribute Details

#active ⇒ Object

Returns the value of attribute active.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def active
  @active
end

#application ⇒ Object

Returns the value of attribute application.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def application
  @application
end

#customer ⇒ Object

Returns the value of attribute customer.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def customer
  @customer
end

#default_servers ⇒ Object (readonly)

Returns the value of attribute default_servers.



15
16
17
# File 'lib/synapse/easy/service.rb', line 15

def default_servers
  @default_servers
end

#enable_backend ⇒ Object

Returns the value of attribute enable_backend.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def enable_backend
  @enable_backend
end

#enable_frontend ⇒ Object

Returns the value of attribute enable_frontend.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def enable_frontend
  @enable_frontend
end

#extension ⇒ Object

Returns the value of attribute extension.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def extension
  @extension
end

#fall ⇒ Object

Returns the value of attribute fall.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def fall
  @fall
end

#function ⇒ Object

Returns the value of attribute function.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def function
  @function
end

#hosts ⇒ Object

Returns the value of attribute hosts.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def hosts
  @hosts
end

#inter ⇒ Object

Returns the value of attribute inter.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def inter
  @inter
end

#keep_default_servers ⇒ Object

Returns the value of attribute keep_default_servers.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def keep_default_servers
  @keep_default_servers
end

#leader_election ⇒ Object

Returns the value of attribute leader_election.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def leader_election
  @leader_election
end

#method ⇒ Object

Returns the value of attribute method.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def method
  @method
end

#mode ⇒ Object

Returns the value of attribute mode.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def mode
  @mode
end

#name ⇒ Object

Returns the value of attribute name.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def name
  @name
end

#path ⇒ Object

Returns the value of attribute path.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def path
  @path
end

#port ⇒ Object

Returns the value of attribute port.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def port
  @port
end

#registry ⇒ Object

Returns the value of attribute registry.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def registry
  @registry
end

#rise ⇒ Object

Returns the value of attribute rise.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def rise
  @rise
end

#uri ⇒ Object

Returns the value of attribute uri.



11
12
13
# File 'lib/synapse/easy/service.rb', line 11

def uri
  @uri
end

Instance Method Details

#add_default_server(name, port) ⇒ Object



54
55
56
# File 'lib/synapse/easy/service.rb', line 54

def add_default_server name, port
  @default_servers << "#{name}:#{port}"
end

#address ⇒ Object



109
110
111
112
# File 'lib/synapse/easy/service.rb', line 109

def address
  return "127.0.0.1" if host.to_s == "localhost"
  Resolv::DNS.new.getaddress(host.to_s).to_s
end

#aliases ⇒ Object



69
70
71
72
73
74
75
# File 'lib/synapse/easy/service.rb', line 69

def aliases
  [
    @application,
    [@application,@function].join("_"),
    [@customer,@application].join("_"),
  ]
end

#check ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/synapse/easy/service.rb', line 128

def check
  begin
    socket = TCPSocket.new(host.to_s,port.to_i)
  rescue Errno::ECONNREFUSED
    sleep 0.1
    retry
  ensure
    socket.close
    return true
  end
  return false
end

#discover_via(discovery_data) ⇒ Object



60
61
62
# File 'lib/synapse/easy/service.rb', line 60

def discover_via discovery_data
  @discovery = discovery_data
end

#discovery ⇒ Object



76
77
78
79
80
81
82
# File 'lib/synapse/easy/service.rb', line 76

def discovery
  {
    method: method,
    path:   path,
    hosts:  hosts,
  }.with_indifferent_access
end

#haproxy ⇒ Object



83
84
85
86
87
88
89
# File 'lib/synapse/easy/service.rb', line 83

def haproxy
  result = {}.with_indifferent_access
  result[:port] = port if enable_frontend
  result[:listen] = listen
  result[:server_options] = server_options
  result
end

#host ⇒ Object



66
67
68
# File 'lib/synapse/easy/service.rb', line 66

def host
  "localhost"
end

#listen ⇒ Object



93
94
95
96
97
98
# File 'lib/synapse/easy/service.rb', line 93

def listen
  [
      "mode #{mode}",
    ( "option httpchk GET #{uri}" if uri and mode.to_s == "http" )
  ].compact
end

#provide_at(haproxy_data) ⇒ Object



63
64
65
# File 'lib/synapse/easy/service.rb', line 63

def provide_at haproxy_data
  @haproxy = haproxy_data
end

#remove_default_server(name, port) ⇒ Object



57
58
59
# File 'lib/synapse/easy/service.rb', line 57

def remove_default_server name, port
  @default_servers.delete "#{name}:#{port}"
end

#server_options ⇒ Object



90
91
92
# File 'lib/synapse/easy/service.rb', line 90

def server_options
  "check inter #{inter} rise #{rise} fall #{fall}" if [inter,rise,fall].compact.size == 3
end

#service ⇒ Object



106
107
108
# File 'lib/synapse/easy/service.rb', line 106

def service
  @service
end

#to_s(format = "%P://%h:%p") ⇒ Object Also known as: s



113
114
115
116
117
118
# File 'lib/synapse/easy/service.rb', line 113

def to_s format="%P://%h:%p"
  format = format.gsub('%P',mode.to_s)
  format = format.gsub('%h',host.to_s)
  format = format.gsub('%i',address.to_s)
  format.gsub('%p',port.to_s)
end

#to_synapse ⇒ Object



99
100
101
102
103
104
105
# File 'lib/synapse/easy/service.rb', line 99

def to_synapse
  return application => {
    default_servers:default_servers,
    discovery: discovery,
    haproxy:   haproxy
  }
end

#to_uri ⇒ Object



125
126
127
# File 'lib/synapse/easy/service.rb', line 125

def to_uri
  URI to_s()
end

#to_url ⇒ Object Also known as: url



120
121
122
123
# File 'lib/synapse/easy/service.rb', line 120

def to_url
  return unless mode.to_sym == :http
  to_s() 
end