forked from chrismroberts/csharp-microservice-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
74 lines (66 loc) · 2.49 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Coravel;
using Serilog;
using Serilog.Formatting.Json;
namespace OrderProcessingWorker
{
public class Program
{
public static void Main(string[] args)
{
DotNetEnv.Env.Load();
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File("logs/log.txt",
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information,
rollingInterval: RollingInterval.Day)
.WriteTo.File("logs/errorlog.txt", restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Warning)
.CreateLogger();
try
{
Log.Information("Starting service..");
var host = CreateHostBuilder(args).Build();
host.Services.UseScheduler(scheduler =>
{
var jobSchedule = scheduler.Schedule<ProcessOrder>();
jobSchedule
.EverySeconds(2)
.PreventOverlapping("ProcessOrderJob");
});
host.Run();
}
catch (System.Exception ex)
{
Log.Fatal(ex, "Exception in application");
}
finally
{
Log.Information("Exiting service");
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureServices((hostContext, services) =>
{
var smtpHost = Environment.GetEnvironmentVariable("SMTP_HOST");
var smtpUser = Environment.GetEnvironmentVariable("SMTP_USER");
var smtpPass = Environment.GetEnvironmentVariable("SMTP_PASS");
services
.AddFluentEmail("hello@morrotec.co.uk")
.AddRazorRenderer()
.AddSmtpSender(smtpHost, 587, smtpUser, smtpPass);
services.AddSingleton<IOrderConnector, OrderQueueConnector>();
services.AddScheduler();
services.AddTransient<ProcessOrder>();
});
}
}
}