lostboy / workless

Extension for Delayed Job to only run workers when needed on Heroku
MIT License
374 stars 80 forks source link

failed_at is not in some delayed_job documents (mongoid) #84

Open tgmerritt opened 9 years ago

tgmerritt commented 9 years ago

Reading through the source of Workless, you check for failed_at attribute of the backend table:

if Rails.version >= "3.0.0"
   Delayed::Job.where(:failed_at => nil)
else
   Delayed::Job.all(:conditions => { :failed_at => nil })
end

But unfortunately when we're using the delayed_job_mongoid gem (which is where I think the problem is ultimately) we don't get this field. Here is an example document from my db:

{
    "_id": {
        "$oid": "54f092fa0bc40e34ff000004"
    },
    "priority": 0,
    "attempts": 0,
    "handler": "--- !ruby/struct:AlertTechBySMSJob\ntid: !ruby/object:Moped::BSON::ObjectId\n  raw_data: !binary |-\n    UgRkwNF5jQACAAB2\njid: !ruby/object:Moped::BSON::ObjectId\n  raw_data: !binary |-\n    VPCS+gvEDjT/AAAC\n",
    "queue": "default",
    "run_at": {
        "$date": "2015-02-27T15:53:30.307Z"
    },
    "updated_at": {
        "$date": "2015-02-27T15:53:30.307Z"
    },
    "created_at": {
        "$date": "2015-02-27T15:53:30.307Z"
    }
}

Therefore there are always no jobs return - so workless never fires.

I'm submitting the issue for documentation to others who might be searching, as well as to alert the team to consider alternative attributes to pivot on.

Because Workless limits attempts to 3, I think it might be better to pivot on this field.

tgmerritt commented 9 years ago

I patched my own version of delayed_job to shove this attribute into the record on enqueue - and the attribute is now present but workless is still not activating on my staging environment.

Yes I've set my APP_NAME and HEROKU_API_KEY env vars on Heroku - it seems the after_create callback just isn't firing when the record is created. This is specific to MongoID as my current production environment uses MongoMapper and Workless is working just fine. We're migrating over to MongoID and the problems have arisen from that.

Gemfile.lock snippets:

workless (1.1.3)
      delayed_job (>= 2.0.7)
      heroku-api
      rails
      rush
delayed_job (4.0.6)
      activesupport (>= 3.0, < 5.0)
    delayed_job_mongoid (2.1.0)
      delayed_job (>= 3.0, < 5)
      mongoid (>= 3.0, < 5)
    delayed_job_tgmerritt (4.0.7)
      activesupport (>= 3.0, < 5.0)