Skip to content

Commit

Permalink
Add fallback polling
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieSinn committed Jul 22, 2024
1 parent 913a06d commit 4fe0dce
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lib/devcycle-ruby-server-sdk/localbucketing/config_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def initialize(sdkKey, local_bucketing, wait_for_init)
@max_config_retries = 2
@config_poller = Concurrent::TimerTask.new({
execution_interval: @local_bucketing.options.config_polling_interval_ms.fdiv(1000)
}) do |task|
}) do |_|
fetch_config
end

Expand All @@ -41,7 +41,7 @@ def initialize(sdkKey, local_bucketing, wait_for_init)
def initialize_config
begin
fetch_config
start_polling
start_polling(false)
rescue => e
@logger.error("DevCycle: Error Initializing Config: #{e.message}")
ensure
Expand Down Expand Up @@ -157,12 +157,18 @@ def get_config_url
"#{configBasePath}/config/#{@config_version}/server/#{@sdkKey}.json"
end

def start_polling
def start_polling(sse)
if sse
@config_poller.shutdown if @config_poller.running?
@config_poller = Concurrent::TimerTask.new({ execution_interval: 60 *10 }) do |_|
fetch_config
end
end
@polling_enabled = true
@config_poller.execute if @polling_enabled && !@sse_active
@config_poller.execute if @polling_enabled && (!@sse_active || sse)
end

def stop_polling
def stop_polling()
@polling_enabled = false
@config_poller.shutdown if @config_poller.running?
end
Expand All @@ -172,7 +178,7 @@ def stop_sse
@polling_enabled = true
@sse_active = false
@sse_client.close if @sse_client
start_polling
start_polling(false)
end

def close
Expand All @@ -194,22 +200,22 @@ def init_sse(path)
end
end
stop_polling
start_polling(true)
end

def handle_sse(eventData)
@logger.debug(eventData)
if eventData["data"] == nil
return
end

@logger.debug("SSE Message received: #{eventData["data"]}")
parsed_event_data = JSON.parse(eventData["data"])

last_modified = parsed_event_data["lastModified"]
event_type = parsed_event_data["type"]

if event_type == "refetchConfig" || event_type == nil
@logger.debug("Re-fetching new config with TS: #{last_modified}")
fetch_config(min_last_modified: last_modified/1000)
fetch_config(min_last_modified: last_modified / 1000)
end
end
end
Expand Down

0 comments on commit 4fe0dce

Please sign in to comment.