Search code examples
ruby-on-railstwiliosidekiqworker

Circular dependency on Rails Sidekiq setup


Sidekiq and Redis are both running locally. Sidekiq will process the job if I just use a put statement. If I take Sidekiq out of the equation, the rake task will send a text via the model.

I have more complicated tasks to setup so I want to get Sidekiq going for the app. I'm following this tutorial http://ruthienachmany.github.io/blog/2013/08/10/sidekiq-redis-cron-jobs/.

I keep getting this error when I manually trigger the rake task in the Sidekiq

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

Relevant Gemfile info:

 gem 'rails', '4.1.1' 

 gem 'sidekiq', '3.2.5'

This is the rake task that calls up the Sidekiq worker (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

Here is my 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

Here is the text_message.rb model that it is calling:

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

Thanks to anyone who sees why I am getting a circular error on Text_Message.


Solution

  • Even though the files for classes are in snake_case, the class name itself should be in CamelCase.

    Replace Text_Message.find(text_message_id) with TextMessage.find(text_message_id).