ruby on rails - Can I send delayed_job an in-memory only ActiveRecord object? -
so had emails didn't go out due mailing service api key not being instantiated. however, detail model objects preserved in database. don't want re-create these in database.
i wrote rake task send out emails , easier try , create temporary in-memory objects rather try find correct activerecord objects based on in detail_params. send out emails using detailmailerjob , pass in instantiated detail object.
temp_obj = detail.new(detail_params) detailmailerjob.new.delay.notify_job(temp_obj)
but i'm noticing following error in delayed::job.all queue after running rake task:
last_error: "activerecord::recordnotfound
does mean way me pass in detail object detailmailerjob first find instantiated record in database? (i.e., no in-memory objects)
edit: here detailmailer & detailmailerjob class.
class detailmailerjob def notify_job(detail) detailmailer.notify_job(detail).deliver end end class detailmailer < actionmailer::base def notify_job(detail) @detail = detail @emailed_to = detail.emailed_to.join(", ") mail(to: detail.emailed_to, subject: "#{detail.full_name} - new message") end end
i have run this, too. it's optimization of dj use saved versions of subclasses of activerecord::base
rather persisting full contents again in dj queue. i'm sure because instances of ar can large.
if define new class same fields of interest detail
not inheriting activerecord::base
, . like:
class detailasaplainoldrubyobject attr_accessor :field_a, :field_b, ... def initialize(params) self.field_a = params['field_a'] self.field_b = params['field_b'] ... end end
now start job instance:
temp_obj = detailasaplainoldrubyobject.new(detail_params) detailmailerjob.new.delay.notify_job(temp_obj)
more details: delayed job - in particular method delay
- works storing class of self
(in example detailmailerjob
), name of method (notify_job
), , serialized representation of argument in database table. worker processes dequeue records table, deserialize argument, , call method on class.
the problem serialization. dj augments yaml special serializer activerecord encodes table name , primary key. deserializing results in find
on table. suppose they're doing save database space , time. of course trick works if activerecord
has been saved. (imo cool if dj skipped tricky serialization if record hasn't been saved, alas doesn't.)
note error message in code @ link above! it's used in exception raised when find
fails.
by using plain object no activerecord
base, ought normal yaml serialization string.
Comments
Post a Comment