JobData is not persisted after each execution in Q

2019-07-04 10:36发布

I have a job where I want to keep track of the 50 latest runs. For some reason it doesn't seem like the state is stored in my simple prototyp:

[PersistJobDataAfterExecution]
public class ApiJob : IJob
{
    private const string JobRunsKey = "jobRuns";
    private const int HistoryToKeep = 50;
    private const string UrlKey = "Url";

    public void Execute(IJobExecutionContext context)
    {
        var jobDataMap = context.JobDetail.JobDataMap;

        var url = context.JobDetail.JobDataMap.GetString(UrlKey);
        var client = new RestClient(url);
        var request = new RestRequest(Method.POST);
        var response = client.Execute(request);
        var runs = new List<JobRun>();
        if (jobDataMap.ContainsKey(JobRunsKey))
        {
            runs = (List<JobRun>) jobDataMap[JobRunsKey];
        }

        Console.WriteLine("I'm running so fast!");

        runs.Insert(0, new JobRun(){Message = "Hello", Result = JobResult.Ok, TimeForRun = DateTime.UtcNow});
        while (runs.Count > HistoryToKeep)
        {
            runs.RemoveAt(HistoryToKeep);
        }
        jobDataMap.Put(JobRunsKey, runs);
     }
}

I try to store the new list with jobDataMap.Put(JobRunsKey, runs) but the next time I trigger the job the key is missing from the JobDataMap. Any suggestions?

标签: c# quartz.net
1条回答
Explosion°爆炸
2楼-- · 2019-07-04 11:14

You probably don't have your JobRun class marked as Serializable.

This should work

[Serializable]
public class JobRun
{
    public string Message       ;
    public string Result        ;
    public DateTime TimeForRun  ;
}
查看更多
登录 后发表回答