I would like to setup a simple notifications system for my web app that is written in Ruby on Rails. My website is a simple auction site, but each auction has 2 stages: a 5 day "no minimum bid" stage where anyone can bid and a 3 day "
I would like to setup a simple notifications system for my web app that is written in Ruby on Rails. My website is a simple auction site, but each auction has 2 stages: a 5 day "no minimum bid" stage where anyone can bid and a 3 day "$1 minimum bid" stage where only the top 10% of the bidders from the previous stage can bid.
我想为我的Web应用程序设置一个简单的通知系统,该系统是用Ruby on Rails编写的。我的网站是一个简单的拍卖网站,但每个拍卖有两个阶段:5天“无最低出价”阶段,任何人都可以出价和3天“1美元最低出价”阶段,其中只有前10%的投标人阶段可以出价。
I want to notify my users when the auction goes from the first stage to the second. What is the best way to do this?
我想在拍卖从第一阶段到第二阶段时通知我的用户。做这个的最好方式是什么?
I've thought of two design options:
我想到了两个设计选择:
I don't know RoR has this, and whether this is an efficient system (because I want to build a system that will have thousands of users participating in hundreds of auctions - whether it gets that big or not is irrelevant, this is sort of a side project for me to learn to write high quality, scalable RoR) but basically, when a user bids on an auction, they become an observer of some sort on the auction. Then, somehow at the 5 day mark, all the listeners are notified. I don't know how that trigger happens though in RoR (is there some way to say "trigger after 5 days from creation" or some such thing?). To summarize, I would like to use the Observer pattern.
我不知道RoR有这个,以及这是否是一个有效的系统(因为我想建立一个系统,将有数千名用户参与数百次拍卖 - 无论它是否变得无关紧要,这是一种一个侧面项目,让我学习编写高质量,可扩展的RoR)但基本上,当用户竞标拍卖时,他们成为拍卖的某种观察者。然后,不知何故,在第5天,所有听众都会收到通知。我不知道在RoR中是如何发生这种触发的(有什么方法可以说“创建5天后触发”还是某种类似的东西?)。总而言之,我想使用Observer模式。
The key issue for me is trying to figure out how to trigger after a set period of time. The best way to do it, I think, would be for each auction to have some set state variable called "auction state". After a certain period of time, that would transition, and at that point every observer on the auction would get notified.
对我来说,关键问题是试图弄清楚如何在一段时间后触发。我认为,最好的方法是让每次拍卖都有一些设置状态变量称为“拍卖状态”。经过一段时间后,这将转变,届时拍卖会上的每个观察者都会收到通知。
There is a rake task that runs everyday at some set time each day. It goes through each auction and if any auction is on day 6, the observers of that auction are notified. This seems like a pretty bad way of going about it - I like the idea of having a state-ful auction.
有一个rake任务每天在某个设定的时间运行。它通过每次拍卖,如果在第6天进行任何拍卖,则通知该拍卖的观察员。这似乎是一种非常糟糕的方式 - 我喜欢进行有状态拍卖的想法。
Thoughts?
思考?
Thanks! Ringo
谢谢!林戈
2 个解决方案
#1
0
Have a look at https://github.com/bvandenbos/resque-scheduler, if you haven't heard yet, resque is a gem for creating background processing, having queues in redis. This gem resque-scheduler, lets you specify when to run a task/job.
看看https://github.com/bvandenbos/resque-scheduler,如果你还没有听说过,resque是创建后台处理的宝石,在redis中有队列。此gem resque-scheduler允许您指定何时运行任务/作业。
For example in your model you would have an after_create to schedule the task for changing the bid state.
例如,在您的模型中,您将有一个after_create来安排更改出价状态的任务。
after_create :shedule_state_change
private
def record_signup
Resque.enqueue_in(5.days, ChangeState)
end
And then you could notify all the bidders for that bid inside that task or create an observer for it.
然后,您可以在该任务中通知所有投标人该投标或为其创建一个观察者。
#2
1
You can take a look at https://github.com/pluginaweek/state_machine, specifically transitions callbacks. Alternatively you can also use the standard active record callbacks https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
您可以查看https://github.com/pluginaweek/state_machine,特别是转换回调。或者,您也可以使用标准的活动记录回调https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
minimum bid" stage where only the top 10% of the bidders from the previous stage can bid.I would like to setup a simple notifications sy