如何让这个 resque 工作发挥作用?
我想让 Resque 工作人员通过 Viralheat API 获取品牌实例标签的情绪 (属于User)在后台,然后将该响应的心情参数保存到一个Tag中。
更新:
我修改了代码,现在工作程序失败了,因为它显示 nil:NilClass 的未定义方法score
。发生这种情况是因为 ActsAsTaggableOn::Tagging.find_by_tag_id(tag.id).score.create
没有 tag.id 可供检查,而且我不认为 .create 是有效的 Ruby。
Viralheat API 响应 (JSON) 格式:
{"prob":0.537702567133646,"mood":"positive","text":"hello, goodbye"}
控制器:
def update
@brand = Brand.find(params[:id])
current_user.tag(@brand, :with => params[:brand][:tag_list], :on => :tags)
if @brand.update_attributes(params[:brand])
redirect_to :root, :notice => "Brand tagged."
else
render :action => 'edit'
end
Resque.enqueue(SentimentJob, @brand.tags)
end
sentiment_job.rb Worker:
require 'resque-retry'
class SentimentJob
@queue = :sentiment_pull
def self.perform(tags)
tags.each do |tag|
url = "http://www.viralheat.com/api/sentiment/review.json"
@sentiment_response = url.to_uri.get(
:api_key => 'MY KEY',
:text => tag.name.to_s ).deserialize
#If I comment out the 3 lines below, the worker runs and I get 3 successful callbacks but can't save them.
@sentiment_value = @sentiment_response[:mood]
@sentiment_store = ActsAsTaggableOn::Tagging.find_by_tag_id(tag.id).score.create(@sentiment_value)
@sentiment_store.save
end
end
end
标签表:
create_table "taggings", :force => true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context"
t.datetime "created_at"
t.string "Sentiment"
end
I want to have a Resque worker get sentiment via the Viralheat API for tags for a brand instance
(belongs to User) in the background, and then save the mood parameter of that response to a Tag.
Update:
I've revised the code and now the worker fails because it says undefined method score
for nil:NilClass. That's happening because ActsAsTaggableOn::Tagging.find_by_tag_id(tag.id).score.create
has no tag.id to check against, also I don't think .create is valid Ruby.
Viralheat API response (JSON) format:
{"prob":0.537702567133646,"mood":"positive","text":"hello, goodbye"}
Controller:
def update
@brand = Brand.find(params[:id])
current_user.tag(@brand, :with => params[:brand][:tag_list], :on => :tags)
if @brand.update_attributes(params[:brand])
redirect_to :root, :notice => "Brand tagged."
else
render :action => 'edit'
end
Resque.enqueue(SentimentJob, @brand.tags)
end
sentiment_job.rb Worker:
require 'resque-retry'
class SentimentJob
@queue = :sentiment_pull
def self.perform(tags)
tags.each do |tag|
url = "http://www.viralheat.com/api/sentiment/review.json"
@sentiment_response = url.to_uri.get(
:api_key => 'MY KEY',
:text => tag.name.to_s ).deserialize
#If I comment out the 3 lines below, the worker runs and I get 3 successful callbacks but can't save them.
@sentiment_value = @sentiment_response[:mood]
@sentiment_store = ActsAsTaggableOn::Tagging.find_by_tag_id(tag.id).score.create(@sentiment_value)
@sentiment_store.save
end
end
end
Tagggings table:
create_table "taggings", :force => true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context"
t.datetime "created_at"
t.string "Sentiment"
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的参数列表中有一个拼写错误:
def self.perform(tag.id, user_id)
应该是def self.perform(tag_id, user_id)
(注意下划线) 。更新
您不应该在 Resque 作业中使用实例变量。您实际上是在声明之前尝试使用
@brand
。您正在尝试从
params
获取信息。没有请求,因此没有params
。您可能想用tag_id
参数替换 find 参数。您排队参数的顺序正在颠倒。您的
enqueue
调用在tag_id
之前指定user_id
。该作业需要tag_id
在user_id
之前。由于您更新了问题,因此您已将
tag_id
参数切换为tags_id
。我不知道您是否意识到这一点,但是调用brand.tags.id
可能只会返回一个 id(我对acts_as_taggable
不太熟悉)。You have a typo in your argument list:
def self.perform(tag.id, user_id)
should bedef self.perform(tag_id, user_id)
(note the underscore).Update
You shouldn't be using instance variables in a Resque Job. You are actually trying to use
@brand
before you declare it.You are trying to get information from
params
. There is no request, and thus noparams
. You probably want to swap out that find argument with thetag_id
argument.The order you are queueing arguments is getting flipped. Your
enqueue
call is specifying theuser_id
before thetag_id
. The job is expecting thetag_id
before theuser_id
.And since you updated your question, you have switched the
tag_id
argument intotags_id
. I don't know if you realize this, but callingbrand.tags.id
will probably return just a single id (I'm not too familiar withacts_as_taggable
).第 1 步 - 修复您的控制器,使其仅在成功时运行作业。
第2步 - 修复你的表列,你不能在heroku上使用驼峰式命名法和PG,在之前的帖子中看到你在heroku上,这会给你带来问题。
因此,运行
rails g migration FixColumnName
进入您的 db/migrate 文件夹并找到迁移,
将其粘贴到其中
运行
rake db:migrate
第 3 步 - 修复您的工作线程,使其能够正常运行你想让它做什么。
请注意 - 我完全忽略了您的“分数”方法,因为您的代码中没有提及它,并且您在问题中没有说明它的用途或应该做什么。
Step 1 - Fix your controller so it's only running the job on success.
Step 2 - Fix your table columns, you can't use the Camel Case naming with PG on heroku, saw in an earlier post you were on heroku, this will give you issue.
so run
rails g migration FixColumnName
go into your db/migrate folder and find the migration
paste this into it
run
rake db:migrate
Step 3 - Fix your worker so that it does what you want it to do.
Please note - I have completely ignored your "score" method as there is no mention of it in your code, and you don't say anywhere in your question what it is used for or what it should be doing.