2

I'm working on a Rails app that needs to execute some code when it boots. Since I want to share this behavior with future apps, I have put this code into a gem using a Railtie class.

lib/my_gem/railtie.rb

module MyGem
  class Railtie < Rails::Railtie
    initializer 'my_gem.registration' do
      # the code goes here
    end
  end
end

In this way, the initializer is executed by each worker (I'm using Pow in development with the 2 default workers and I guess it will be the same in production with unicorn and multiple workers) and this messes up the entire app (in the initializer I set an ENV variable and this is overwritten by each worker).

Is there a way to make the code be executed only by the 1 (the first) worker ???

Thanks for your help and have a nice day.

Community
  • 1
  • 1
Sig
  • 5,476
  • 10
  • 49
  • 89
  • 1
    If anyone is having the same issue I have solved moving my code from the initializer into a rake task and running it (from Capistrano) before booting up the app. In this way my ENV variable is set just once. – Sig Aug 22 '13 at 08:53

0 Answers0