Skip to content

Instantly share code, notes, and snippets.

@julik
Created September 21, 2009 08:54
Show Gist options
  • Save julik/190142 to your computer and use it in GitHub Desktop.
Save julik/190142 to your computer and use it in GitHub Desktop.
require File.dirname(__FILE__) + "/../lib/init"
# Log on my own
ActiveRecord::Base.logger = Logger.new(TW_ROOT + "/log/worker.log")
File.open(WORKER_PIDFILE, "w") {|f| f << Process.pid }
begin
loop do
# We might have lost the connection when we forked, so reconnect
ActiveRecord::Base.verify_active_connections!
job = Job.find_first_pending
# Process the actual job in the child,
# so that any memory that gets overconsumed will be freed
# upon exit independently of the Ruby GC. We can easily do this
# since we don't need to receive any info back from the processor
if job
child_pid = fork do
# Ensure all sockets are reinstated in the child process
MessageBus.reconnect!
ActiveRecord::Base.verify_active_connections!
# Run the job
job.exec!
end
end
sleep(0.4) # Do not torture the DB all the time
end
ensure
File.unlink(WORKER_PIDFILE)
Process.waitall
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment