Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to pass objects into a Resque job?

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?

like image 314
Christian Fazzini Avatar asked Dec 30 '11 15:12

Christian Fazzini


2 Answers

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.

like image 90
Sergio Tulentsev Avatar answered Jan 03 '23 21:01

Sergio Tulentsev


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)
like image 38
JustMe Avatar answered Jan 03 '23 20:01

JustMe