Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use async/await in Hangfire job ?

I recently started using Hangfire to handle my background jobs in an ASP.NET project.

My jobs are involving lots, lots (and lots) of HTTP calls using WebRequest (I may migrate to something else, but that's for later).

While browsing SO on this subject, I stumbled upon people who do async calls in hangfire.

Is there any point in using async/await tasks inside a Hangfire job ? I'm aware that it is a queue, so jobs won't execute concurrently, but since my project has no GUI, is there any reason I would use async/await to do my HTTP calls inside a Hangfire job ?

PS : The requests that are involving a long-running job use the Task "design pattern", so clients won't experience any timeout

like image 387
Arthur Attout Avatar asked Sep 19 '25 21:09

Arthur Attout


1 Answers

You can absolutely use Hangfire to run async methods, and use awaits within those async methods.

The SO question you linked to was making a blocking Wait call on the method passed to Hangfire to be serialized and then later executed by the hangfire service.

This is very different from something like

hangfireClient.Enqueue(() => myClass.RunAsync());

where RunAsync is an async method that contains awaits.

public async Task<bool> RunAsync() {
    //do your work
    var subJob = new worker();
    return await subJob.DoWork();
}
like image 150
Mattkwish Avatar answered Sep 23 '25 11:09

Mattkwish