使用 Web 应用程序管理定时事件的首选方法是什么?

发布于 2024-09-24 23:25:47 字数 150 浏览 2 评论 0原文

我正在设计一个 ASP.NET 4.0 Web 应用程序,管理员可以在其中创建一个有期限的拍卖。过期时间将存储在数据库中。考虑到应用程序实例在预定时间可能尚未运行,如何确保拍卖在预定时间结束?该应用程序将由 IIS7 托管。我正在考虑 Windows 服务,但我想知道还有哪些其他选择。

I'm designing an ASP.NET 4.0 Web application where administrators may create an auction with an expiration. The expiration time would be stored in a database. How can I ensure that the auction ends at the predetermined time, considering the fact that an application instance may not be running when it is time? The application will be hosted with IIS7. I am considering Windows service, but I am wondering what other options are out there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

当梦初醒 2024-10-01 23:25:47

您可以在此处从两种方案中进行选择:

  • 使用 Web 应用程序进行懒惰
  • 使用服务进行活动

懒惰方案

除非您必须立即与拍卖获胜者进行交互,否则您可以等到应用程序启动,然后让应用程序确定是否有任何过期的拍卖。在那一刻收集它们并进行相应的处理。

活动场景

创建一个从数据库中挑选第一个到期拍卖的服务。存储该日期时间并让服务休眠直到拍卖到期。在该日期时间启动服务并处理即将到期的拍卖。然后让服务寻找下一个到期的拍卖并让服务再次休眠。等等..我认为Windows Workflow Foundation包含所有工具以及本次实践的要求。

介于两者之间

激活一个调度程序,每小时/半小时/15 分钟唤醒您的网络应用程序并执行一些懒惰的操作。使用 HostMonitor 等调度程序。

You can choose from two scenarios here:

  • Lazy with the web application
  • Active with a service

Lazy Scenario

Unless you have to interact instantly with the winner of an auction you could wait until the application starts and then let the application determine if there are any auctions that are expired. Collect them at that moment and handle them accordingly.

Active Scenario

Create a service that picks the first expiring auction from the DB. Store that DateTime and let the service sleep till the auction expires. Raise the service at that DateTime and process the expiring auction. Then let the service look for the next auction(s) to expire and let the service sleep again. And on and on.. I think The Windows Workflow Foundation contains all tools and requirements for this practice.

Something in between

Activate a scheduler that wakes up you web-app every hour/half hour/15 minutes and do the lazy stuff. Use a scheduler like HostMonitor.

清晰传感 2024-10-01 23:25:47

处理定时事件的 Windows 服务将是最佳实践。在 ASP.NET 中使用 System.Threading.Timer 是不好的。它可能适用于您的情况,因为所做的更改不会直接影响用户界面,但我不会打赌它会完美地工作。如果您需要在 Web 应用程序后台安排事件,请使用服务器应用程序。

要记住一件事;您可能会同时进行数百或数千次公开拍卖。如果您在每次拍卖开始时设置计时器,则该服务器将管理数百或数千个休眠线程。我只会查找在下次正常投票之前结束的拍卖,并仅为这些拍卖设置计时器。这将使您的等待线程数量减少到大约几十个。但是,如果您正在编写下一个 eBay,那么当您进行数十万或数百万次拍卖,并且每分钟开始和结束数百或数千次拍卖时,即使这样也会令人窒息。

A windows service that handles the timed events would be the best practice. Using a System.Threading.Timer in ASP.NET is bad juju; it MIGHT work in your case, because the change being made doesn't affect the UI directly, but I wouldn't bet on it working flawlessly. If you need to schedule events in the background of a web app, use a server app.

One thing to keep in mind; you may have hundreds or thousands of open auctions at a time. If you set a timer on every auction when it's opened, you'll have hundreds or thousands of sleeping threads managed by this server. I'd look only for auctions that would end before the next time you'd normally poll, and set timers for those auctions only. That would get you down to maybe a few dozen waiting threads. If you're writing the next eBay, though, even this will choke when you get hundreds of thousands, or millions, of auctions, and several hundred or thousand begin and end every minute.

回忆追雨的时光 2024-10-01 23:25:47

如果您希望它 100% 可靠,那么执行所有计划逻辑的 Windows 服务是最佳选择。正如您所说,您不能信任 Web 应用程序,因为它甚至可能没有运行。

If you want it to be 100% reliable, a Windows service that perform all scheduled logic is the way to go. As you say, you cannot trust a web application since it may not even be running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文