作业执行调用现有的控制器动作方法

时间:2018-06-02 13:57:18

标签: c# asp.net quartz-scheduler quartz.net

我有两种操作方法DayStart()DayEnd(),我必须致电DayStart() 上午8:05(周一至周五)和DayEnd()上午8:04(周一至周五)这是迄今为止Quartz.net的最佳选择,但我不知道如何打电话jobExecution方法中的这两个动作方法。

有没有办法实现这个目标?

public ActionResult StartDay()
{
  int userid = Auth.UserID;
  daysServices.StartDay(userid);
  return RedirectToAction("Index", "Home", new { isdayClose = false });
}

public ActionResult CloseDay()
{
  int userid = Auth.UserID;
  int dayId = daysServices.getActiveDay();
  int shiftId = daysServices.getActiveShift();
  daysServices.CLoseDay(dayId, userid, shiftId);           
  return RedirectToAction("TokenDayAmount", "Home", new { isdayClose = true });
}

public async Task Execute(IJobExecutionContext context)
{
  await Console.Out.WriteLineAsync("HelloJob is executing.");
  //I hope something like this? DayStart();
}

1 个答案:

答案 0 :(得分:1)

您需要三个步骤。首先创建一份工作

public class SomeJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
            DayStart(); //this is your start method
    }
}

其次,当您的工作执行时,您需要一个调度程序。您可以使用Cron Trigger安排工作,如

public class SomeJobScheduler
{
    public static void Start()
    {
        IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
        scheduler.Start();

        IJobDetail job = JobBuilder.Create<SomeJob>().Build();
        ITrigger trigger = TriggerBuilder.Create()
        .WithCronSchedule("0 05 8 ? * MON-FRI *") //This expression to schedule your job Mon-Fri 8.05 AM
        .Build();

        scheduler.ScheduleJob(job, trigger);
    }
}

您可以找到有关Cron Ttrigger Here

的更多信息

第三次致电Scheduler中的Global,asax.cs

 void Application_Start(object sender, EventArgs e)
    {
     SomeJobScheduler.Start();
    }