WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 72cbf06

Browse files
committed
Fix errors when writing to backup file from different processes
1 parent bf943d4 commit 72cbf06

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/unleash/backup_file_writer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def self.save!(toggle_data)
66
Unleash.logger.debug "Will save toggles to disk now"
77

88
backup_file = Unleash.configuration.backup_file
9-
backup_file_tmp = "#{backup_file}.tmp"
9+
backup_file_tmp = "#{backup_file}.tmp-#{Process.pid}"
1010

1111
File.open(backup_file_tmp, "w") do |file|
1212
file.write(toggle_data)

spec/unleash/toggle_fetcher_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@
6565
backup_file = Unleash.configuration.backup_file
6666
expect(File.exist?(backup_file)).to eq(true)
6767
end
68+
69+
# NOTE: this is a frequent issue with Puma (in clustered mode). See Unleash/unleash-ruby-sdk#108
70+
it "does not log errors due to concurrent forked processes" do
71+
log_file = ->(pid) { File.join(Dir.tmpdir, "#{pid}.log") }
72+
pids = Array.new(10) do
73+
fork do
74+
Unleash.logger = Logger.new(log_file.call(Process.pid))
75+
expect(Unleash.logger).not_to receive(:error)
76+
described_class.new engine
77+
end
78+
end
79+
80+
pids.each do |pid|
81+
Process.wait(pid)
82+
process_status = $? # rubocop:disable Style/SpecialGlobalVars
83+
error_log_file = log_file.call(pid)
84+
expect(File.exist?(error_log_file))
85+
86+
error_msg = "Process #{pid} failed with errors:\n#{File.read(error_log_file).lines[1..].join}"
87+
expect(process_status).to be_success, error_msg
88+
end
89+
end
6890
end
6991
end
7092

0 commit comments

Comments
 (0)