From d4306f401dd1a1f9805b4c7472b79c64d9c7f22f Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 5 Sep 2022 17:11:22 +0800 Subject: [PATCH 1/3] Hangfire support specific queue --- .../Hangfire/HangfireBackgroundJobManager.cs | 26 +++++++++++++++++-- .../Hangfire/HangfireJobExecutionAdapter.cs | 4 ++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireBackgroundJobManager.cs b/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireBackgroundJobManager.cs index 2ebad0797f..2583ada9fc 100644 --- a/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireBackgroundJobManager.cs +++ b/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireBackgroundJobManager.cs @@ -1,6 +1,9 @@ using System; +using System.Reflection; using System.Threading.Tasks; using Hangfire; +using Hangfire.States; +using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; namespace Volo.Abp.BackgroundJobs.Hangfire; @@ -8,16 +11,35 @@ namespace Volo.Abp.BackgroundJobs.Hangfire; [Dependency(ReplaceServices = true)] public class HangfireBackgroundJobManager : IBackgroundJobManager, ITransientDependency { + protected AbpBackgroundJobOptions Options { get; } + + public HangfireBackgroundJobManager(IOptions options) + { + Options = options.Value; + } + public virtual Task EnqueueAsync(TArgs args, BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan? delay = null) { return Task.FromResult(delay.HasValue ? BackgroundJob.Schedule>( - adapter => adapter.ExecuteAsync(args), + adapter => adapter.ExecuteAsync(GetQueueName(typeof(TArgs)),args), delay.Value ) : BackgroundJob.Enqueue>( - adapter => adapter.ExecuteAsync(args) + adapter => adapter.ExecuteAsync(GetQueueName(typeof(TArgs)) ,args) )); } + + protected virtual string GetQueueName(Type argsType) + { + var queueName = EnqueuedState.DefaultQueue; + var queueAttribute = Options.GetJob(argsType).JobType.GetCustomAttribute(); + if (queueAttribute != null) + { + queueName = queueAttribute.Queue; + } + + return queueName; + } } diff --git a/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireJobExecutionAdapter.cs b/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireJobExecutionAdapter.cs index a2ad7dc77d..7ba0cd7db4 100644 --- a/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireJobExecutionAdapter.cs +++ b/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireJobExecutionAdapter.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using Hangfire; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; @@ -20,7 +21,8 @@ public class HangfireJobExecutionAdapter Options = options.Value; } - public async Task ExecuteAsync(TArgs args) + [Queue("{0}")] + public async Task ExecuteAsync(string queue, TArgs args) { if (!Options.IsJobExecutionEnabled) { From 46924c59a693fac86dfcaac7c517ba9409d28a60 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 5 Sep 2022 17:19:40 +0800 Subject: [PATCH 2/3] Update document --- docs/en/Background-Jobs-Hangfire.md | 35 ++++++++++++++++++++++++ docs/zh-Hans/Background-Jobs-Hangfire.md | 35 ++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/docs/en/Background-Jobs-Hangfire.md b/docs/en/Background-Jobs-Hangfire.md index 4ee58d84ef..869a8075af 100644 --- a/docs/en/Background-Jobs-Hangfire.md +++ b/docs/en/Background-Jobs-Hangfire.md @@ -80,6 +80,41 @@ After you have installed these NuGet packages, you need to configure your projec } ```` +### Specifying Queue + +You can use the [`QueueAttribute`](https://docs.hangfire.io/en/latest/background-processing/configuring-queues.html) to specify the queue. + +````csharp +using System.Threading.Tasks; +using Volo.Abp.BackgroundJobs; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Emailing; + +namespace MyProject +{ + [Queue("alpha")] + public class EmailSendingJob + : AsyncBackgroundJob, ITransientDependency + { + private readonly IEmailSender _emailSender; + + public EmailSendingJob(IEmailSender emailSender) + { + _emailSender = emailSender; + } + + public override async Task ExecuteAsync(EmailSendingArgs args) + { + await _emailSender.SendAsync( + args.EmailAddress, + args.Subject, + args.Body + ); + } + } +} +```` + ### Dashboard Authorization Hangfire Dashboard provides information about your background jobs, including method names and serialized arguments as well as gives you an opportunity to manage them by performing different actions – retry, delete, trigger, etc. So it is important to restrict access to the Dashboard. diff --git a/docs/zh-Hans/Background-Jobs-Hangfire.md b/docs/zh-Hans/Background-Jobs-Hangfire.md index b1bf7b8c79..fb899758cc 100644 --- a/docs/zh-Hans/Background-Jobs-Hangfire.md +++ b/docs/zh-Hans/Background-Jobs-Hangfire.md @@ -66,6 +66,41 @@ public class YourModule : AbpModule } ```` +### 指定队列 + +你可以使用 [`QueueAttribute`](https://docs.hangfire.io/en/latest/background-processing/configuring-queues.html) 来指定队列. + +````csharp +using System.Threading.Tasks; +using Volo.Abp.BackgroundJobs; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Emailing; + +namespace MyProject +{ + [Queue("alpha")] + public class EmailSendingJob + : AsyncBackgroundJob, ITransientDependency + { + private readonly IEmailSender _emailSender; + + public EmailSendingJob(IEmailSender emailSender) + { + _emailSender = emailSender; + } + + public override async Task ExecuteAsync(EmailSendingArgs args) + { + await _emailSender.SendAsync( + args.EmailAddress, + args.Subject, + args.Body + ); + } + } +} +```` + 1. 如果你想要使用Hangfire的面板,你可以在 `Module` 类的 `OnApplicationInitialization` 方法添加: `UseHangfireDashboard` ````csharp From fe3a2f8c250ed7bf25eae28e41d042165778f3a1 Mon Sep 17 00:00:00 2001 From: Ebicoglu Date: Mon, 5 Sep 2022 17:21:52 +0300 Subject: [PATCH 3/3] Give extra info when the token doesn't work --- .../Version/BranchGithubVersionProvider.cs | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/Version/BranchGithubVersionProvider.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/Version/BranchGithubVersionProvider.cs index 524eecb33c..cd3969a3ab 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/Version/BranchGithubVersionProvider.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/Version/BranchGithubVersionProvider.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Threading.Tasks; using Octokit; using Octokit.Internal; +using Volo.Abp; using Volo.Abp.DependencyInjection; namespace Volo.Docs.GitHub.Documents.Version @@ -12,10 +14,25 @@ namespace Volo.Docs.GitHub.Documents.Version { public async Task> GetVersions(string name, string repositoryName, string token) { - var client = GetGitHubClient(name, token); - var branches = await client.Repository.Branch.GetAll(name, repositoryName); + try + { + var client = GetGitHubClient(name, token); + var branches = await client.Repository.Branch.GetAll(name, repositoryName); - return branches.Select(b => new GithubVersion {Name = b.Name}).ToList(); + return branches.Select(b => new GithubVersion { Name = b.Name }).ToList(); + } + catch (Exception e) + { + var apiException = (Octokit.ApiException)e; + if (apiException.ApiError?.Message == "Bad credentials" || + apiException.StatusCode == HttpStatusCode.Unauthorized) + { + throw new AbpException("Invalid credentials for the GitHub repository: " + name + "/" + repositoryName + ". " + + "The GitHub access token: " + token + " does not work!" + "\n\rError: " + e); + } + + throw new AbpException("Could not get branches from GitHub repository: " + name + "/" + repositoryName + "\n\rError: " + e.Message); + } } private static GitHubClient GetGitHubClient(string name, string token)