Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resque Worker: invalid operator: $oid

A worker that was working a few days ago has ceased to work for some reason.

The resque log reports an exception of Mongo::OperationFailure with the errorinvalid operator: $oid

class SimilarTargets
  @queue = :similar_queue

  def self.perform(target_id)
    source_target = Target.find(target_id)

    ....

  end
end

The worker is failing on Target.find(target_id), even when a straight string is passed in via the rails console.

Target.find(id) works fine in the console and elsewhere in the code and I can't figure out why this has failing even though the worker class has never changed in the last week.

like image 451
w2bro Avatar asked Jul 17 '12 04:07

w2bro


2 Answers

Did you upgrade Mongoid recently? The error makes it sound like the .find() method is receiving something like {"$oid": "[STRING]"}, which is the strict json representation of an Object ID for Mongo.

You can get around it with something like this, assuming you just want a quick fix:

target_id = target_id["$oid"] unless target_id.is_a?(String)
like image 124
MrKurt Avatar answered Oct 05 '22 09:10

MrKurt


Another option is to ensure that when you enqueue a Moped::BSON::ObjectId you explicitly pass in a string representation. E.g.,

Resque.enqueue(MyJob, @mongoid_instance.class.to_s, @mongoid_instance.id.to_s)
like image 21
Jonathan R. Wallace Avatar answered Oct 05 '22 09:10

Jonathan R. Wallace