使用应用程序启动中的任务模拟 cron 作业
我正在尝试找到一种非常简单的方法来为我的 Web 应用程序启动一个简单的 cron 作业。我正在考虑的是在 Application_Start 事件中启动一个任务。该任务将有一个 while 循环,并且每小时左右执行一些操作,就像正常的 cron 作业一样。
这是个好主意还是会带来一些麻烦?
我能想到的一些问题如下:
任务突然停止工作或挂起。也许进行一些故障转移 检查任务是否仍在运行以及是否仍在运行的机制 重新启动它。
如果任务完全出错并且我必须重新启动,则会发生内存泄漏
整个应用程序。- 无法控制动态更改任务,但这不应该是
对于我想做的事情来说是个问题,但对于其他人来说可能是问题。
有人对此有什么建议或经验吗?
I'm trying to find a very easy easy way to start a simple cron job for my Web Application. What I was thinking about is starting a Task in the Application_Start event. This task will have a while loop and do some action every hour or so like a normal cron job would do.
Is this a good idea or will this give some trouble?
Some of the problems I could think of are the following:
The Task suddenly stops working or hangs. Maybe make some fail over
mechanism that would check if the task is still running and if not
restart it.Memory leaking if the Task totally goes wrong and I have to restart
the whole application.- No control in changing the Task on the fly but this shouldn't be a
problem for the thing I want to do but for others it might be.
Does someone have any suggestions or experiences with trying this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管 Darin 说在 Web 应用程序中执行 cron 作业是一个坏主意(我在某种程度上同意他的观点),但如果明智地使用并且对于短期运行的作业来说,它可能并没有那么糟糕。
在 Web 应用程序中使用相同的 Quartz.NET 可能会很好,我正在我的一个项目中使用这样的
http://bugsquash.blogspot.com/2010/06/embeddable -quartznet-web-consoles.html 适用于小型作业,并且运行良好 - 易于监控(比监控远程 Windows 进程更容易),可能用于共享主机。
Although Darin says that doing cron jobs in a web application is a bad idea (and I agree with him in some way), it may be not so bad, when used wisely and for short running jobs.
Using same Quartz.NET in web application may be quite nice, I'm using in one of my projects like this
http://bugsquash.blogspot.com/2010/06/embeddable-quartznet-web-consoles.html for small jobs and it is running nice - it's easy to monitor (easier than monitoring remote windows process), may be used on shared hosting.
在 Web 应用程序中执行 cron 作业是一个坏主意。 IIS 可以随时回收该应用程序,您的工作将停止。我建议您在单独的 Windows 服务中执行此操作。你可以看看Quartz.NET。另一种可能性是控制台应用程序,它执行该作业并在 Windows 调度程序中进行调度。
Doing cron jobs in a web application is a bad idea. IIS could recycle the application at any time and your job will stop. I would recommend you performing this in a separate windows service. You could take a look at Quartz.NET. Another possibility is a console application which does the job and which is scheduled within the Windows Scheduler.