delayed_job_mongoid icon indicating copy to clipboard operation
delayed_job_mongoid copied to clipboard

Worker doesn't pickup jobs created by a mongoid embedded document

Open jafuentest opened this issue 6 years ago • 1 comments

As the title says, you can't use the handle_asynchronously option inside an object that will be persisted as a subdocument.

In the example below, every time you create a comment, the job will be created, but when the worker picks it up for execution, nothing will happen because calling comment.find(:id) will return nil

class Post
  include Mongoid::Document
  embeds_many :comments
end

class Comment
  include Mongoid::Document
  embedded_in :post

  after_create :notify_post_author

  def notify_post_author
    Notification.create(user: post.user, text: 'you got a comment')
  end

  handle_asynchronously :notify_post_author, queue: 'notifications'
end

This is referenced in Issue https://github.com/collectiveidea/delayed_job_mongoid/issues/11 however it leads to another gem rather than a fix, so I'm curious as to why this wasn't addressed by this gem

jafuentest avatar Aug 13 '19 00:08 jafuentest

This is indeed an issue and I experienced this in my own app. The problem is that DelayedJob's class serialization is treating the embedded document as if it were a parent-level document, and looks for its collection.

Instead, we need to serialize the parent document, and then "drill-down" to the relevant embedded document -- potentially multiple levels of nesting.

I will consider a fix for this later.

johnnyshields avatar Apr 01 '22 16:04 johnnyshields