简体   繁体   中英

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. 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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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