简体   繁体   中英

How to invoke an Action Method in a controller at a particular time

I am in the midst of designing a Job assigning System which is to be developed using ASP.NET MVC framework. One of the requirements is to check the status of a job (which is assigned to a job operator) after a particular time (can be 1 hr or 2 hr from the time of job assignment ). if the status is Rejected, teh system need to send an email to Admin.

I actually have an action method in a controller which will search the database for any rejected job and if it find any , an email will be send to the Admin. The email implementation (using .Net Postal library ) is also done inside the action Method.

So my question is

1.How to invoke that particular action method from the controller at a particular time (which can be set in the past. eg: trigger 2 hr from now).

2.Is creating a batch file and assigning it to Task scheduler or windows service is feasible in this scenario THAN implementing something which will check the database directly and send email every x minutes?

//Check this out for job scheduling

// using this you can make custom jobs that will trigger automatically after some time.

// Install package quartz

// create a job class

using System;
using Quartz;

namespace FooBar
{
    public class LoggingJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {


            Common.Logging.LogManager.Adapter.GetLogger("LoggingJob").Info(
                string.Format("Logging job : {0} {1}, and proceeding to log", 
                    DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));

        }
    }
}

// Now create your time activated job. Easy

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Common.Logging;

using Quartz;
using Quartz.Impl;

namespace FooBar
{
    class Program
    {

        private static ILog Log = LogManager.GetCurrentClassLogger();

        static void Main(string[] args)
        {
            try
            {
                // construct a scheduler factory
                ISchedulerFactory schedFact = new StdSchedulerFactory();

                // get a scheduler
                IScheduler sched = schedFact.GetScheduler();
                sched.Start();

                IJobDetail job = JobBuilder.Create<LoggingJob>()
                    .WithIdentity("myJob", "group1")
                    .Build();

                ITrigger trigger = TriggerBuilder.Create()
                    .WithDailyTimeIntervalSchedule
                    (s =>
                        s.WithIntervalInSeconds(10)
                            .OnEveryDay()
                            .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(10, 15))
                    )
                    .Build();

                sched.ScheduleJob(job, trigger);
            }
            catch (ArgumentException e)
            {
                Log.Error(e);
            } 
        }
    }
}

// use this for reference https://www.codeproject.com/Articles/860893/Scheduling-With-Quartz-Net

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