Tracking the progress¶

There are two ways to implement this task: polling and pushing. Polling is easier to understand, but server push is a more comfortable way, because it helps you to avoid unnecessary calls to server. Plus, SignalR greatly simplifies the latter task.

I’ll show you a simple example, where client only needs to check for a job completion. You can see the full sample in Hangfire.Highlighter project.

Highlighter has the following background job that calls an external web service to highlight code snippets:

  1. public void Highlight(int snippetId)
  2. {
  3. var snippet = _dbContext.CodeSnippets.Find(snippetId);
  4. if (snippet == null) return;
  5.  
  6. snippet.HighlightedCode = HighlightSource(snippet.SourceCode);
  7. snippet.HighlightedAt = DateTime.UtcNow;
  8.  
  9. _dbContext.SaveChanges();
  10. }

Polling for a job status¶

When can we say that this job is incomplete? When the HighlightedCode property value is null. When can we say it was completed? When the specified property has value – this example is simple enough.

So, when we are rendering the code snippet that is not highlighted yet, we need to render a JavaScript that makes ajax calls with some interval to some controller action that returns the job status (completed or not) until the job was finished.

  1. public ActionResult CheckHighlighted(int snippetId)
  2. {
  3. var snippet = _db.Snippets.Find(snippetId);
  4.  
  5. return snippet.HighlightedCode == null
  6. ? new HttpStatusCodeResult(HttpStatusCode.NoContent)
  7. : Content(snippet.HighlightedCode);
  8. }

When code snippet becomes highlighted, we can stop the polling and show the highlighted code. But if you want to track progress of your job, you need to perform extra steps:

  • Add a column Status to the snippets table.
  • Update this column during background work.
  • Check this column in polling action.
    But there is a better way.

Using server push with SignalR¶

Why do we need to poll our server? It can say when the snippet becomes highlighted itself. And SignalR, an awesome library to perform server push, will help us. If you don’t know about this library, look at it, and you’ll love it. Really.

I don’t want to include all the code snippets here (you can look at the sources of this sample). I’ll show you only the two changes that you need, and they are incredibly simple.

First, you need to add a hub:

  1. public class SnippetHub : Hub
  2. {
  3. public async Task Subscribe(int snippetId)
  4. {
  5. await Groups.Add(Context.ConnectionId, GetGroup(snippetId));
  6.  
  7. // When a user subscribes a snippet that was already
  8. // highlighted, we need to send it immediately, because
  9. // otherwise she will listen for it infinitely.
  10. using (var db = new HighlighterDbContext())
  11. {
  12. var snippet = await db.CodeSnippets
  13. .Where(x => x.Id == snippetId && x.HighlightedCode != null)
  14. .SingleOrDefaultAsync();
  15.  
  16. if (snippet != null)
  17. {
  18. Clients.Client(Context.ConnectionId)
  19. .highlight(snippet.Id, snippet.HighlightedCode);
  20. }
  21. }
  22. }
  23.  
  24. public static string GetGroup(int snippetId)
  25. {
  26. return "snippet:" + snippetId;
  27. }
  28. }

And second, you need to make a small change to your background job method:

  1. public void HighlightSnippet(int snippetId)
  2. {
  3. ...
  4. _dbContext.SaveChanges();
  5.  
  6. var hubContext = GlobalHost.ConnectionManager
  7. .GetHubContext<SnippetHub>();
  8.  
  9. hubContext.Clients.Group(SnippetHub.GetGroup(snippet.Id))
  10. .highlight(snippet.HighlightedCode);
  11. }

And that’s all! When user opens a page that contains unhighlighted code snippet, his browser connects to the server, subscribes for code snippet notification and waits for update notifications. When background job is about to be done, it sends the highlighted code to all subscribed users.

If you want to add progress tracking, just add it. No additional tables and columns required, only JavaScript function. This is an example of real and reliable asynchrony for ASP.NET applications without taking much effort to it.

原文:
原文:

http://docs.hangfire.io/en/latest/background-processing/tracking-progress.html