Skip to content

Instantly share code, notes, and snippets.

@albus522
Forked from terrbear/dj-fork.rb
Created April 26, 2011 16:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albus522/942589 to your computer and use it in GitHub Desktop.
Save albus522/942589 to your computer and use it in GitHub Desktop.
def forked!
ActiveRecord::Base.connection.reconnect!
Rails.cache.instance_variable_get(:@data).reset
Mongoid.config.master.connection.close
load File.join(RAILS_ROOT, 'config/initializers/mongoid.rb')
end
def run(job)
runtime = Benchmark.realtime do
fork do
forked!
begin
Timeout.timeout(self.class.max_run_time.to_i) { job.invoke_job }
job.destroy
rescue Exception => e
handle_failed_job(job, e)
Kernel.exit!(1) # work failed
end
Kernel.exit!(0)
end
Process.wait
end
# TODO: warn if runtime > max_run_time ?
say "* [JOB] #{name} completed after %.4f" % runtime
$?.exitstatus == 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment