-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
70 lines (57 loc) · 2.14 KB
/
Program.cs
File metadata and controls
70 lines (57 loc) · 2.14 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
using BackupService;
using Microsoft.Extensions.Logging.EventLog;
var builder = Host.CreateApplicationBuilder(args);
var backupOptions = builder.Configuration.GetSection("BackupOptions").Get<BackupOptions>();
if (backupOptions == null || !backupOptions.Backups.Any())
{
Console.WriteLine("No backups found in configuration!");
return;
}
builder.Services.Configure<BackupOptions>(
builder.Configuration.GetSection(BackupOptions.SectionName));
builder.Services.Configure<FileLoggerOptions>(
builder.Configuration.GetSection(FileLoggerOptions.SectionName));
builder.Services.AddSingleton<FtpBackupRunner>();
builder.Services.AddSingleton<FtpUploadRunner>();
builder.Services.AddSingleton<HttpBackupRunner>();
builder.Services.AddSingleton<EmailNotificationService>();
builder.Services.AddSingleton<BackupCoordinator>();
builder.Services.AddHostedService<Worker>();
builder.Services.AddWindowsService(options =>
{
options.ServiceName = "BackupService";
});
builder.Logging.ClearProviders();
builder.Logging.AddEventLog(new EventLogSettings
{
LogName = "Application",
SourceName = "BackupService"
});
builder.Logging.AddFileLogger(
builder.Configuration.GetSection(FileLoggerOptions.SectionName));
int starHour = builder.Configuration.GetValue<int>("ServiceSettings:StartHour");
int startMinute = builder.Configuration.GetValue<int>("ServiceSettings:StartMinute");
ServiceScheduler.StartServiceAtConfiguredTime(starHour, startMinute, () =>
{
Console.WriteLine("The timer worked, the service starts!");
});
var host = builder.Build();
var coordinator = host.Services.GetRequiredService<BackupCoordinator>();
try
{
Console.WriteLine("Starting initial backup test for all configured jobs...");
bool allSuccessful = await coordinator.RunBackupsAsync(CancellationToken.None);
if (allSuccessful)
{
Console.WriteLine("Initial backup test sequence completed successfully!");
}
else
{
Console.WriteLine("Initial backup test sequence FAILED! Check the logs above for details.");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred during the initial test: {ex.Message}");
}
host.Run();