Jan de Jager
Jan de Jager

Reputation: 870

Quartz.Net Job Progress

Is there an easy way for a IJob to publish data from within the Job. I have a Job running an import process, but would like to retrieve the progress by looking up the Job and requesting it.

I thought that i may be able to use the JobDetails, but does not seem to reflect the changes made within the Job?

Upvotes: 5

Views: 5410

Answers (2)

Evgenyt
Evgenyt

Reputation: 10721

It is quite possible.

I would go with something like this. In job:

public virtual void Execute(IJobExecutionContext context)
{
    int i = 0;
    while (i++ < 100)
    {
        context.JobDetail.JobDataMap["progress"] = i;
        Thread.Sleep(1000);
    }
}

In code that wants to retrieve progress:

var jobs = scheduler.GetCurrentlyExecutingJobs();
foreach (var j in jobs)
{
    Console.WriteLine("Progress of {0} is {1}", 
        j.JobDetail.Key, 
        j.JobDetail.JobDataMap["progress"]);
}

You can filter result of GetCurrentlyExecutingJobs, of course.

Also, you can set job to be Durable and PersistJobDataAfterExecution, so after it is done, you can retrieve its progress and anything else via scheduler.GetJobDetail(..) and accessing its JobDataMap afterwards.

Upvotes: 9

Marko Lahma
Marko Lahma

Reputation: 6884

There isn't a built-in way that would give you easily the thing you are looking for. But it isn't that hard to publish progress info to some other facility via publish subscribe either. You could also periodically write progress info to persistent storage - that would also give you the benefit of easy retry when you know the last successful bit that was processed.

An outside thread/process could also determine the status if it polled the target where you are importing and you would have somewhere the info about total items that are going to be imported.

Quartz.NET jobs are units that are processed as an.. well unit and so there isn't the notion of how much there is yet to be done. Or do you need this info in another job that depends on import process?

Upvotes: 2

Related Questions