简体   繁体   English

如何在作业执行期间将数据保存到JobDataMap,然后再访问?

[英]How to save data to JobDataMap during job execution and access it after?

I'm currently working on some kind of simplified wrapper around Quartz.net to be able to manage all registered and running in background jobs, display additional information about job execution progress, percentage, return some kind of messages about current job state, etc. If, for example, my current job is posting data to social media, I want to see which social media is data currently posted to, or if job is sending emails I want to see in real time how many email messages are currently generated, how many are already sent, how many left, etc. For this purpose I save data to IJobExecutionContext (context.JobDetail.JobDataMap) in job Execute method. 我目前正在Quartz.net上进行某种简化的包装,以能够管理所有在后台作业中注册和运行的程序,显示有关作业执行进度,百分比的其他信息,返回有关当前作业状态的某种消息等。例如,如果我当前的工作是将数据发布到社交媒体上,我想查看当前数据发布到哪个社交媒体,或者如果该工作正在发送电子邮件,我想实时查看当前生成了多少电子邮件,如何为此,我已经将数据保存到IJobExecutionContext (context.JobDetail.JobDataMap)中的作业Execute方法中。 Then use my manager to get all currently executing jobs: 然后使用我的经理获取当前执行的所有作业:

public List<IJobExecutionContext> RunningJobs
{
     get { return Scheduler.GetCurrentlyExecutingJobs().ToList(); }
}

and display details fetched from JobDataMap of each IJobExecutionContext . 并显示从每个IJobExecutionContext JobDataMap获取的详细信息。 This kinda works fine. 这样还行。 But the problem is that if, for example, job execution has failed I want to log (save) as much data as possible, including exception object itself, in JobDataMap . 但是问题是,例如,如果作业执行失败,我想在JobDataMap记录(保存)尽可能多的数据,包括异常对象本身。 But, the problem is, when job execution is completed context object is disposed. 但是,问题是,当作业执行完成时,将放置上下文对象。 And I lost all of that data. 我丢失了所有这些数据。 So when I try to fetch all JobDetail of currently scheduled jobs: 因此,当我尝试获取当前计划作业的所有JobDetail时:

public List<IJobDetail> AllJobs
{
     get
     {
         var result = new List<IJobDetail>();
         var jobGroups = Scheduler.GetJobGroupNames();

         foreach(string group in jobGroups)
         {
            var groupMatcher = GroupMatcher<JobKey>.GroupContains(group);
            var jobKeys = Scheduler.GetJobKeys(groupMatcher);

            foreach(var jobKey in jobKeys)
            {
                var detail = Scheduler.GetJobDetail(jobKey);
                result.Add(detail);
            }
         }

         return result;
     }
}

I can not see the data I saved during job execution to detail.JobDataMap . 我看不到作业执行期间保存到detail.JobDataMap But I still can see data saved when job was initialized/scheduled, before it actually being executed. 但是我仍然可以看到在作业实际执行之前初始化/计划作业时保存的数据。 As an option I can catch the exception an log it during the execution (which I am actually doing) but I want to mark this job as failed and display the cause of the fail in my Jobs detail view. 作为一种选择,我可以在执行期间(实际上是在执行中)捕获该异常的日志,但是我想将此作业标记为失败并在“作业”详细信息视图中显示失败的原因。 Which takes data from my manager (which is like some kind of repository of my jobs) using List<IJobDetail> AllJobs (implemented above). 它使用List<IJobDetail> AllJobs (在上面实现)从我的经理那里获取数据(就像我的工作的某种存储库)。

So do I have to use some kind of additional job details storage to save this data during execution and then fetch all scheduled jobs and map this data by job ids or something. 因此,我是否必须使用某种附加的作业详细信息存储来在执行期间保存此数据,然后获取所有计划的作业并按作业ID或其他内容映射此数据。 Which is not the solution I would prefer. 这不是我想要的解决方案。 Or it there actually a way to save data to JobDataMap on execution and access this data after execution? 还是实际上有一种在执行时将数据保存到JobDataMap并在执行后访问此数据的方法?

After some research I've discovered that there are two types of jobs in Quartz: stateful and stateless (non stateful jobs). 经过一些研究,我发现Quartz中有两种类型的作业:有状态和无状态(非有状态的作业)。

Stateful job instances follow slightly different rules from regular Job instances. 有状态作业实例遵循的规则与常规作业实例略有不同。 The key difference is that their associated JobDataMap is re-persisted after every execution of the job, thus preserving state for the next execution. 关键区别在于,每次执行作业后都会重新关联其关联的JobDataMap,从而保留下一次执行的状态。 The other difference is that stateful jobs are not allowed to execute concurrently, which means new triggers that occur before the completion of the execute(xx) method will be delayed. 另一个区别是有状态作业不允许同时执行,这意味着在execute(xx)方法完成之前发生的新触发器将被延迟。

So to achieve my goal I just have to decorate my job class with PersistJobDataAfterExecution and DisallowConcurrentExecution attributes which will make my job stateful and it will persist it's JobDataMap after execution. 因此,要实现我的目标,我只需要用PersistJobDataAfterExecutionDisallowConcurrentExecution属性装饰我的工作类,这将使我的工作有状态,并且在执行后将其持久保存为JobDataMap。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 执行前访问JobDatamap - Access JobDatamap before execution 在设置期间将 JobDataMap 添加到作业时,Quarts.net 调度程序无法添加作业 SQL 表 - Quarts.net scheduler fails to add jobs SQL table when JobDataMap is added to job during setup 在程序执行期间,如何告诉不同的类保存到同一文件夹? - How to tell different classes to save to the same folder during execution of a program? 执行后MS Access DB不保存更改(C#) - MS Access DB doesnt save changes after execution (C#) IIS重启后如何防止hangfire重复执行作业? - How to prevent hangfire recurring job execution after IIS restart? 返回数据后如何继续执行方法? - How to continue execution of method after returning data? 如何在执行期间暂停、保存状态并稍后从同一点继续? - How do I pause during execution, save state, and continue from same point later on? 如何在ReactiveCommand执行期间禁用按钮,然后在完成后重新启用? - How to disable a button during ReactiveCommand execution, then re-enable after finished? 执行后/执行期间,如何从ASP NET检索SSIS包日志? (C#) - How do I retrieve SSIS package logging from ASP NET after/during execution? (C#) 在作业完成完全执行后调用函数 - Call a function after the Job has completed Full Execution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM