简体   繁体   English

在Application Start中使用任务模拟cron作业

[英]Simulating a cron job with a Task in Application Start

I'm trying to find a very easy easy way to start a simple cron job for my Web Application. 我正在尝试找到一种非常简单的方法来为Web应用程序启动简单的cron作业。 What I was thinking about is starting a Task in the Application_Start event. 我在想的是在Application_Start事件中启动任务。 This task will have a while loop and do some action every hour or so like a normal cron job would do. 该任务将有一个while循环,并且每小时都会执行一些操作,就像正常的cron工作一样。

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? 有人对此有任何建议或经验吗?

Doing cron jobs in a web application is a bad idea. 在Web应用程序中执行cron作业是一个坏主意。 IIS could recycle the application at any time and your job will stop. IIS可以随时回收应用程序,您的工作将停止。 I would recommend you performing this in a separate windows service. 我建议您在单独的Windows服务中执行此操作。 You could take a look at Quartz.NET . 您可以看一下Quartz.NET Another possibility is a console application which does the job and which is scheduled within the Windows Scheduler. 另一种可能性是控制台应用程序,该应用程序可以执行此任务,并且可以在Windows Scheduler中进行调度。

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. 尽管Darin认为在Web应用程序中执行cron作业是一个坏主意(我在某种程度上同意他的观点),但如果明智地使用它并用于短期工作,它可能还不错。
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应用程序中使用相同的Quartz.NET可能会很不错,我正在像这样的一个项目中使用http://bugsquash.blogspot.com/2010/06/embeddable-quartznet-web-consoles.html进行小型工作而且运行良好-易于监视(比监视远程Windows进程更容易),可以在共享主机上使用。

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

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