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 = ""
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.
因此,我正在运行一个工作,以便从api每分钟提取最低价格,当最低价格与出价相符时,某些人会收到文本通知,虽然,我遇到的问题是我希望工作保持不变在哪里运行,我从api中提取最低价格,但我不希望用户每分钟都收到相同的文本通知。
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.
现在我拥有它所以这不会发生但是在匹配出价之后它基本上从数据库中删除了。所以基本上我问我如何能够每分钟为最低价格的竞价匹配编写api,但只向用户发送一个文本,通知他们竞价匹配,而不必从数据库中删除该竞标。
1 个解决方案
#1
1
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
我真的在想这个,我刚刚在我的出价表上创建了一个名为“已保存出价”的新列,并在它变为零之前将其设置为等于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
#1
1
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
我真的在想这个,我刚刚在我的出价表上创建了一个名为“已保存出价”的新列,并在它变为零之前将其设置为等于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