-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
114 lines (102 loc) · 4.48 KB
/
Copy pathProgram.cs
File metadata and controls
114 lines (102 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using MassTransit;
using Company.Consumers;
using Hangfire;
using Hangfire.Azure;
using Hangfire.MemoryStorage;
using MassTransit.HangfireIntegration;
using Microsoft.Azure.Cosmos;
using Hangfire.SqlServer;
using Microsoft.Extensions.DependencyInjection;
namespace MtHangfire
{
public class Program
{
public static async Task Main(string[] args)
{
await CreateHostBuilder(args).Build().RunAsync();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
// ConfigureHangfireSqlServer(services);
ConfigureHangfireCosmosDb(services);
// ConfigureHangfireInMemory(services);
services.AddHostedService<Worker>();
services.AddMassTransit(x =>
{
x.AddMessageScheduler(new Uri("queue:scheduler"));
x.AddConsumer<MtConOneConsumer, MtConOneConsumerDefinition>();
x.AddConsumer<MtConTwoConsumer, MtConTwoConsumerDefinition>();
x.UsingAzureServiceBus((context, cfg) =>
{
cfg.Host(
"");
// cfg.UseHangfireScheduler(context.GetRequiredService<IHangfireComponentResolver>(),
// "scheduler");
cfg.UseHangfireScheduler("scheduler");
cfg.UseMessageScheduler(new Uri("queue:scheduler"));
cfg.ConfigureEndpoints(context);
});
});
});
private static void ConfigureHangfireCosmosDb(IServiceCollection services)
{
var options = new CosmosDbStorageOptions()
{
ExpirationCheckInterval = TimeSpan.FromMinutes(2),
CountersAggregateInterval = TimeSpan.FromMinutes(2),
QueuePollInterval = TimeSpan.Zero
// JobKeepAliveInterval = TimeSpan.FromMinutes(5)
};
var cosmoClientOptions = new CosmosClientOptions
{
ApplicationName = "hangfire",
RequestTimeout = TimeSpan.FromSeconds(60),
ConnectionMode = ConnectionMode.Direct,
MaxRetryAttemptsOnRateLimitedRequests = 3,
MaxRetryWaitTimeOnRateLimitedRequests = TimeSpan.FromSeconds(30)
};
// services.AddHangfireServer();
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseAzureCosmosDbStorage("",
"",
"SchedulerDB", "Schedules", storageOptions: options));
JobStorage.Current = CosmosDbStorage.Create("https://", "",
"SchedulerDB", "Schedules", cosmoClientOptions, options);
}
private static void ConfigureHangfireSqlServer(IServiceCollection services)
{
var connectionString = "";
JobStorage.Current = new SqlServerStorage(connectionString);
// Add Hangfire services.
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(connectionString, new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true
}));
}
private static void ConfigureHangfireInMemory(IServiceCollection services)
{
services.AddHangfire(config =>
{
config.UseMemoryStorage();
});
services.AddHangfireServer();
services.AddSingleton<IHangfireComponentResolver, HangfireComponentResolver>();
}
}
}