From 988bd61c415833e6d04d73270393aa50b7ea2ec5 Mon Sep 17 00:00:00 2001 From: budi Date: Mon, 27 Aug 2018 20:14:53 +0700 Subject: [PATCH] add notify --- lib/toggleable/configuration.rb | 1 + lib/toggleable/feature_toggler.rb | 17 ++++++++++++--- spec/class_initializer.rb | 1 + spec/toggleable/feature_toggler_spec.rb | 29 ++++++------------------- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/lib/toggleable/configuration.rb b/lib/toggleable/configuration.rb index e885b70..fbdef4f 100644 --- a/lib/toggleable/configuration.rb +++ b/lib/toggleable/configuration.rb @@ -7,6 +7,7 @@ class Configuration attr_accessor :palanca_host attr_accessor :palanca_user attr_accessor :palanca_password + attr_accessor :notify_endpoint ## optional for notify changes attr_accessor :storage ## storage used. default: memory store attr_accessor :namespace ## required for prefixing the keys. default: `toggleable`` attr_accessor :logger ## optional, it will not log if not configured. diff --git a/lib/toggleable/feature_toggler.rb b/lib/toggleable/feature_toggler.rb index 7b09131..a252cc0 100644 --- a/lib/toggleable/feature_toggler.rb +++ b/lib/toggleable/feature_toggler.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: true +# frozen_string_literal: false require 'singleton' require 'rest-client' @@ -121,11 +121,22 @@ def read_key_expired?(key) end def log_changes(mapping, actor) - previous_values = available_features + toggles = '' + values = '' mapping.each do |key, val| - next if previous_values[key].to_s == val.to_s Toggleable.configuration.logger.log(key: key, value: val, actor: actor) + toggles.concat("#{key},") + values.concat("#{val},") end + + notify_changes(toggles[0...-1], values[0...-1]) if Toggleable.configuration.notify_endpoint + end + + def notify_changes(toggles, values) + url = "#{Toggleable.configuration.notify_endpoint}/notify_toggle?keys=#{toggles}&values=#{values}" + RestClient::Resource.new(url).get timeout: 2, open_timeout: 1 + rescue StandardError + nil end end end diff --git a/spec/class_initializer.rb b/spec/class_initializer.rb index 4804655..71b230c 100644 --- a/spec/class_initializer.rb +++ b/spec/class_initializer.rb @@ -25,5 +25,6 @@ def log(key:, value:, actor:) Toggleable.configure do |t| t.logger = logger t.palanca_host = 'localhost:8027' + t.notify_endpoint = 'localhost:5858' t.use_memoization = false end diff --git a/spec/toggleable/feature_toggler_spec.rb b/spec/toggleable/feature_toggler_spec.rb index bb2e2c6..d5fbabf 100644 --- a/spec/toggleable/feature_toggler_spec.rb +++ b/spec/toggleable/feature_toggler_spec.rb @@ -81,19 +81,12 @@ before do stub_request(:post, "http://localhost:8027/_internal/toggle-features/bulk-update").to_return(status: 200, body: 'success') + stub_request(:get, "http://localhost:5858/notify_toggle?keys=other_key&values=true").to_return(status: 200, body: 'success') end describe '#mass_toggle! with memory store' do - let(:mapping_before) { - { - 'key' => 'true', - 'other_key' => 'false' - } - } - let(:mapping_after) { { - 'key' => 'true', 'other_key' => 'true' } } @@ -103,7 +96,6 @@ before do subject.register('key') subject.register('other_key') - allow(subject).to receive(:available_features).and_return(mapping_before) end it do @@ -113,30 +105,23 @@ end describe '#mass_toggle! with redis' do - before do - allow(Toggleable.configuration).to receive(:storage).and_return(redis_storage) - end - - let(:mapping_before) { - { - 'key' => 'true', - 'other_key' => 'false' - } - } - let(:mapping_after) { { - 'key' => 'true', 'other_key' => 'true' } } let(:actor_id) { 1 } + before do + allow(Toggleable.configuration).to receive(:storage).and_return(redis_storage) + allow_any_instance_of(RestClient::Resource).to receive(:get).and_raise(StandardError) + end + + before do subject.register('key') subject.register('other_key') - allow(subject).to receive(:available_features).and_return(mapping_before) end it do