Skip to content

Commit

Permalink
Add possibility to shutdown the client in a clean fashion.
Browse files Browse the repository at this point in the history
Not tested throughly.
  • Loading branch information
rarruda committed Jan 4, 2019
1 parent 5753b4a commit 532842a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
6 changes: 6 additions & 0 deletions lib/unleash/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ def is_enabled?(feature, context = nil, default_value = false)
return toggle_result
end

def shutdown
Thread.kill(Unleash.reporter.scheduled_executor.thread)
Thread.kill(Unleash.toggle_fetcher.scheduled_executor.thread)
Unleash.reporter.send unless Unleash.configuration.disable_metrics
end

private
def info
return {
Expand Down
8 changes: 5 additions & 3 deletions lib/unleash/metrics_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
module Unleash

class MetricsReporter
attr_accessor :last_time, :client
attr_accessor :last_time, :client, :scheduled_executor

def initialize
self.last_time = Time.now
end

def build_hash
self.scheduled_executor = Unleash::ScheduledExecutor.new('MetricsReporter', Unleash.configuration.metrics_interval)
self.scheduled_executor.run do
Unleash.reporter.send
end
end

def generate_report
Expand Down
7 changes: 4 additions & 3 deletions lib/unleash/scheduled_executor.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
module Unleash

class ScheduledExecutor
attr_accessor :name, :interval, :max_exceptions, :retry_count
attr_accessor :name, :interval, :max_exceptions, :retry_count, :thread

def initialize(name, interval, max_exceptions = 5)
self.name = name || ''
self.interval = interval
self.max_exceptions = max_exceptions
self.retry_count = 0
self.thread = nil
end

def run(&blk)
thread = Thread.new do
self.thread = Thread.new do
Thread.current[:name] = self.name

loop do
Expand All @@ -33,7 +34,7 @@ def run(&blk)
break
end
end
Unleash.logger.info "thread #{name} ended"
Unleash.logger.warn "thread #{name} ended"
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/unleash/toggle_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
module Unleash

class ToggleFetcher
attr_accessor :toggle_cache, :toggle_lock, :toggle_resource, :etag, :retry_count
attr_accessor :toggle_cache, :toggle_lock, :toggle_resource, :etag, :retry_count, :scheduled_executor

def initialize
self.etag = nil
self.toggle_cache = nil
self.toggle_lock = Mutex.new
self.toggle_resource = ConditionVariable.new
self.retry_count = 0
self.scheduled_executor = nil

# start by fetching synchronously, and failing back to reading the backup file.
begin
Expand All @@ -25,8 +26,8 @@ def initialize
end

# once we have initialized, start the fetcher loop
scheduledExecutor = Unleash::ScheduledExecutor.new('ToggleFetcher', Unleash.configuration.refresh_interval)
scheduledExecutor.run { remote_toggles = fetch() }
self.scheduled_executor = Unleash::ScheduledExecutor.new('ToggleFetcher', Unleash.configuration.refresh_interval)
self.scheduled_executor.run { remote_toggles = fetch() }
end

def toggles
Expand Down

0 comments on commit 532842a

Please sign in to comment.