Is it safe to pass objects into a Resque job? For example:
Resque.enqueue(Foobar, @foo, @bar)
Instead of:
Resque.enqueue(Foobar, @foo.id, @bar.id)
Any disadvantage if I pass the object(s) in?
Resque github page says (https://github.com/defunkt/resque)
... your jobs must only accept arguments that can be JSON encoded.
Also, there's an effect you should take into consideration: object that you pass gets copied. Let's say, it's a record in a database. If later, when job will execute, this object is changed in the database, the job won't notice, it will operate on its own copy. Depending on your requirements, this may be desired behavior, or may be not.
If you pass an id
of that object instead, you can retrieve latest version of it in the job.
what I am doing is Marshal.dump(object)
and on the other side I do a Marshal.restore(object)
works like a charm and it is quick...
eg:
@chart = chart_factory.build(chart_config)
marshal_dump = Marshal.dump(@chart)
Resque.enqueue(ChartsWorker, marshal_dump, url)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With