title: rails periodic task
body: <p>I have a ruby on rails app in which I'm trying to find a way to run some code every few seconds.</p>
  
  <p>I've found lots of info and ideas using cron, or cron-like implementations, but these are only accurate down to the minute, and/or require external tools. I want to kick the task off every 15 seconds or so, and I want it to be entirely self contained within the application (if the app stops, the tasks stop, and no external setup).</p>
  
  <p>This is being used for background generation of cache data. Every few seconds, the task will assemble some data, and then store it in a cache which gets used by all the client requests. The task is pretty slow, so it needs to run in the background and not block client requests.</p>
  
  <p>I'm fairly new to ruby, but have a strong perl background, and the way I'd solve this there would be to create an interval timer &amp; handler which forks, runs the code, and then exits when done.<br>
  It might be even nicer to just simulate a client request and have the rails controller fork itself. This way I could kick off the task by hitting the URI for it (though since the task will be running every few seconds, I doubt I'll ever need to, but might have future use). Though it would be trivial to just have the controller call whatever method is being called by the periodic task scheduler (once I have one).</p>