Skip to content

Instantly share code, notes, and snippets.

@brandon-beacher
Created October 26, 2011 19:45
Show Gist options
  • Save brandon-beacher/1317570 to your computer and use it in GitHub Desktop.
Save brandon-beacher/1317570 to your computer and use it in GitHub Desktop.
class Job < ActiveRecord::Base
serialize :data
serialize :exception
named_scope :current_customer, lambda { { :conditions => { :customer_id => Customer.current.id } } }
def self.spawn(person_id, data)
customer = Customer.current
job = create!(:person_id => person_id, :data => data)
job.spawn do
begin
job.run
rescue Exception => exception
job.update_attribute(:exception, :class => exception.class.to_s, :message => exception.message, :backtrace => exception.backtrace.join("\n"))
ensure
job.update_attribute(:running, false)
end
end
job
end
private
def update_data(hash)
update_attribute(:data, data.merge(hash))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment