-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
53 lines (44 loc) · 1.78 KB
/
Copy pathProgram.cs
File metadata and controls
53 lines (44 loc) · 1.78 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
using Hangfire;
using Hangfire.Console;
using Hangfire.Server;
using Hangfire.PostgreSql;
using HangfireExample;
using HangfireExample.Jobs;
if(Environment.GetEnvironmentVariable("POSTGRES_HOST") == null)
DotNetEnv.Env.Load(".env.local");
var builder = WebApplication.CreateBuilder(args);
var connStringTemplate = builder.Configuration.GetConnectionString("HangfireConnection");
string connString = connStringTemplate
.Replace("${POSTGRES_HOST}", Environment.GetEnvironmentVariable("POSTGRES_HOST") ?? "localhost")
.Replace("${POSTGRES_DB}", Environment.GetEnvironmentVariable("POSTGRES_DB") ?? "hangfire_db")
.Replace("${POSTGRES_USER}", Environment.GetEnvironmentVariable("POSTGRES_USER") ?? "postgres")
.Replace("${POSTGRES_PASSWORD}", Environment.GetEnvironmentVariable("POSTGRES_PASSWORD") ?? "postgres");
var factory = new PostgresConnectionFactory(connString);
builder.Services.AddHangfire(config =>
{
config.UsePostgreSqlStorage((options)=>
options.UseConnectionFactory(factory)
);
config.UseConsole();
});
builder.Services.AddHangfireServer();
builder.Services.AddTransient<MyJob>();
builder.Services.AddTransient<MaintenanceJob>();
builder.Services.AddSingleton<IConnectionFactory>(factory);
var app = builder.Build();
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
Authorization = new[] { new AllowAllAuthorizationFilter() }
});
var scope = app.Services.CreateScope();
// Или повторяющаяся задача:
RecurringJob.AddOrUpdate<MyJob>(
"my-job",
job => job.Run((PerformContext)null),
"*/5 * * * * *");
RecurringJob.AddOrUpdate<MaintenanceJob>(
"maintenance-cleanup",
job => job.CleanupAsync((PerformContext)null),
Cron.Minutely);
app.MapGet("/", () => "Hangfire with PostgreSQL is running!");
app.Run();