繁体   English   中英

sidekiq工人以错误的顺序发送twilio呼叫

[英]sidekiq workers sending twilio calls in wrong order

我有一个SMS应用程序,该应用程序将SMS消息发送给sidekiq worker,然后ping twilio实际发送消息。 我遇到的问题是,通过将消息发送给工作人员,有时超过160个字符的消息会以错误的顺序发送。 我认为这是因为sidekiq正在同时运行作业。 我该如何解决这个问题?

以前,我将循环浏览消息的每个160个字符,并将每个160个字符串发送给工作人员以进行发送。 这引起了问题,因为工作人员将进行设置并同时运行,以使消息不正常。 为了解决这个问题,我将160个字符的逻辑移到了worker中,我相信这解决了一条消息的问题。

但是,如果多条消息在1-2秒内通过,它们将同时发送,因此很可能再次出现故障。 如何确保sidekiq以我调用perform_async方法的顺序处理消息? 这是我的代码:

//messages_controller.rb

SendSMSWorker.new.perform(customer.id, message_text, 'sent', false, true)

//send_sms_worker.rb

def perform(customer_id, message_text, direction, viewed, via_api)
    customer = Customer.find(customer_id)
    company  = customer.company
    texts = message_text.scan(/.{1,160}/) # split the messages up into 160 char pieces
    texts.each do |text|
      message = customer.messages.new(
        user_id:     company.admin.id, # set the user_id to the admin's ID when using the api
        company_id:  company.id,
        text:        text,
        direction:   'sent',
        viewed:      false,
        via_api:     true
      )
      # send_sms returns nil if successful
      if error = message.send_sms
        customer.mark_as_invalid! if error.code == 21211
      else
        # only save the message if the SMS was successfully sent
        puts "API_SEND_MESSAGE company_id: #{company.id}, customer_id: #{customer.id}, message_id: #{message.id}, texts_count: #{texts.count}"
        message.save
        Helper.publish(company.admin, message.create_json_with_extra_attributes(true))
      end
    end
  end

需要明确的是,message.send_sms是实际上通过twilio发送短信的消息模型上的方法。 谢谢!

如果您要发送多封邮件,则每封邮件都会使用自己的路由到达目的地运营商。 即使它们以正确的顺序发送,也不能保证它们会以正确的顺序在手机上收到。 解决此问题的一种方法是使用最多1600个字符的串联消息(在美国)。 如果您通过“消息”资源发送一条长消息,它将作为一条长消息收到。 只要确保您正在使用“消息”资源:

@ client.account.messages.create()

代替

client.account.sms.messages.create()

你可以在这里阅读更多:

https://www.twilio.com/help/faq/sms/does-twilio-support-concatenated-sms-messages-or-messages-over-160-characters

http://twilio-ruby.readthedocs.org/en/latest/usage/messages.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM