Sequential jobs by queue

Trying to understand if I can do this in Hangfire.

I have an ASP.NET WEB API application. The application takes requests from users to print labels on Zebra printers that exist on our internal network.

It’s working okay right now, but when the user wants to print a large number of labels with a single request it can take some time which holds up the API.

What I want to do is get the request from the user, send back a 200 OK and put the label into a queue to be printed.

A couple of caveats.

  1. I need the labels to print in order that they were requested.
  2. If a label is printing to printer, we can not send more labels there until that job is done.
  3. If a label is requested to printer then it can print right away.

My thought was to have a queue for each printer and process them in the order received.

Is this something that I can do with Hangfire? What challenges can I expect?

What if the print job fails, can I put it back in the queue? What if it fails multiple times, can I then cancel it so it doesn’t try to print again?

Take a look at this chapter in the documentation:

By starting one worker instance for each queue (as suggested in one of the comments below the doc chapter), and setting the degree of parallelism to 1, you would get the jobs done one by one in the order that they are added.

I think… :wink:

Thanks, I’ll give that a try.

Hi, did you resolved your problem?

that how i test out :slight_smile:

var options = new BackgroundJobServerOptions
// This is the default value
WorkerCount = 1
// Not WorkerCount = Environment.ProcessorCount * 5
// if u pc or server have more than one Processor, then it will not
// process in sequential order

using (var server = new BackgroundJobServer(options))