Sidekiq worker无法写入数据库或日志文件

时间:2013-03-26 18:03:14

标签: ruby-on-rails redis twilio sidekiq

我正在Herokou和Redis上构建一个应用程序,它会在包含移动电话号码的输入CSV文件中为每一行发送一条SMS消息。使用Twilio在下面显示的sidekiq工作程序中发送消息。问题是即使正在为CSV中的所有行发送SMS,数据库写入(TextMessage.create)和日志写入(puts语句)仅针对一个执行CSV中的一行。在CSV文件中为每一行产生了一个Sidekiq工作者。似乎只有一个Sidekiq工作者具有I / O(DB,文件)访问权限,并且它将其与其他Sidekiq工作者锁定。任何帮助都将是赞赏。

sidekiq worker:

require 'sidekiq'
require 'twilio-rb'

class TextMessage < ActiveRecord::Base
    include Sidekiq::Extensions

    def self.send_message(number, body, row_index, column_index, table_id)
        puts "TextMessage#send_message: ROW INDEX: #{row_index} COLUMN INDEX: #{column_index} TABLEID: #{table_id} BODY: #{body} PHONE: #{number}"

        Twilio::Config.setup :account_sid  => 'obfuscated', :auth_token   => '<obfuscated>'
    sms = Twilio::SMS.create :to => number, :from => '+17085555555', :body => body +  ' | Sent: ' + Time.now.in_time_zone('Central Time (US & Canada)').strftime("%m/%d/%Y %I:%M%p Central")
        TextMessage.create :to => number, :from => '+17085555555'
        ImportCell.add_new_column(table_id, row_index, column_index, "Time Sent", Time.now.in_time_zone('Central Time (US & Canada)').strftime("%m/%d/%Y %I:%M%p Central"))
    end
end

致电sidekiq工作人员:

 TextMessage.delay_until(time_to_send, :retry => 3).send_message(phone, 'Scheduled: ' + time_to_send.in_time_zone('Central Time (US & Canada)').strftime("%m/%d/%Y %I:%M%p Central"), row_index, column_index, table.id)
      column_index += 1  

Heroku Procfile

worker: bundle exec sidekiq -C config/sidekiq.yml

sidekiq.yml

:verbose: false
:concurrency:  3
:queues:
  - [default, 5]

config/initializers/redis.rb

uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)

Sidekiq.configure_server do |config|
 database_url = ENV['DATABASE_URL']
 if(database_url)
   ENV['DATABASE_URL'] = "#{database_url}?pool=25"
   ActiveRecord::Base.establish_connection
  end
end

1 个答案:

答案 0 :(得分:1)

我是评论你的问题的人之一,只是解决了它!

你正在使用.Kate,SideKiq似乎不喜欢,所以我尝试使用.new,然后使用.save,这使它工作!我认为它与.create不是线程安全或类似的东西有关,但老实说我不知道​​。

非工作代码:

class HardWorker
    include Sidekiq::Worker

    def perform(name, count)
        puts 'Doing some hard work!'

        UserInfo.create(
         :user => "someone",
         :misc1 => 0,
         :misc2 => 0,
         :misc3 => 0,
         :comment => "Made from HardWorker",
         :time_changed => Time.now
        )

        puts 'Done with hard work!'
    end
end

工作代码:

class HardWorker
    include Sidekiq::Worker

    def perform(name, count)
        puts 'Doing some hard work!'

        a_row = UserInfo.new(
         :user => "someone",
         :misc1 => 0,
         :misc2 => 0,
         :misc3 => 0,
         :comment => "Made from HardWorker",
         :time_changed => Time.now
        )

        a_row.save

        puts 'Done with hard work!'
    end
end