简体   繁体   中英

Quartz.Net ASP.NET Status update

I'm using quartz in an asp.net site and I'm using it to fire off a longer background task. I want to be able to use a timer or jquery to call the job and get back status and progress info similar to how I was doing it before. Here is code that I was using:

protected void Timer1_Tick(object sender, EventArgs e)
{    
    if (MyTaskManager.Instance.IsTaskRunning)
    {
         if (MyTaskManager.Instance.TotalItems > 0) ProgressLabel.Text =   string.Format(ProgressLabel.Text, MyTaskManager.Instance.TotalItems, MyTaskManager.Instance.ItemsProcessed);
                    else ProgressLabel.Text = string.Format("Records Processed: {0}", MyTaskManager.Instance.ItemsProcessed);
    }
    else
    {
        Timer1.Enabled = false;
    }
}

Has anyone done this that could point me in the right direction?

It might not be what you're looking for but I've used SignalR with Quartz.Net and it works great.
I've published a simple application in my repository .

You have to create a custom Hub which you will use to interact with your ASP.NET page.
Your ( quartz.net ) job will interact your ASP.NET page through your Hub the same way.

Once you have installed ASP.NET SignalR :

Install-Package Microsoft.AspNet.SignalR

You can create a startup class to configure SignalR:

[assembly: OwinStartup(typeof(AspNetQuartSignalR.Startup))]

namespace AspNetQuartSignalR
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}

You have to reference a couple of scripts in your page:

  1. jquery-2.1.1.js
  2. jquery.signalR-2.1.1.js

and the automatically generated /signalr/hubs

Now you can create your own Hub :

public class QuartzHub : Hub
{
   ...
}

with methods which will allow you to interact with the scripts in your ASP.NET page.

Let's say your Hub has a method CheckQuartzStatus which gives you the status of all the quartz.net triggers configured:

public void CheckQuartzStatus()
{
    string message = string.Empty;

    var allTriggerKeys = Global.Scheduler.GetTriggerKeys(GroupMatcher<TriggerKey>.AnyGroup());
    foreach (var triggerKey in allTriggerKeys)
    {
    ITrigger trigger = Global.Scheduler.GetTrigger(triggerKey);
    message += string.Format("{0} = {1}", trigger.Key, Global.Scheduler.GetTriggerState(trigger.Key)) + Environment.NewLine;
    }

    Clients.All.onCheckQuartzStatus(message);
}

Your jQuery script can interact with this method in a very simple way:

quartz.server.checkQuartzStatus();

As you can see your Hub method at some point fires an action: onCheckQuartzStatus .
That is a call to an event defined in your javascript defined in the page :

quartz.client.onCheckQuartzStatus = function (message) {
    alert(message);
};

You can see how the interaction works looking at the script in the Default.aspx page.

You can read a lot more here .

You're going to have to build all of this functionality yourself. Quartz.Net jobs run in the threadpool and you don't have a way of referencing them from the scheduler. You could do this by having the job write its progress somewhere and then have your timer check that spot for progress.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM