我正在使用sidekiq和redis设置后台任务。它在开发中是否可以正常工作,但是在暂存站点上进行测试时,我得到了:
NameError:未初始化的常量InviteWorker :: User
/controllers/users_controller.rb
class UsersController < ApplicationController
def invite
params[:user] ? @user = User.where(:referral_code => params[:user])[0] : @user = User.find_by_email(cookies[:h_email])
user_id = @user.id
content = params[:body]
emails = params[:email].split(',')
total_emails = emails.length
user_email = @user.email
referral_code = @user.referral_code
cloudsponge_emails = params[:count_cloudsponge]
tracked_count = false
InviteWorker.perform_async(content, emails, total_emails, user_email, referral_code, cloudsponge_emails, tracked_count, user_id)
redirect_to :back
end
/app/workers/invite_worker.rb
class InviteWorker
include Sidekiq::Worker
def perform(content, emails, total_emails, user_email, referral_code, cloudsponge_emails, tracked_count, user_id)
user = User.find(user_id)
emails.each_with_index do |email,index|
email = email.gsub(/\s+/, '')
if email.include? '<'
email = email.split('<')[1].split('>')[0]
end
if User.where(:email => email).empty? && EmailInvite.where('created_at >= ?', Time.now.beginning_of_day).where(:email => email).empty? && User.new.verify_email(email)
UserMailer.invite_email(content, email, user_email, referral_code).deliver_now
if index == 0 || tracked_count == false
EmailInvite.create(:user => user, :email => email, :count => total_emails)
tracked_count = true
else
EmailInvite.create(:user => user, :email => email)
end
end
end
end
end
/config/application.rb
config.autoload_paths += %W(
#{config.root}/app/workers
#{config.root}/lib
)
config.eager_load_paths += %W(
#{config.root}/app/workers
#{config.root}/lib
)
Procfile
web: bundle exec puma -C ./config/puma.rb
worker: bundle exec sidekiq -e $RACK_ENV -r ./config/sidekiq.rb -c ${SIDEKIQ_CONCURRENCY:-5}
编辑-提供的错误消息:
4 TID-u31nw InviteWorker JID-ea609b6557e45a4d299dcde7 INFO: start
4 TID-u31nw InviteWorker JID-ea609b6557e45a4d299dcde7 INFO: fail: 0.002 sec
4 TID-u31nw WARN: {"context":"Job raised exception","job":{"class":"InviteWorker","args":["Hey, join me and sign up!",["email@umd.edu"],1,"email@gmail.com","68QBZ","0",false,2945],"retry":true,"queue":"default","jid":"ea609b6557e45a4d299dcde7","created_at":1531780015.902798,"enqueued_at":1531780015.9035575},"jobstr":"{\"class\":\"InviteWorker\",\"args\":[\"Hey, join me and sign up!\",[\"email@umd.edu\"],1,\"email@gmail.com\",\"68QBZ\",\"0\",false,2945],\"retry\":true,\"queue\":\"default\",\"jid\":\"ea609b6557e45a4d299dcde7\",\"created_at\":1531780015.902798,\"enqueued_at\":1531780015.9035575}"}
4 TID-u31nw WARN: NameError: uninitialized constant InviteWorker::User
4 TID-u31nw WARN: /app/app/workers/invite_worker.rb:5:in `perform'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/processor.rb:187:in `execute_job'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/processor.rb:169:in `block (2 levels) in process'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/middleware/chain.rb:128:in `block in invoke'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/middleware/chain.rb:133:in `invoke'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/processor.rb:168:in `block in process'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/processor.rb:139:in `block (6 levels) in dispatch'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/job_retry.rb:98:in `local'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/processor.rb:138:in `block (5 levels) in dispatch'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq.rb:36:in `block in <module:Sidekiq>'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/processor.rb:134:in `block (4 levels) in dispatch'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/processor.rb:199:in `stats'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/processor.rb:129:in `block (3 levels) in dispatch'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/job_logger.rb:8:in `call'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/processor.rb:128:in `block (2 levels) in dispatch'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/job_retry.rb:73:in `global'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/processor.rb:127:in `block in dispatch'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/logging.rb:48:in `with_context'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/logging.rb:42:in `with_job_hash_context'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/processor.rb:126:in `dispatch'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/processor.rb:167:in `process'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/processor.rb:85:in `process_one'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/processor.rb:73:in `run'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/util.rb:16:in `watchdog'
/app/vendor/bundle/ruby/2.3.0/gems/sidekiq-5.1.3/lib/sidekiq/util.rb:25:in `block in safe_thread'
答案 0 :(得分:1)
就我而言,我刚刚在生产中重新启动了Sidekiq,它解决了一个问题:sudo service sidekiq restart
答案 1 :(得分:0)
在我的情况下,问题出在application.yml的redis的“名称空间”参数中,尝试将其写入并重新加载redis,sidekiq和服务器
redis: 网址:redis:// localhost:6379/1 命名空间:“ some_namespace_name”
答案 2 :(得分:0)
哦,天哪,这对我来说真的很难理解,这与eager_load或自动加载路径无关。如果您将puma与许多工作人员一起使用,则需要在config / puma.rb
中的工作人员编号以下调用这些workers 2
preload_app! #used when workers are more than 0