HangfireIO / Hangfire

An easy way to perform background job processing in .NET and .NET Core applications. No Windows Service or separate process required
https://www.hangfire.io
Other
9.45k stars 1.71k forks source link

Async support for storage API #401

Open sergeyzwezdin opened 9 years ago

sergeyzwezdin commented 9 years ago

Modern data access libraries are migrating to async methods in their API. For example, updated MongoDB driver rewritten with async support.

On the other hand, HangFire have synchronous API for internal objects like IStorageConnection, IFetchedJob, IMonitoringApi, IWriteOnlyTransaction and so on.

It forces to use workarounds like AsyncHelper to build custom storage for HangFire.

So my suggestion is to update API in async way (e.g. return Task<TResult> instead of TResult).

odinserj commented 9 years ago

I'm suggesting to use async methods too to keep up with the times, but the changes will be introduced in 2.0 version (next year), because of a lot of breaking changes. I'm planning to make background processes async as well (#150) to gain more opportunities from async storage methods.

sergeyzwezdin commented 9 years ago

Really cool news. Do you have an idea about dates at next year? I will need to make these changes for mongo as well.

Gillardo commented 8 years ago

Any news on async support yet? Just started using Hangfire, but looks like i may have to stop as it seems it does current support my codebase and i aint updating it all to run using non-await calling just to use hangfire

IanYates commented 8 years ago

You can use

var task = Task.Run( async ()=>   {
   var x = bleh;
   await y();
   return z;
});

task.Wait();

It's OK (not perfect) from within Hangfire - there won't be any ugly deadlock. However you will have one of Hangfire's thread's waiting on your separately executing Task to finish. Not the end of the world.

You can also use https://github.com/StephenCleary/AsyncEx/wiki/AsyncContext to do something similar.

Having said that, proper async support in v2.0 next year will be awesome :)

mlewski commented 8 years ago

+1, @odinserj, per comment above, could you add to the 2.0 issue-milestone?

anasmohad commented 7 years ago

Till the Hangfire supports async/await, is it a good idea to encapsulate the call of the jobs creations methods within fire and forget APIs such as HostingEnvironment.QueueBackgroundWorkItem or StephenCleary/AspNetBackgroundTasks ?

examples:

HostingEnvironment.QueueBackgroundWorkItem((cancelationToken) => BackgroundJob.Enqueue(() => SomeClass.DoWork()));

or

BackgroundTaskManager.Run(() =>
{
  BackgroundJob.Enqueue(() => SomeClass.DoWork());
});