Module: MusicBrainz::ClientModules::FailsafeProxy

Included in:
MusicBrainz::Client
Defined in:
lib/musicbrainz/client_modules/failsafe_proxy.rb

Instance Method Summary collapse

Instance Method Details

#failsafe?Boolean

Returns:

  • (Boolean)

35
36
37
# File 'lib/musicbrainz/client_modules/failsafe_proxy.rb', line 35

def failsafe?
  MusicBrainz.config.tries_limit > 1 && MusicBrainz.config.query_interval.to_f > 0
end

#get_contents(url) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/musicbrainz/client_modules/failsafe_proxy.rb', line 5

def get_contents(url)
  return super unless failsafe?

  response = { body: nil, status: 500 }
  MusicBrainz.config.tries_limit.times do
    wait_util_ready!
    response = super
    break if response[:status] == 200
  end

  response
end

#ready?Boolean

Returns:

  • (Boolean)

26
27
28
# File 'lib/musicbrainz/client_modules/failsafe_proxy.rb', line 26

def ready?
  time_passed > MusicBrainz.config.query_interval
end

#time_passedObject


18
19
20
# File 'lib/musicbrainz/client_modules/failsafe_proxy.rb', line 18

def time_passed
  Time.now.to_f - @last_query_time ||= 0.0
end

#time_to_waitObject


22
23
24
# File 'lib/musicbrainz/client_modules/failsafe_proxy.rb', line 22

def time_to_wait
  MusicBrainz.config.query_interval - time_passed
end

#wait_util_ready!Object


30
31
32
33
# File 'lib/musicbrainz/client_modules/failsafe_proxy.rb', line 30

def wait_util_ready!
  sleep(time_to_wait) unless ready?
  @last_query_time = Time.now.to_f
end