Rails 4.0 Sneak Peek: Asynchronous ActionMailer
reefpoints.dockyard.com
reefpoints.dockyard.com
But, why doesn't this let me pass objects to Mailer methods? I can't help but think passing ids and finding the object again on the other side is bad design.
Myself, over the past few years, instead of a job queue, I have been using a simple gem called spawn (https://github.com/tra/spawn) to fork processes to asynchronously send mail from Rails. It lets me pass objects around... I hope that this asynchronous ActionMailer gets this functionality as well!
The reason queueing systems like Resque and Sidekiq pass id's around (instead of objects) is to make sure that the asynchronous worker always has a fresh object straigh from the data store. I guess that's why Rails core has decided to implement it like this as well.
It is good design. Why? Because you don't want to pass mutable state (read: your user model) across thread/process boundaries if you can help it. What if the mailer modifies it? What if the caller modifies it while the mailer is working with it? It may no longer be valid. Rather than trying to reason about all possible states it could be in, it's easier just to make the job bootstrap itself with all the data it needs.
Confining instances to threads is far more sane than using mutexes and locks or relying on your runtime's GIL to watch out for you.
There of course may be other issues with marshaling. E.g., marshaling of procs is particularly tricky. But I've been marshaling with sidekiq for the past couple months and really haven't run into any problems. And it cleaned up the code a fair bit because I basically treat it as an RPC without any of the ceremonious DB lookups at the start of every async method.