How to run my ruby code after Rails server start?
Asked Answered
B

5

31

I tried:

after_initialize do
  #code
end

But: (documentation)

Some parts of your application, notably observers and routing, are not yet set up at the point where the after_initialize block is called.

I need routing and logger in my code

Any ideas?

Bailment answered 17/12, 2011 at 9:45 Comment(4)
could you state your problem more explicitly.Cadwell
I want on_server_start event.Bailment
Why would you want that? What do you want to do? Explain your problem and maybe there is a better solution than an "on_server_start" event.Parkman
@MaurícioLinhares Here's an example: I want a STOMP subscriber that would start after the server has started and not when the console is running.Miniskirt
D
35

See section 3.1 from http://guides.rubyonrails.org/configuring.html

I believe you would put this code in config/application.rb

config.after_initialize do
    # ....
end

# config.after_initialize takes a block which will be run after Rails has finished initializing the application. 
# That includes the initialization of the framework itself

Also http://guides.rubyonrails.org/initialization.html

Deplume answered 17/12, 2011 at 17:13 Comment(3)
This also runs when the Rails console (rails c) is invoked.Vaughnvaught
...and on rake-tasks, that depend on rails. Is there a dependable mechanism to get notified only when the server has started up?!Tupelo
can we also have config.shut_down .. something like that? when rails server stop.Crunode
C
18

@house9's answer is correct, as pointed out by the comments, this will also execute when running rake tasks, console, etc. I used the following to recognize when a server was actually being executed:

# application.rb
if defined?(Rails::Server)
  config.after_initialize do
    # Do stuff here
  end
end
Clanton answered 8/6, 2017 at 16:32 Comment(2)
This only works if started with rails server if started directly with puma, it does not... (and Puma::Server is always defined)Groundwork
Also verified this does not work with Passenger (so likely not any server in production).Fresnel
F
5

Lines added to config.ru will be run by the Rails server, but not by Rails console or Rake tasks that load the environment.

# config.ru
# This file is used by Rack-based servers to start the application.

require ::File.expand_path("../config/environment", __FILE__)
# your code here (after environment is loaded)
run Rails.application
Fresnel answered 8/1, 2020 at 23:44 Comment(0)
S
4

Another option is to create a custom initializer. It's just a ruby file that lives under config/initializers/ and is executed exactly "on_server_start" event :)

Secateurs answered 2/1, 2012 at 17:38 Comment(1)
As with @house9's response this runs in rails console and rake tasks that load the rails environment load.Fresnel
G
3

Since Rails 5 the default server is Puma, so code in config/puma.rb will be run just once, and only if the server is started.

Groundwork answered 2/8, 2019 at 11:22 Comment(1)
This works wonderfully for me, unlike the other solutions. ThanksSeethrough

© 2022 - 2024 — McMap. All rights reserved.