user3437721
user3437721

Reputation: 2289

Rails - after_initialize or initializer

Whenever my Rails 4 application loads after a deployment, I want to make a REST APi call to another application to tell it my app is ready and kick off some tests.

I was initially going to do this in the initialzers folder, but then I figured that it executes this stuff while the app is initializing and the app may not be fully initialized, so if it kicks off some tests, it may fail.

Then I read about the after_initialize, but I'm not sure how to use it and where the code should go to make the rest call after the app is fully loaded?

Can some one help?

Here is the code I want to run when the app has loaded:

if Rails.env.dev?
   response = HTTParty.post(the_rest_url,
                  {
                    :basic_auth => @auth
                  })
    puts response.message

Upvotes: 3

Views: 6484

Answers (1)

j-dexx
j-dexx

Reputation: 10406

I would put it in config/environments/development.rb as you want it to run in development.

It's outlined in the guides

config.after_initialize do
  response = HTTParty.post(the_rest_url,
                  {
                    :basic_auth => @auth
                  })
    puts response.message
end

Note that this block will be run for rake tasks

Upvotes: 2

Related Questions