我有一个后台任务,它发送一个由Resque处理的电子邮件,它抛出了一个TypeError,我无法弄清楚是什么导致它。
我将任务排入队列:Resque.enqueue(ShownUpvoteEvent, self.id)
然后我在工人中处理它:
class ShownUpvoteEvent
@queue = :email
def self.perform(event_id)
UserMailer.shown_upvote_event(event_id).deliver
end
end
对UserMailer
的调用会触发此方法:
def shown_upvote_event(event_id)
@event = TimelineEvent.find(event_id)
@user = @event.subject
@actor = @event.actor
@song = @event.secondary_subject
if VideoVote.find_by_user_id_and_video_id(@actor.id, @song.id).value == 1
@voted = "upvoted"
else
@voted = "downvoted"
end
mail(:to => @user.email, :subject => "#{@actor.name} #{@voted} the song you showed to him/her.")
end
Resque显示我成功将事件对象的id传递给worker,但我不知道是什么导致了这个错误TypeError
Error
can't convert String into Integer.
这是堆栈跟踪:
/app/app/mailers/user_mailer.rb:60:in `[]'
/app/app/mailers/user_mailer.rb:60:in `shown_upvote_event'
/app/.bundle/gems/ruby/1.9.1/gems/actionpack-3.0.7/lib/abstract_controller/base.rb:150:in `process_action'
/app/.bundle/gems/ruby/1.9.1/gems/actionpack-3.0.7/lib/abstract_controller/base.rb:119:in `process'
/app/.bundle/gems/ruby/1.9.1/gems/actionpack-3.0.7/lib/abstract_controller/rendering.rb:41:in `process'
/app/.bundle/gems/ruby/1.9.1/gems/actionmailer-3.0.7/lib/action_mailer/old_api.rb:75:in `process'
/app/.bundle/gems/ruby/1.9.1/gems/actionmailer-3.0.7/lib/action_mailer/base.rb:471:in `process'
/app/.bundle/gems/ruby/1.9.1/gems/actionmailer-3.0.7/lib/action_mailer/base.rb:466:in `initialize'
/app/.bundle/gems/ruby/1.9.1/gems/actionmailer-3.0.7/lib/action_mailer/base.rb:450:in `new'
/app/.bundle/gems/ruby/1.9.1/gems/actionmailer-3.0.7/lib/action_mailer/base.rb:450:in `method_missing'
/app/app/workers/send_emails.rb:46:in `perform'
堆栈跟踪指向第60行真的很奇怪,因为它似乎不应该调用代码:
def heard_event(event_id)
@event = TimelineEvent.find(event_id)
@user = @event.subject
@actor = @event.actor
@song = @event.secondary_subject
showable_video = ShowableVideo.find_by_user_id_and_profile_id_and_video_id(@user.id, @actor.id, @song.id)
if showable_video.heard == true
@heard = "had already heard"
else
@heard = "had not yet heard"
end
#below is line 60, but this method should not be running
mail(:to => @user.email, :subject => "#{@actor.name} #{@heard} the song you showed to him/her.")
end
def shown_upvote_event(event_id)
@event = TimelineEvent.find(event_id)
@user = @event.subject
@actor = @event.actor
@song = @event.secondary_subject
if VideoVote.find_by_user_id_and_video_id(@actor.id, @song.id).value == 1
@voted = "upvoted"
else
@voted = "downvoted"
end
mail(:to => @user.email, :subject => "#{@actor.name} #{@voted} the song you showed to him/her.")
end