Ruby Equivalent of ASP.NETs Application_Start?

Go To


I'd like to kick off a thread that wakes up every so many seconds to do some processing within a Ruby-on-Rails web app. In .NET, the logical place would have been to create a thread in the global.asax file and start it in Application_Start.

Is there a similar paradigm for doing such things in RoR? Would this be done in the ApplicationController?

As an aside: am I entering the wrong search strings or are search engines really bad at finding information for building RoR apps?

2009-06-16 13:47
by jerhinesmith


Rails doesn't really have an equivalent. The Whenever RubyGem is a nice abstraction of Cron that lets you use syntax like the example below from within your Rails application:

every 30.seconds do
  # Do something interesting...

Some resources you may find useful:

2009-06-16 14:03
by John Topley


Search engines are not only bad, but almost the ONLY place you'll find information for RoR. :(

I think the paradigm you're looking for is a cron job, though that presumes you're running under Linux. What I've seen most often is essentially a "scheduled task" that pulls down a page every X y's and the processing is done within that request. You can even do this remotely by having your scheduled task point to the remote box's cron "hit point".

You could put the code in ApplicationController and have it check to see if the job was accomplished in the last X y's, but the problem is that you could be starved for requests for longer than the intended interval.

Good luck!

2009-06-16 13:58
by Stefan Mai
I thought about using a cron job, but I'd like to keep everything self-contained within the webapp if possible - jerhinesmith 2009-06-16 14:00
"Search engines are not only bad, but almost the ONLY place you'll find information for RoR" - There are numerous books on RoR, a full API and a new wiki system. It's not as bad as 2 years ag - marcgg 2009-06-16 15:02
@marcgg Maybe. Yes if you know what you're doing and are relatively experienced. But if you're new to rails a lot of those books are out of date, and getting up to date with "the rails way" is difficult to learn cohesively. Or at least that's been my experience - Stefan Mai 2009-06-16 16:21


There isn't a pat answer to this question because it depends heavily on your specific requirements. Certainly, however, there isn't a direct equivalent to the concept you describe in ASP.NET However, you'll want to take a look at Background Processing on Rails as a great starting point.

2009-06-16 14:06
by John Feminella


A couple of years ago I tried daemon_generator but for some reason now forgotten I never ended up using it.

2009-06-16 14:13
by Jonas Elfström