Search code examples
jobsruby-on-rails-5rake-taskbeanstalkd

Backburner rake task - NameError: undefined local variable or method


I'm starting up with Backburner to run some jobs in the background of my app. I've tried following the documentation under "Working Jobs", but I'm clearly doing something very obvious and very incorrect.

Gemfile:

gem 'backburner'

Rakefile:

require_relative 'config/application'
require 'backburner/tasks'

Rails.application.load_tasks

And then in the rails console:

rake backburner:work

(which I think will just run all pending, enqueued backburner jobs.)

But all I get is the error: NameError: undefined local variable or method 'work' for main:Object from (pry):1:in '__pry__'

I sorta know what pry does (I use it to throw binding.pry into my code to debug, but that's about it), but I have no idea how it might be causing this. Not to mention I'm still pretty weak on the whole jobs and rake task front in general.

Any help would be much appreciated!


Solution

  • Apparently I'm in the business of asking dumb questions, lately. I was passing a variable to my Backburner.enqueue that is an ID, so I expected it to be numerical, like so: @admin.id. This would have been fine if @admin was defined in the scope of the helper I'm calling my enqueue in, but I'm actually passing that variable to that helper method...which means @admin is not defined, which, guess what Daniel, means @admin.id is undefined. Which means that I'm hitting the job anyways (because no, Backburner is in fact NOT breaking), but I'm not getting the job enqueued the way I thought I was because I was passing an invalid property to it.

    So it sort of turns out that the error I got looked completely unrelated to the issue, but there it is. Always pass the variables your methods are expecting, or else expect errors.

    EDIT:
    Also, I can't downvote my own post. I tried. It deserves it.

    EDIT 2:
    Turns out it's really hard/not supported to get beanstalkd going on Heroku. Der. Also should have checked that. I just liked how beanstalkd was handling some things and went with it before getting to deploy and realizing it was a no-go.

    Instead, I went with Resque as suggested by Heroku. They also support Delayed Job (DJ), but noted it is pretty heavy and Resque is better.

    All that to say, I finally got to where I needed to go with Resque, and because it was such a thing I made a little documentation to help me (and maybe any other sad soul who stumbles across this lame post) get background workers running on a Heroku-hosted Rails app in the future. Be warned: it really is a guide for dummies like me, since I had to puzzle out a lot of what now is fairly obvious to me. There are gaps in the Resque documentation that I've included, which really just boils down to being explicit about what certain things mean, and where things go.