Class: Heroku::ReadOnly
- Inherits:
-
Object
- Object
- Heroku::ReadOnly
- Defined in:
- lib/heroku/readonly.rb
Class Method Summary collapse
Instance Method Summary collapse
- #check ⇒ Object
- #enabled? ⇒ Boolean
-
#initialize(options = {}) ⇒ ReadOnly
constructor
A new instance of ReadOnly.
- #poll(attempt = 1) ⇒ Object
- #read_from_cache ⇒ Object
- #write_to_cache ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ReadOnly
Returns a new instance of ReadOnly.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/heroku/readonly.rb', line 5 def initialize(={}) raise ArgumentError unless .is_a?(Hash) @enabled = false @have_polled = false @url = [:url] @time_between_retries = [:time_between_retries] || 2 @write_cache = [:write_cache] @read_cache = [:read_cache] end |
Class Method Details
.disable(put_url) ⇒ Object
72 73 74 |
# File 'lib/heroku/readonly.rb', line 72 def self.disable(put_url) update(put_url, "ok") end |
.enable(put_url) ⇒ Object
68 69 70 |
# File 'lib/heroku/readonly.rb', line 68 def self.enable(put_url) update(put_url, "readonly") end |
.update(put_url, body) ⇒ Object
63 64 65 66 |
# File 'lib/heroku/readonly.rb', line 63 def self.update(put_url, body) out = `curl -s -m 3 -X PUT -H "Content-Type:" -d "#{body}" "#{put_url}"` raise(out) unless ($? == 0) end |
Instance Method Details
#check ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/heroku/readonly.rb', line 16 def check Timeout.timeout(1) do RestClient.get(@url) != 'ok' end rescue StandardError, Timeout::Error => e true end |
#enabled? ⇒ Boolean
53 54 55 56 57 58 59 60 61 |
# File 'lib/heroku/readonly.rb', line 53 def enabled? if @read_cache read_from_cache elsif !@have_polled raise('never polled, no read_cache') else @enabled end end |
#poll(attempt = 1) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/heroku/readonly.rb', line 24 def poll(attempt=1) result = check if (result == @enabled) || (attempt >= 3) @have_polled = true @enabled = result write_to_cache result elsif attempt < 3 sleep(@time_between_retries) poll(attempt+1) end end |
#read_from_cache ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/heroku/readonly.rb', line 43 def read_from_cache if !File.exists?(@read_cache) true elsif File.mtime(@read_cache) < Time.now - 60*5 true else File.read(@read_cache).strip != 'ok' end end |
#write_to_cache ⇒ Object
37 38 39 40 41 |
# File 'lib/heroku/readonly.rb', line 37 def write_to_cache if @write_cache File.open(@write_cache, 'w') { |f| f.write(@enabled ? 'readonly' : 'ok') } end end |