Search code examples
ruby-on-railstwiliobackground-processschedulerrufus-scheduler

rufus gem how to keep one job running while stopping the other


    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

end

def send_message(phone_number, alert_message) account_sid = "" auth_token = ""

    @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

so I'm running a job to pull the lowest price every minute from an api and when the lowest price matches a bid someones placed they receive a text notification, which works although, the problem I'm having is i want the job to keep running where ever minute I'm pulling the lowest price from api, but i don't want the user to keep getting the same text notification every minute.

Right now I have it so this doesn't happen but after a bid is matched it is essentially deleted from the database. so basically I'm asking how can I keep scraping the api every minute for the lowest price bid match but only send one text to the user notifying them of the bid match and to not have to delete that bid from the database.


Solution

  • I was really over thinking this i just created a new column called saved bid on my bid table and set that equal to bid.bid before it became zero

         @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