Handle Maintenance Tasks for Rails Apps without Cron
This is originally from tech.rufy.com/entry/15, but half the time I can't access his site so I'm storing it here. He usually has this code in the index actions of his site.It checks for an existing process and if it doesn't find one, spawns a new thread to handle maintenance tasks.
pid_file = '/tmp/running.pid'
begin
File.stat pid_file
rescue
f = File.new pid_file, 'w'
f < < fork {
loop {
# do lots of maintenance
sleep 15*60
}
}
f.close
end