Passing instance variables into delayed job

1.3k Views Asked by At

I'm trying to use delayed_jobs (background workers) to process my incoming email.

class EmailProcessor

  def initialize(email)
    @raw_html = email.raw_html
    @subject = email.subject
  end

  def process
    do something with @raw_html & @subject
  end
  handle_asynchronously :process, :priority => 20

end

The problem is I can't pass instance variables (@raw_html & @subject) into delayed jobs. Delayed jobs requests that I save data into the model to be retrieved in the background task, but I would prefer to have a background worker complete the entire task (including saving the record).

Any thoughts?

1

There are 1 best solutions below

1
Nic Nilov On

Use delay to pass params to a method that you want to run in the background:

class EmailProcessor

  def self.process(email)
    # do something with the email
  end
end

# Then somewhere down the line:

EmailProcessor.delay.process(email)