Skip to content
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

Unable to save backup file. Exception thrown Errno::ENOENT #108

Open
skcc321 opened this issue Jun 7, 2022 · 4 comments
Open

Unable to save backup file. Exception thrown Errno::ENOENT #108

skcc321 opened this issue Jun 7, 2022 · 4 comments
Assignees
Labels
bug Something isn't working new

Comments

@skcc321
Copy link

skcc321 commented Jun 7, 2022

Describe the bug

Hi there, I face some issues when I use puma in clustered mode (following the documentation in readme)
But I would list here a bit more issues:

Steps to reproduce the bug

system: MacOS BigSur 11.4

setup unleash according to the readme for puma (clustered mode) https://github.com/Unleash/unleash-client-ruby/blob/c745ed4569b5e83498d22e0fbf618e8eb6ce2002/README.md#with-preload_app

  • initializer & puma.rb hook.
    bundle exec puma -w 3 --preload

Expected behavior

No errors should be thrown.

Logs, error output, etc.

...
[2022-06-07T12:06:38.866649+02:00          Unleash ERROR] : Unable to save backup file. Exception thrown Errno::ENOENT:'No such file or directory @ rb_file_s_rename - (/var/folders/pf/_39szp0x68v6s0vrr0jfz9l80000gn/T/unlea
sh-oh-backend-repo.json.tmp, /var/folders/pf/_39szp0x68v6s0vrr0jfz9l80000gn/T/unleash-oh-backend-repo.json)'                                                                                                                  
[2022-06-07T12:06:38.867553+02:00          Unleash ERROR] : stacktrace: ["/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/unleash-4.2.1/lib/unleash/toggle_fetcher.rb:76:in `rename'", "/Users/viktorsych/.rb
env/versions/3.0.2/lib/ruby/gems/3.0.0/gems/unleash-4.2.1/lib/unleash/toggle_fetcher.rb:76:in `block in save!'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/unleash-4.2.1/lib/unleash/toggle_fetcher.rb
:72:in `synchronize'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/unleash-4.2.1/lib/unleash/toggle_fetcher.rb:72:in `save!'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/unleash
-4.2.1/lib/unleash/toggle_fetcher.rb:63:in `fetch'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/unleash-4.2.1/lib/unleash/toggle_fetcher.rb:22:in `initialize'", "/Users/viktorsych/.rbenv/versions/3.0
.2/lib/ruby/gems/3.0.0/gems/unleash-4.2.1/lib/unleash/client.rb:21:in `new'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/unleash-4.2.1/lib/unleash/client.rb:21:in `initialize'", "/Users/viktorsych/Co
de/oh/overhaul-backend/lib/unleash.rb:19:in `new'", "/Users/viktorsych/Code/oh/overhaul-backend/lib/unleash.rb:19:in `preload'", "config/puma.rb:56:in `block in _load_from'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ru
by/gems/3.0.0/gems/puma-5.6.4/lib/puma/configuration.rb:297:in `block in run_hooks'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/puma-5.6.4/lib/puma/configuration.rb:295:in `each'", "/Users/viktorsyc
h/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/puma-5.6.4/lib/puma/configuration.rb:295:in `run_hooks'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/puma-5.6.4/lib/puma/cluster/worker.rb:55:in `run'
", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/puma-5.6.4/lib/puma/cluster.rb:204:in `worker'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/puma-5.6.4/lib/puma/cluster.rb:97:in `
block in spawn_worker'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/activesupport-6.1.5/lib/active_support/fork_tracker.rb:10:in `block in fork'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/ge
ms/3.0.0/gems/activesupport-6.1.5/lib/active_support/fork_tracker.rb:10:in `block in fork'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/activesupport-6.1.5/lib/active_support/fork_tracker.rb:8:in `fo
rk'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/activesupport-6.1.5/lib/active_support/fork_tracker.rb:8:in `fork'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/activesupport-6
.1.5/lib/active_support/fork_tracker.rb:27:in `fork'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/activesupport-6.1.5/lib/active_support/fork_tracker.rb:8:in `fork'", "/Users/viktorsych/.rbenv/versio
ns/3.0.2/lib/ruby/gems/3.0.0/gems/activesupport-6.1.5/lib/active_support/fork_tracker.rb:27:in `fork'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/puma-5.6.4/lib/puma/cluster.rb:97:in `spawn_worker'"
, "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/puma-5.6.4/lib/puma/cluster.rb:79:in `block in spawn_workers'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/puma-5.6.4/lib/puma/clus
ter.rb:72:in `times'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/puma-5.6.4/lib/puma/cluster.rb:72:in `spawn_workers'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/puma-5.6.4/l
ib/puma/cluster.rb:415:in `run'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/puma-5.6.4/lib/puma/launcher.rb:182:in `run'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/puma-5.6.
4/lib/puma/cli.rb:81:in `run'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/puma-5.6.4/bin/puma:10:in `<top (required)>'", "/Users/viktorsych/.rbenv/versions/3.0.2/bin/puma:23:in `load'", "/Users/vikt
orsych/.rbenv/versions/3.0.2/bin/puma:23:in `<top (required)>'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/bundler-2.2.32/lib/bundler/cli/exec.rb:58:in `load'", "/Users/viktorsych/.rbenv/versions/3.
0.2/lib/ruby/gems/3.0.0/gems/bundler-2.2.32/lib/bundler/cli/exec.rb:58:in `kernel_load'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/bundler-2.2.32/lib/bundler/cli/exec.rb:23:in `run'", "/Users/vikto
rsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/bundler-2.2.32/lib/bundler/cli.rb:478:in `exec'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/bundler-2.2.32/lib/bundler/vendor/thor/lib/thor/comma
nd.rb:27:in `run'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/bundler-2.2.32/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/rub
y/gems/3.0.0/gems/bundler-2.2.32/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/bundler-2.2.32/lib/bundler/cli.rb:31:in `dispatch'", "/Users/viktor
sych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/bundler-2.2.32/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/bundler-2.2.32/lib/bundler/cli.
rb:25:in `start'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/bundler-2.2.32/exe/bundle:49:in `block in <top (required)>'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/bundler-2
.2.32/lib/bundler/friendly_errors.rb:103:in `with_friendly_errors'", "/Users/viktorsych/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/bundler-2.2.32/exe/bundle:37:in `<top (required)>'", "/Users/viktorsych/.rbenv/versions
/3.0.2/bin/bundle:23:in `load'", "/Users/viktorsych/.rbenv/versions/3.0.2/bin/bundle:23:in `<main>'"]

Screenshots

No response

Additional context

No response

Unleash version

4.2.1

Subscription type

No response

Hosting type

No response

SDK information (language and version)

ruby, RoR, Sidekiq, Puma

@skcc321 skcc321 added bug Something isn't working new labels Jun 7, 2022
@skcc321
Copy link
Author

skcc321 commented Jun 7, 2022

Just to make everything work for puma clustered/single modes, sidekiq, rails console & other libs I did next config like a workaround:

in lib/unleash.rb

# frozen_string_literal: true

require "unleash"
require "socket"
require "securerandom"
require "forwardable"

module UNLEASH
  FakeClient = Struct.new(:turn_on) do
    def enabled?(*)
      turn_on
    end

    def if_enabled(*)
      yield if turn_on
    end

    def shutdown; end
    def shutdown!; end
  end

  class << self
    extend Forwardable

    def_delegators :client,
      :enabled?,
      :if_enabled,
      :get_variant,
      :shutdown,
      :shutdown!

    def preload
      @client = if ENV.fetch("UNLEASH_TOKEN", nil) && ENV.fetch("UNLEASH_URL", nil)
        Unleash::Client.new(
          app_name: "#{ENV.fetch("DD_SERVICE", nil)}-#{SecureRandom.uuid}",
          environment: ENV.fetch("DD_ENV", nil),
          instance_id: Socket.gethostname,
          url: ENV.fetch("UNLEASH_URL", nil),
          custom_http_headers: { Authorization: ENV.fetch("UNLEASH_TOKEN", nil) }
        )
      else
        FakeClient.new(false)
      end
    end

    def client
      @client || preload
    end
  end
end

in puma.rb

require_relative File.expand_path("../lib/unleash.rb", __dir__) # if not --preload

# unleash
on_worker_boot do
  UNLEASH.preload
end

on_worker_shutdown do
  UNLEASH.shutdown
end

in config/initializers/unleash.rb

require_relative File.expand_path("../../lib/unleash.rb", __dir__)

Rails.application.console do
  UNLEASH.preload
end

Sidekiq.configure_server do |config|
  config.on(:startup) do
    UNLEASH.preload
  end

  config.on(:shutdown) do
    UNLEASH.shutdown
  end
end

So now I can call UNLEASH.enabled?("some feature") in any place, and it will work (Unleash::Client will be initialized in a lazy way).
In puma, sidekiq, rails c I do preload the client to avoid some delay on the initial call.
in RSpec it is disabled by default because my test env does not have credentials ENV vars. I can allow(UNLEASH).to receive(:enabled?).with("some feature").and_return(true) if I want to make sure the test works well with enabled/disabled feature.

@skcc321
Copy link
Author

skcc321 commented Jun 7, 2022

But such configuration looks awful, so would be great to find some more convenient way of unleash client configuration.

@Tymek Tymek moved this from New to Todo in Issues and PRs Jun 9, 2022
@atraastrum
Copy link

atraastrum commented Apr 4, 2023

I also faced same issue with Unicorn server. Worker processes use same file for backup and throw error as posted by creator of this issue. The potential workaround is to set different backup file for each worker process:

after_worker_ready do |server, worker|
  app_name = Unleash.configuration.app_name
  Unleash.configuration.backup_file = Dir.tmpdir + "/unleash-#{app_name}-#{worker.nr}-repo.json"
  ::UNLEASH = Unleash::Client.new
end

@ganey
Copy link

ganey commented Feb 29, 2024

I also faced same issue with Unicorn server. Worker processes use same file for backup and throw error as posted by creator of this issue. The potential workaround is to set different backup file for each worker process:

after_worker_ready do |server, worker|
  app_name = Unleash.configuration.app_name
  Unleash.configuration.backup_file = Dir.tmpdir + "/unleash-#{app_name}-#{worker.nr}-repo.json"
  ::UNLEASH = Unleash::Client.new
end

Thanks for this, I've also got it working in the worker boot block

on_worker_boot do |index|
  Unleash.configuration.backup_file = Dir.tmpdir + "/unleash-#{index}-repo.json"
  Rails.configuration.unleash = Unleash::Client.new
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working new
Projects
None yet
Development

No branches or pull requests

4 participants