How to maintain a job history using Quartz scheduler

后端 未结 3 644
暗喜
暗喜 2021-02-02 02:46

I\'d like to maintain a history of jobs that were scheduled by a Quartz scheduler containing the following properties: \'start time\', \'end time\', \'success\', \'error\'.

3条回答
  •  遇见更好的自我
    2021-02-02 03:05

    If you're happy to just update the database at the end, you can get the job name and run time from the IJobExecutionContext:

    public void JobWasExecuted(JobExecutionContext context,
                           JobExecutionException jobException)
    {
        var sql = @"INSERT INTO MyJobAuditHistory 
                    (JobKey, RunTime, Status, Exception) VALUES (?, ?, ?, ?)";
    
        // create command etc.
        command.Parameters.Add(context.JobDetail.Key.ToString());
        command.Parameters.Add(context.JobRunTime);
        command.Parameters.Add(jobException == null ? "Success" : "Fail");
        command.Parameters.Add(jobException == null ? null : jobException.Message);
    }
    

提交回复
热议问题