rufus gem如何在阻止另一个工作的同时保持一项工作

时间:2017-01-20 02:27:04

标签: ruby-on-rails twilio background-process scheduler rufus-scheduler

    This is my scheduler.rb in my initializer file

    unless defined?(Rails::Console) || File.split($0).last == 'rake'
 s = Rufus::Scheduler.singleton
 s.every '1m', :tag => 'main_process' do
   Rails.logger.info "hello, it's #{Time.now}"
   Rails.logger.flush
   Bid.all.each do |bid|
     id = bid.event_id
      puts "*" * 50
      puts bid.id
      puts bid.event_id
      puts "*" * 50
       # @price = BuyNowBid.find_by(bid_id: params[:bid_id])
      @events = Unirest.get("https://api.seatgeek.com/2/events/#{id}?&client_id=NjQwNTEzMXwxNDgxNDkxODI1").body
      if @events["stats"]
        @low = @events["stats"]["lowest_price"] || 0
        @avg = @events["stats"]["average_price"] || 0
        BuyNowBid.create(bid_id: bid.id, lowest_price: @low , average_price: @avg)

        if @low <= bid.bid
          send_message("+13125501444", "Lowest price matched! Buy your ticket now!")
          bid.bid = 0
          bid.save
        end

      else
        puts 'problem with @events?'
        p @events
      end


   end
 end

def send_message(phone_number,alert_message)     account_sid =&#34;&#34;  auth_token =&#34;&#34;

    @client = Twilio::REST::Client.new account_sid, auth_token
    @twilio_number = "" 
    message = @client.messages.create(
      :from => @twilio_number,
      :to => phone_number,
      :body => alert_message
    )
    puts message.to
  end

因此,我正在运行一份工作,以便从api每分钟提取最低价格,当最低价格与某个人的出价相符时,他们会收到一个文字通知,虽然我可以解决这个问题我希望这份工作能够随时随地保持运行我从api中提取最低价格,但我不希望用户每分钟都收到相同的文本通知。

现在我已经拥有它所以这不会发生但是在匹配出价后它基本上会从数据库中删除。所以基本上我一直在问我如何能够每分钟为最低价格的竞价匹配抓取api,但只向用户发送一个文本,通知他们竞价匹配,而不必从数据库中删除该竞标。

1 个答案:

答案 0 :(得分:1)

我真的在想这个我只是在我的出价表上创建了一个名为“已保存出价”的新列,并在它变为零之前将其设置为等于bid.bid

     @events = Unirest.get("https://api.seatgeek.com/2/events/#{id}?&client_id=NjQwNTEzMXwxNDgxNDkxODI1").body

      if @events["stats"]
        @low = @events["stats"]["lowest_price"] || 0
        @avg = @events["stats"]["average_price"] || 0
        BuyNowBid.create(bid_id: bid.id, lowest_price: @low , average_price: @avg)

        if @low <= bid.bid
          send_message("+13125501444", "Lowest price matched for #{@events["title"]} ! Buy your ticket now for #{bid.saved_bid}!")
          **bid.saved_bid = bid.bid**
          bid.bid = 0
          bid.save