简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM