Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 718
user3437721 Avatar asked Nov 25 '25 02:11

user3437721


1 Answers

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

like image 133
j-dexx Avatar answered Nov 26 '25 16:11

j-dexx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!