Class: BoringBackup::Notifiers::Sentinel

Inherits:
Object
  • Object
show all
Defined in:
lib/boring_backup/notifiers/sentinel.rb

Constant Summary collapse

PAYLOAD_VERSION =
1
OPEN_TIMEOUT =
5
READ_TIMEOUT =
10
MAX_ATTEMPTS =
3
LOCAL_HOSTS =
%w[localhost 127.0.0.1 0.0.0.0]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, host:) ⇒ Sentinel

Returns a new instance of Sentinel.



17
18
19
20
# File 'lib/boring_backup/notifiers/sentinel.rb', line 17

def initialize(key:, host:)
  @key = key
  @host = host
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



15
16
17
# File 'lib/boring_backup/notifiers/sentinel.rb', line 15

def host
  @host
end

#keyObject

Returns the value of attribute key.



15
16
17
# File 'lib/boring_backup/notifiers/sentinel.rb', line 15

def key
  @key
end

Instance Method Details

#notify(result) ⇒ Object



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
# File 'lib/boring_backup/notifiers/sentinel.rb', line 26

def notify(result)
  body = payload(result).to_json
  attempt = 0

  begin
    attempt += 1
    response = post(body)

    return true if response.is_a?(Net::HTTPSuccess)
    raise BoringBackup::Error, "#{response.code} #{response.body}" if retryable?(response)

    warn "Sentinel ping failed: #{response.code} #{response.body}"

    false
  rescue => e
    if attempt < MAX_ATTEMPTS
      sleep(2**(attempt - 1))
      retry
    end

    warn "Sentinel ping error after #{attempt} attempts: #{e.message}"

    false
  end
end

#ping_urlObject



22
23
24
# File 'lib/boring_backup/notifiers/sentinel.rb', line 22

def ping_url
  URI.join(normalized_host, "/ping/#{key}")
end