循环依赖于Rails Sidekiq设置

时间:2014-11-27 04:19:28

标签: ruby-on-rails twilio sidekiq worker

Sidekiq和Redis都在本地运行。如果我只使用put语句,Sidekiq将处理该作业。如果我将Sidekiq从等式中取出,则rake任务将通过模型发送文本。

我有更复杂的任务要设置,所以我想让Sidekiq进入应用程序。我正在学习本教程http://ruthienachmany.github.io/blog/2013/08/10/sidekiq-redis-cron-jobs/

当我手动触发Sidekiq中的rake任务时,我一直收到此错误

2014-11-27T03:55:40.906Z 36691 TID-ouue3wz8o WARN: Circular dependency detected while autoloading constant Text_Message

相关的Gemfile信息:

 gem 'rails', '4.1.1' 

 gem 'sidekiq', '3.2.5'

这是调用Sidekiq工作人员的rake任务(send_scheduled_text.rake):

require_relative '../../app/workers/send_text_worker'

namespace :send_scheduled_text do

  task:texts => :environment do

    TextMessage.all.each do |text_message|
      if ((text_message.sentstatus == false) && (Date.today ==  text_message.scheduled_date))
      # Sidekiq code: 
      SendTextWorker.perform_async(text_message.id)
      end
    end
  end
end

这是我的send_text_worker.rb

class SendTextWorker
  include Sidekiq::Worker

  def perform(text_message_id)
    text = Text_Message.find(text_message_id)
    SendText.new(text).send_text_message
  end
end

以下是它正在调用的text_message.rb模型:

require 'twilio-ruby'
require 'date'

class TextMessage < ActiveRecord::Base

 belongs_to :client, dependent: :destroy
 belongs_to :step, dependent: :destroy

 before_save :grab_phone

  def grab_phone
    self.phone = step.goal.action_plan.client.phone
  end

  def send_text_message(message)

    twilio_sid = ENV["TWILIO_ACCT_SID"]
    twilio_token = ENV["TWILIO_AUTH_TOKEN"]
    twilio_phone_number = ENV["TWILIO_PHONE_NUMBER"]

    phone = step.goal.action_plan.client.phone

    @twilio_client = Twilio::REST::Client.new(twilio_sid, twilio_token)

    @twilio_client.account.sms.messages.create(
      :from => "+1#{twilio_phone_number}",
      :to => phone,
      :body => message)

    self.sentstatus = true
    self.save!
  end  
end

感谢所有看到我在Text_Message上收到循环错误的原因。

1 个答案:

答案 0 :(得分:2)

即使类的文件是snake_case,类名本身也应该在CamelCase中。

Text_Message.find(text_message_id)替换为TextMessage.find(text_message_id)