Timothy Andrew

Read more by Timothy here

YAML Serialization for Delayed Job

When we first moved excel generation off to a delayed job on survey-web, we had code that looked like this:

1
2
responses = Response.where(:foo => bar)
Delayed::Job.enqueue(MyCustomJob.new(responses))

And this would bomb with an error like Can't dump anonymous Module. After some time getting nowhere, we solved it like this:

1
2
response_ids = Response.where(:foo => bar).map(&:id)
Delayed::Job.enqueue(MyCustomJob.new(response_ids))

And in the job:

1
responses = Response.where('id in (?)', response_ids)

While refactoring a lot of that code over the last few days, we ran into the same issue. But with one difference. A controller spec was failing, but a test for the job which also passed a bunch of responses into it passed.

We wondered if maybe it was because we were passing a relation into the job instead of an array.

So we tried:

1
2
responses = Response.where(:foo => bar).all
Delayed::Job.enqueue(MyCustomJob.new(responses))

And that worked great.

(The files in question are here and here).