Custom models not included in Stalker worker
-
16-06-2021 - |
Question
I'm trying to get Stalker working with Beanstalkd in my Ramaze application. I can enqueue jobs no problem, but my worker doesn't recognize my custom models. Here is my jobs.rb:
require 'stalker' include Stalker BEANSTALK_URL = '0.0.0.0:11300' job 'award_badges' do |args| Badge.award_badges(args[:user_id], args[:badge_category]) end
I run the jobs.rb with stalker jobs.rb and always receive the error: Exception NameError -> uninitialized constant Badge
In all the stalker examples no libraries are included, how does Stalker know about those classes?
Solution
This is because you're not loading your enviroment with your jobs, here is a well formatted jobs.rb:
require File.expand_path("../environment", __FILE__)
require 'stalker'
include Stalker
job "my_job" do |args|
# Do something here...
end
As you can see in the first three lines we first require our rails enviroment and then stalker, so it can be aware about your models and stuff.
Cheers!