Code to be run on server start up

42 views
Skip to first unread message

Mike Witt

unread,
Oct 16, 2015, 12:11:47 PM10/16/15
to rubyonra...@googlegroups.com
In Rails 4.2, is there a place where I can put code that I want to be
called immediately after the rails server starts up (as opposed to when
an HTTP request comes in). In other words, I want my code executed
right after I do a "rails server" command or a "heroku restart."

As one simple example, suppose I wanted to maintain an item in the
database that would simply count system reloads. Where would that code
go?

Hassan Schroeder

unread,
Oct 16, 2015, 12:22:11 PM10/16/15
to rubyonrails-talk
On Fri, Oct 16, 2015 at 8:46 AM, Mike Witt <msg...@gmail.com> wrote:
> In Rails 4.2, is there a place where I can put code that I want to be called
> immediately after the rails server starts up

Initializers are called *during* startup; would that do?

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Mike Witt

unread,
Oct 16, 2015, 12:30:45 PM10/16/15
to rubyonra...@googlegroups.com
On 10/16/2015 09:21:44 AM, Hassan Schroeder wrote:
> On Fri, Oct 16, 2015 at 8:46 AM, Mike Witt <msg...@gmail.com> wrote:
> > In Rails 4.2, is there a place where I can put code that I want to
> be called
> > immediately after the rails server starts up
>
> Initializers are called *during* startup; would that do?

I'm not sure. Would I have access to the database, either through
ActiveRecords or ?

Pardon my ignorance here. I'm not sure where to find documentation
concerning the "transition" between getting all the framework
initialized and the "normal" rails stuff where you're just responding
to http requests one-by-one.

-Mike

Mike Witt

unread,
Oct 16, 2015, 1:42:28 PM10/16/15
to rubyonra...@googlegroups.com
On 10/16/2015 09:30:15 AM, Mike Witt wrote:
> On 10/16/2015 09:21:44 AM, Hassan Schroeder wrote:
>> On Fri, Oct 16, 2015 at 8:46 AM, Mike Witt <msg...@gmail.com> wrote:
>> > In Rails 4.2, is there a place where I can put code that I want to
>> be called
>> > immediately after the rails server starts up
>>
>> Initializers are called *during* startup; would that do?
>
> I'm not sure. Would I have access to the database, either through
> ActiveRecords or ?

OK, well it does appear to work. I created a file in
config/initializers and put my code in there. It seems to be running as
hoped for. At least in development mode. Thank you Hassan.

-Mike

Matt Jones

unread,
Oct 20, 2015, 10:13:43 AM10/20/15
to Ruby on Rails: Talk
As you've noted, config/initializers is the right place for this. For reference or general curiosity, there's a detailed guide to the Rails initialization process here:


--Matt Jones
Reply all
Reply to author
Forward
0 new messages