简体   繁体   English

如何在asp.net MVC中创建定时调度程序?

[英]How do I create a timed scheduler in asp.net MVC?

I would like to create a relatively simple timed event scheduling function in an MVC website for application functions, not necessarily for users at this time, but could be in the future. 我想在MVC网站中为应用程序功能创建一个相对简单的定时事件调度功能,当前不一定是用户,但将来可能会。 My current desire is to use the schedule to cause specific changes to the website based on timed events. 我当前的愿望是使用时间表根据定时事件对网站进行特定更改。 The general idea is shown below at the end of the post. 总体思路如下所示。

I first investigated Quartz for asp.net because it was recommended on other posts, but the advice in that open source package specifically recommended not using it in an embedded mode. 我首先对asp.net的Quartz进行了调查,因为在其他帖子中也推荐使用它,但是该开源软件包中的建议特别建议不要在嵌入式模式下使用它。 I don't think I can add it as a service to the server where the MVC site is hosted. 我认为我不能将其作为服务添加到托管MVC站点的服务器中。

Although I'm familiar with the concept of multi-threading, I am not experienced with it. 尽管我熟悉多线程的概念,但是我对它没有经验。 Is running the Timer Process below on a background thread a good solution, or should I be looking at something else? 在后台线程上运行下面的Timer Process是一个好的解决方案,还是我应该看看别的东西? I assumed the Timer Process would simply execute forever, comparing the schedule against the system clock and cause the required event code to run when the time trigger occurs. 我假设计时器进程将永远执行,将时间表与系统时钟进行比较,并在发生时间触发时使所需的事件代码运行。

If threading is a bad idea please advise where I should look. 如果穿线不是一个好主意,请告知我应该看的地方。 If threading is the right idea, any suggestions or warnings would be appreciated. 如果穿线是正确的想法,任何建议或警告将不胜感激。 I am reading up on threading at this time. 我目前正在阅读线程。

I have several specific questions: 我有几个具体问题:

1) Should the individual event code run on the same thread, or does it matter? 
2) How do I handle the problem others have suggested occurs if 'IIS kills the thread' or the 'app pool is recycled'.
3) Is it a wrong idea that I send some message to the MVC site that triggers the Timer Process to overcome the issues in #2 above?
4) Is there a way to force a page to automatically refresh with a new display when the timer event occurs?  That doesn't seem to be the way asp.net MVC normally works.

Finally, this sounded like a problem that others have probably resolved and reduced to an open source solution for asp.net MVC. 最后,这听起来像是其他人可能已经解决并将其简化为asp.net MVC的开源解决方案的问题。 As I look through Nu-Get there are a few that seem to describe this problem, but they have very few downloads. 当我浏览Nu-Get时,似乎有一些描述了此问题,但下载量很少。 Any recommendations along this route? 这条路线上有什么建议吗?

在此处输入图片说明

I think in your case will be good to use Hangfire. 我认为以您的情况使用Hangfire会很好。 You can configure like parallel server and create different jobs with different schedule. 您可以像并行服务器一样配置,并以不同的时间表创建不同的作业。

For NetCore app: 对于NetCore应用程序:

app.UseHangfireServer();
app.UseHangfireDashboard("/hangfire", options);

// Process notifications every 6 hours
RecurringJob.AddOrUpdate<B2CEngineNotifications>(jobEngine =>  jobEngine.SendBillingNotifications(), "0 */6 * * *");

In this case each job works in his own thread. 在这种情况下,每个作业都在自己的线程中工作。

1) Should the individual event code run on the same thread, or does it matter? 1)单个事件代码应该在同一线程上运行还是有关系? It depends what type of events do you expect. 这取决于您期望的事件类型。 If they are not depended, you can do it in separate threads. 如果不依赖它们,则可以在单独的线程中进行操作。

How do I handle the problem others have suggested occurs if 'IIS kills the thread' or the 'app pool is recycled'. 如果“ IIS杀死线程”或“应用程序池被回收”,我该如何处理别人建议的问题。 Hangfire will create in your DB some tables. Hangfire将在您的数据库中创建一些表。 If you will terminate process, on next start it get all data from db and start execute events on scedule. 如果要终止进程,则在下次启动时,它将从db获取所有数据,并在scedule上开始执行事件。

Is it a wrong idea that I send some message to the MVC site that triggers the Timer Process to overcome the issues in #2 above? 我向MVC站点发送一些消息以触发计时器进程以克服上面#2中的问题是一个错误的主意吗? Hangfire has his own interface to manage jobs. Hangfire有自己的界面来管理工作。 Or you need to have a option to start event from your MVC app? 或者您需要选择从MVC应用启动事件? You can do it from MVC just change db values. 您可以从MVC做到这一点,只需更改db值即可。 And hangfire will start job. hangfire将开始工作。

Is there a way to force a page to automatically refresh with a new display when the timer event occurs? 有没有一种方法可以在发生计时器事件时强制页面自动刷新为新的显示? That doesn't seem to be the way asp.net MVC normally works. 这似乎不是asp.net MVC正常工作的方式。 In this case you need to use AJAX to have "live" connection with server and "ask" is something changed or not. 在这种情况下,您需要使用AJAX与服务器建立“实时”连接,而“询问”是否已更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM