Tutorial :Ruby Equivalent of ASP.NETs Application_Start?



Question:

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?


Solution:1

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...  end  

Some resources you may find useful:


Solution:2

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!


Solution:3

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.


Solution:4

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »