4

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?

echan00
  • 2,788
  • 2
  • 18
  • 35

1 Answers1

0

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)
Nic Nilov
  • 5,056
  • 2
  • 22
  • 37
  • 5
    This doesn't solve the problem, I would still be trying to pass 'email' variable into the delayed jobs – echan00 Jul 28 '16 at 22:32