Reputation: 19723
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?
Upvotes: 7
Views: 2561
Reputation: 21894
The main disadvantage I can see, apart from passing big objects that will be serialized compared to ids, is that the objects most likely will get out of sync, since obviously, the job can be executed later.
You should definitely use ids in most cases.
Upvotes: 2
Reputation: 287
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)
Upvotes: 3
Reputation: 66505
Pass complete objects to resque can result in a heavy load in Redis as well as serialization overheads. This might not be a problem for you, but you have to keep this in mind.
Personally I prefer enqueuing IDs.
Upvotes: 0
Reputation: 230521
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.
Upvotes: 11