Skip to content

Commit

Permalink
Merge pull request #6410 from abpframework/maliming/hangfire-async
Browse files Browse the repository at this point in the history
Make HangfireJobExecutionAdapter async.
  • Loading branch information
realLiangshiwei committed Dec 1, 2020
2 parents c615aed + 3840a42 commit 1b36843
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,14 @@ public class HangfireBackgroundJobManager : IBackgroundJobManager, ITransientDep
public virtual Task<string> EnqueueAsync<TArgs>(TArgs args, BackgroundJobPriority priority = BackgroundJobPriority.Normal,
TimeSpan? delay = null)
{
if (!delay.HasValue)
{
return Task.FromResult(
BackgroundJob.Enqueue<HangfireJobExecutionAdapter<TArgs>>(
adapter => adapter.Execute(args)
)
);
}
else
{
return Task.FromResult(
BackgroundJob.Schedule<HangfireJobExecutionAdapter<TArgs>>(
adapter => adapter.Execute(args),
delay.Value
)
);
}
return Task.FromResult(delay.HasValue
? BackgroundJob.Schedule<HangfireJobExecutionAdapter<TArgs>>(
adapter => adapter.ExecuteAsync(args),
delay.Value
)
: BackgroundJob.Enqueue<HangfireJobExecutionAdapter<TArgs>>(
adapter => adapter.ExecuteAsync(args)
));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.Threading;

namespace Volo.Abp.BackgroundJobs.Hangfire
{
Expand All @@ -22,7 +20,7 @@ public HangfireJobExecutionAdapter(
Options = options.Value;
}

public void Execute(TArgs args)
public async Task ExecuteAsync(TArgs args)
{
if (!Options.IsJobExecutionEnabled)
{
Expand All @@ -39,8 +37,8 @@ public void Execute(TArgs args)
{
var jobType = Options.GetJob(typeof(TArgs)).JobType;
var context = new JobExecutionContext(scope.ServiceProvider, jobType, args);
AsyncHelper.RunSync(() => JobExecuter.ExecuteAsync(context));
await JobExecuter.ExecuteAsync(context);
}
}
}
}
}

0 comments on commit 1b36843

Please sign in to comment.