Sidekiq + Rails模型生成额外记录

时间:2015-02-02 04:49:09

标签: ruby-on-rails ruby-on-rails-4 console sidekiq worker

我的预定短信存在问题。我运行一个rake任务,检查是否应该将文本消息放入Sidekiq队列。处理记录(发送文本)但生成新的空记录,并且sentstatus不会更新为“true”。

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
        if (Date.today == text_message.scheduled_date) && (Time.now.hour >= text_message.scheduled_time.hour)
        # Sidekiq code: 
        SendTextWorker.perform_async(text_message.id)
        end
      end
    end
  end
end

send_text_worker.rb

class SendTextWorker
  include Sidekiq::Worker

  def perform(text_message_id)
    text = TextMessage.find(text_message_id)
    text.send_text_message(text.content, text.phone)
  end
end

text_message.rb

require 'twilio-ruby'
require 'date'

class TextMessage < ActiveRecord::Base

  belongs_to :client, dependent: :destroy
  belongs_to :step, dependent: :destroy
  has_many :coach_emails

  before_save :grab_phone

  def grab_phone
    self.phone = phone
  end

  def send_text_message(message, phone)

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

    begin
      @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)

      rescue Twilio::REST::RequestError => e
        puts e.message
    end

   if e != "400" || e != "500"
      self.sentstatus = true
   end

    self.save!
  send  
send

Rails控制台:在调用rake任务之前 (sentstatus是假的)

irb(main):001:0> TextMessage.all
  TextMessage Load (0.5ms)  SELECT "text_messages".* FROM "text_messages"
=> #<ActiveRecord::Relation [#<TextMessage id: 164, client_id: nil, content: "Testing Sidekiq processing", incoming_message: false, created_at: "2015-02-02 04:43:29", updated_at: "2015-02-02 04:43:29", scheduled_date: "2015-02-01", sentstatus: false, step_id: 4, phone: "+14127364161", scheduled_time: "2000-01-01 14:00:00">]>

Rails控制台:调用rake任务后 (sentstatus是假的,应该是真的。我也有这个新奇怪的空记录)

irb(main):001:0> TextMessage.all
  TextMessage Load (0.5ms)  SELECT "text_messages".* FROM "text_messages"
=> #<ActiveRecord::Relation [#<TextMessage id: 164, client_id: nil, content: "Testing Sidekiq processing", incoming_message: false, created_at: "2015-02-02 04:43:29", updated_at: "2015-02-02 04:43:29", scheduled_date: "2015-02-01", sentstatus: false, step_id: 4, phone: "+14127364161", scheduled_time: "2000-01-01 14:00:00">, 
#<TextMessage id: 165, client_id: nil, content: nil, incoming_message: nil, created_at: "2015-02-02 04:45:24", updated_at: "2015-02-02 04:45:24", scheduled_date: nil, sentstatus: true, step_id: nil, phone: nil, scheduled_time: nil>]>

我有一种感觉,这是我缺少的Sidekiq细微差别。谢谢你的任何想法!

1 个答案:

答案 0 :(得分:0)

我最终将模型逻辑移到了我的工作人员身上。 Presto - 计时工作,我没有产生任何额外的零记录。