Code Monkey home page Code Monkey logo

Comments (4)

atraastrum avatar atraastrum commented on June 19, 2024 1

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

from unleash-client-ruby.

skcc321 avatar skcc321 commented on June 19, 2024

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.

from unleash-client-ruby.

skcc321 avatar skcc321 commented on June 19, 2024

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

from unleash-client-ruby.

ganey avatar ganey commented on June 19, 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

from unleash-client-ruby.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.