如何让这个 resque 工作发挥作用?

发布于 2024-12-05 18:13:42 字数 1826 浏览 1 评论 0原文

我想让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老旧海报 2024-12-12 18:13:42

您的参数列表中有一个拼写错误: 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_iduser_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 be def 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 no params. You probably want to swap out that find argument with the tag_id argument.

The order you are queueing arguments is getting flipped. Your enqueue call is specifying the user_id before the tag_id. The job is expecting the tag_id before the user_id.

And since you updated your question, you have switched the tag_id argument into tags_id. I don't know if you realize this, but calling brand.tags.id will probably return just a single id (I'm not too familiar with acts_as_taggable).

话少心凉 2024-12-12 18:13:42

第 1 步 - 修复您的控制器,使其仅在成功时运行作业。

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."
    #update was successful, create the job
    Resque.enqueue(SentimentJob, @brand.tags)
  else
    render :action => 'edit'
  end
end

第2步 - 修复你的表列,你不能在heroku上使用驼峰式命名法和PG,在之前的帖子中看到你在heroku上,这会给你带来问题。

因此,运行 rails g migration FixColumnName

进入您的 db/migrate 文件夹并找到迁移,

将其粘贴到其中

class FixColumnName < ActiveRecord::Migration
  def self.up
    rename_column :taggings, :Sentiment, :sentiment
  end

  def self.down
    # rename back if you need or do something else or do nothing
  end
end

运行 rake db:migrate

第 3 步 - 修复您的工作线程,使其能够正常运行你想让它做什么。

请注意 - 我完全忽略了您的“分数”方法,因为您的代码中没有提及它,并且您在问题中没有说明它的用途或应该做什么。

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          


    #find the tag
    @tag = ActsAsTaggableOn::Tagging.find_by_tag_id(tag.id)

    #set the tags value for the field sentiment to @sentiment_response[:mood]
    @tag.sentiment = @sentiment_response[:mood]

    #save the tag
    @tag.save

    end
  end
end

Step 1 - Fix your controller so it's only running the job on success.

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."
    #update was successful, create the job
    Resque.enqueue(SentimentJob, @brand.tags)
  else
    render :action => 'edit'
  end
end

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

class FixColumnName < ActiveRecord::Migration
  def self.up
    rename_column :taggings, :Sentiment, :sentiment
  end

  def self.down
    # rename back if you need or do something else or do nothing
  end
end

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.

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          


    #find the tag
    @tag = ActsAsTaggableOn::Tagging.find_by_tag_id(tag.id)

    #set the tags value for the field sentiment to @sentiment_response[:mood]
    @tag.sentiment = @sentiment_response[:mood]

    #save the tag
    @tag.save

    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文