Still partialy an OpenQuestion. How do I run background jobs in rails? All the doc I've found so far is for response based code e.g. user pushed button, Ruby bootstraps itself -> runs some code -> sends back a page. How can I just plain, run some code and, for example, change the escalation level on a "bug" record from "normal" to "too old" if a record ages too much? Alternately can I write a background job that'll, for example, spit out a nicely formatted text file every night at 2:00 AM? There are several things that might work, depending on your needs. h2. Forking with Ruby Ruby contains a fork method in the Kernel class that starts a background process. Using it in rails has been discussed: * http://one.textdrive.com/pipermail/rails/2005-March/004592.html * http://redhanded.hobix.com/inspect/daemonize.html I just tried this, and it seems to stall the engine (which is good in that it prevents race conditions, but it means forking isn't the right tool for long running tasks). Here's the code I used:

class HalController < ApplicationController
    def tick
        Process.detach fork {
            now = DateTime.now
            sleep 30
            Race.new { |r|
                r.system     = System.find_all.first
                r.start_date = now.to_s
                r.end_date   = DateTime.now.to_s
                }.save
            }
        render_text "tock #{DateTime.now}"
        end
    end
When several users go to hal/tick at once the race-condition test blocks are all consecutive (no overlap) but the GUIs return in sequence (e.g. several minutes later for the last ones). --[[MarkusQ]] h2. Threading with Ruby http://www.rubycentral.com/book/tut_threads.html It should be noted that Spinning off Thread's via Thread.new { code_block } will not work with apache. It will however work with lighttpd (reasons why?)
I've had the same problem with forking off new processes. Apache sets a limit on the amount of time a subprocess is allowed to run before returning the results of a CGI script. Since a rails page, any thread, or any subprocess is considered a child of Apache, it enforces these limits, killing off the children and returning an empty page to the client. Something more annoying is that it seems to wait to return any results until all subprocesses are complete. This is, of course, considered a security feature.
h2. Use cron or the like to run ruby code (a helper to your app). You could have a cron job launch your "escalate_bugs" script. For info regarding accessing your data from a ruby script see HowToPopulateYourDbFromScript and "how to access _your_ Rails environment from a script.":Environments#script h2. Use cron or the like to generate a "tick" to your rails app Or, if you'd rather handle it all through rails, you could have controller (call it "background" for example) and give it a method (say "check time") that you then trigger periodically from a cron job

* * * * * wget localhost:background/check_time
Note that the check_time method should actually check the time and not just "do its thing" everytime it is called, since it may well be triggered by anything from the BlackHatLadysAuxillaryAnnualDoSPicnic to TheGoogleSpider