-
Notifications
You must be signed in to change notification settings - Fork 43
feat: write backup files when streaming #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'unleash/configuration' | ||
|
||
module Unleash | ||
class BackupFileWriter | ||
def self.save!(toggle_data) | ||
Unleash.logger.debug "Will save toggles to disk now" | ||
|
||
backup_file = Unleash.configuration.backup_file | ||
backup_file_tmp = "#{backup_file}.tmp" | ||
|
||
File.open(backup_file_tmp, "w") do |file| | ||
file.write(toggle_data) | ||
end | ||
File.rename(backup_file_tmp, backup_file) | ||
rescue StandardError => e | ||
# This is not really the end of the world. Swallowing the exception. | ||
Unleash.logger.error "Unable to save backup file. Exception thrown #{e.class}:'#{e}'" | ||
Unleash.logger.error "stacktrace: #{e.backtrace}" | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
RSpec.describe Unleash::StreamingEventProcessor do | ||
let(:engine) { YggdrasilEngine.new } | ||
let(:processor) { Unleash::StreamingEventProcessor.new(engine) } | ||
let(:backup_file) { Unleash.configuration.backup_file } | ||
|
||
before do | ||
Unleash.configure do |config| | ||
config.url = 'http://test-url/' | ||
config.app_name = 'test-app' | ||
end | ||
Unleash.logger = Unleash.configuration.logger | ||
end | ||
|
||
after do | ||
File.delete(backup_file) if File.exist?(backup_file) | ||
end | ||
|
||
class TestEvent | ||
attr_reader :type, :data | ||
|
||
def initialize(type, data) | ||
@type = type | ||
@data = data | ||
end | ||
end | ||
|
||
def feature_event(name, enabled = true) | ||
{ | ||
"events": [{ | ||
"type": "feature-updated", | ||
"eventId": 1, | ||
"feature": { | ||
"name": name, | ||
"enabled": enabled, | ||
"strategies": [{ "name": "default" }] | ||
} | ||
}] | ||
}.to_json | ||
end | ||
|
||
def backup_contains_feature?(name) | ||
return false unless File.exist?(backup_file) | ||
|
||
parsed = JSON.parse(File.read(backup_file)) | ||
feature_names = parsed['features'].map { |f| f['name'] } | ||
feature_names.include?(name) | ||
end | ||
|
||
describe '#process_event' do | ||
it 'processes valid events and saves full engine state' do | ||
event = TestEvent.new('unleash-updated', feature_event('test-feature')) | ||
processor.process_event(event) | ||
|
||
expect(engine.enabled?('test-feature', {})).to eq(true) | ||
expect(backup_contains_feature?('test-feature')).to eq(true) | ||
end | ||
|
||
it 'ignores unknown event types' do | ||
event = TestEvent.new('unknown-event', feature_event('test-feature')) | ||
processor.process_event(event) | ||
|
||
expect(File.exist?(backup_file)).to eq(false) | ||
expect(engine.enabled?('test-feature', {})).to be_falsy | ||
end | ||
|
||
it 'saves full engine state, not partial event data' do | ||
processor.process_event(TestEvent.new('unleash-updated', feature_event('first-feature', true))) | ||
processor.process_event(TestEvent.new('unleash-updated', feature_event('second-feature', false))) | ||
|
||
expect(backup_contains_feature?('first-feature')).to eq(true) | ||
expect(backup_contains_feature?('second-feature')).to eq(true) | ||
end | ||
|
||
it 'handles invalid JSON gracefully without creating backup' do | ||
event = TestEvent.new('unleash-updated', 'invalid json') | ||
|
||
expect { processor.process_event(event) }.not_to raise_error | ||
expect(File.exist?(backup_file)).to eq(false) | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extracted from toggle_fetcher so that we can share it between polling and streaming
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bikeshedding: This might be better served / mode idiomatic by using MixIns in Ruby.
Here is an example:
https://blog.appsignal.com/2021/01/13/using-mixins-and-modules-in-your-ruby-on-rails-application.html
or
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Questions I asked myself:
It lead me to the current solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
I think this solution is fine honestly. If I were going to fuss, it'd be in the direction of making this explicitly stateless rather than the tacit connection that a MixIn brings here. I vote we leave it alone, I think it's a nice improvement to the code structure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your points make sense! Go for it!