簡體   English   中英

如何讓這個resque工作起作用?

[英]How to get this resque job to work?

我希望Resque工作人員通過Viralheat API獲取情緒,以獲取后台品牌實例(屬於User)的標簽,然后將該響應的mood參數保存到Tag。

更新

我修改了代碼,現在工作失敗了,因為它表示nil的未定義方法score :NilClass。 發生這種情況是因為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工人

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

你的參數列表中有一個拼寫錯誤: def self.perform(tag.id, user_id)應該是def self.perform(tag_id, user_id) (注意下划線)。

更新

您不應該在Resque Job中使用實例變量。 在宣布它之前,您實際上正在嘗試使用@brand

您正試圖從params獲取信息。 沒有請求,因此沒有params 您可能希望使用tag_id參數替換該tag_id參數。

您排隊參數的順序正在被翻轉。 您的enqueue調用是在tag_id之前指定user_id 該作業期望user_id之前的tag_id

並且由於您更新了問題,因此您已將tag_id參數切換為tags_id 我不知道你是否意識到這一點,但調用brand.tags.id可能只返回一個id(我對acts_as_taggable不太熟悉)。

第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上使用Camel Case命名與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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM