Class: Profiler::Cluster::SlaveProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/profiler/cluster/slave_proxy.rb

Constant Summary collapse

OPEN_TIMEOUT =

A slow or hung slave must not block the master's request thread for long.

5
READ_TIMEOUT =

seconds

10

Instance Method Summary collapse

Constructor Details

#initialize(slave_name, open_timeout: nil, read_timeout: nil) ⇒ SlaveProxy

seconds

Raises:



14
15
16
17
18
19
20
21
# File 'lib/profiler/cluster/slave_proxy.rb', line 14

def initialize(slave_name, open_timeout: nil, read_timeout: nil)
  entry = Profiler.slave_registry.find!(slave_name)
  raise Profiler::Error, "Slave profiler '#{slave_name}' is offline" if entry.status == "offline"

  @base_url = entry.url.to_s.chomp("/")
  @open_timeout = open_timeout || OPEN_TIMEOUT
  @read_timeout = read_timeout || READ_TIMEOUT
end

Instance Method Details

#clear(type: nil) ⇒ Object



45
46
47
48
# File 'lib/profiler/cluster/slave_proxy.rb', line 45

def clear(type: nil)
  params = type ? "?type=#{URI.encode_www_form_component(type)}" : ""
  delete_json("/_profiler/api/profiles/clear#{params}")
end

#delete_json(path) ⇒ Object



65
66
67
68
# File 'lib/profiler/cluster/slave_proxy.rb', line 65

def delete_json(path)
  uri = build_uri(path)
  request(uri, Net::HTTP::Delete.new(uri))
end

#find_by_parent(parent_token) ⇒ Object



40
41
42
43
# File 'lib/profiler/cluster/slave_proxy.rb', line 40

def find_by_parent(parent_token)
  data = get_json("/_profiler/api/profiles", parent_token: parent_token, all_types: true)
  Array(data["profiles"]).map { |h| profile_from_api(h) }
end

#get_json(path, params = {}) ⇒ Object

Generic HTTP access for action tools



52
53
54
55
# File 'lib/profiler/cluster/slave_proxy.rb', line 52

def get_json(path, params = {})
  uri = build_uri(path, params)
  request(uri, Net::HTTP::Get.new(uri))
end

#list(limit: 50, offset: 0) ⇒ Object

Storage-compatible interface for MCP query tools



25
26
27
28
29
30
# File 'lib/profiler/cluster/slave_proxy.rb', line 25

def list(limit: 50, offset: 0, **)
  # all_types mirrors Profiler.storage.list, which returns every profile type;
  # without it the proxy would only ever see http profiles (see ProfilesController#index).
  data = get_json("/_profiler/api/profiles", limit: limit, offset: offset, all_types: true)
  Array(data["profiles"]).map { |h| profile_from_api(h) }
end

#load(token) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/profiler/cluster/slave_proxy.rb', line 32

def load(token)
  data = get_json("/_profiler/api/profiles/#{token}")
  return nil unless data["token"] || data["profile"]

  raw = data["profile"] || data
  profile_from_api(raw)
end

#patch_json(path, body = {}) ⇒ Object



61
62
63
# File 'lib/profiler/cluster/slave_proxy.rb', line 61

def patch_json(path, body = {})
  request(*json_request(Net::HTTP::Patch, path, body))
end

#post_json(path, body = {}) ⇒ Object



57
58
59
# File 'lib/profiler/cluster/slave_proxy.rb', line 57

def post_json(path, body = {})
  request(*json_request(Net::HTTP::Post, path, body))
end