Job Scheduler inside of MVC3 Website

2019-07-11 03:22发布

问题:

I'm looking for some guidance in getting a Job Scheduler setup for use INSIDE of my MVC3 website. I've been looking at Quartz.NET for this, but am having trouble finding guidance online as far as the setup to make sure it's running when the website is.

With the information above taken into consideration. I'm aware of a couple of the "drawbacks" to setting up a job scheduler inside of IIS. The jobs that I'm looking to setup are simple cleanup jobs where timing is rather unimportant so if IIS goes through and recycles the thread for the website it really just won't be a big deal. ie. If it runs 10 times a day due to recycling/restarting the website thread that would be fine/If it runs once a month because the thread isn't getting started that's fine as well (although quite unlikely).

I seem to recall a question on SO or a post through Google that showed setting it up inside of the Application_Start() in Global.asax, but can't find this info anywhere now. Does anyone have any recommendation on how to implement something similar to what I'm talking about?

回答1:

After some more searching I found this question on SO that talks about implementing Quartz.NET inside of the global.asax file.

To implement it I simply referenced the Quartz.dll in the new (at the time of this post) download package and used the following code...

    protected void Application_Start()
    {
        /*
         * Include other Application_Start() code...
         */

        //Job Scheduling
        try {
            //Setup the new Scheduler
            var sf = new StdSchedulerFactory();
            var sched = sf.GetScheduler();
            sched.Start();

            //Setup the Job
            var jobDetail = new JobDetailImpl( "myJob", null, typeof( DocCleanup ) );

            //Create 1 week trigger that will go on forever
            var trigger = new SimpleTriggerImpl( "jobTrigger", SimpleTriggerImpl.RepeatIndefinitely, new TimeSpan( 168, 0, 0 ) );
            trigger.StartTimeUtc = DateTimeOffset.Now;

            //Add the job to the scheduler
            sched.ScheduleJob( jobDetail, trigger );
        } catch (Exception ex) { //Implement Exception code... }
    }

I had no desire to create a console application/sql job/etc for this particular need. This is mainly due to the extreme un-reliability that is acceptable from it as mentioned in my question. With that said if you need ANY sort of guarantee as to when a job will run I doubt you will want to implement Quartz.NET in this manner.



回答2:

You can create a console application to do your Clean up job (I am not sure what kind of clean up you want to do). You can use the Windows Task Scheduler to invoke this app in a specific time interval to do your job

Another option is to Create an SQL Job which will be executed on a specific time interval. The Stored proc inside your Job can call managed code. So you can call your dot net code from that.

IF it is simply cleaning SQL tables, Just write an SQL job which does it.