-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
253 lines (228 loc) · 8.19 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
using BtSqlBackupService.Entities;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Quartz;
using Quartz.Impl;
using Topshelf;
namespace BtSqlBackupService
{
public class BtSqlBackupService : ServiceControl
{
private static readonly IConfiguration AppSettingsConfiguration =
new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false, true)
.AddEnvironmentVariables()
.Build();
private static readonly SmtpClientSettings SmtpClientSettings =
AppSettingsConfiguration
.GetSection("SmtpClientSettings")
.Get<SmtpClientSettings>();
private static readonly string AppDirPath =
Path.GetDirectoryName(
Assembly.GetEntryAssembly()?.Location
);
/// <summary>
/// Full path to sql_commands.json file.
/// </summary>
private static readonly string SqlCommandsFilePath =
Path.Combine(AppDirPath, "sql_commands.json");
/// <summary>
/// Static <see cref="ILogger" /> instance for app-wide general logging.
/// </summary>
/// <remarks>
/// Logging configuration and filtering is loaded from appsettings.json.
/// A console logger and a Window Event Log logger are added by default.
/// </remarks>
private static readonly ILogger<BtSqlBackupService> Logger =
LoggerFactory.Create(builder =>
{
builder
.AddConfiguration(AppSettingsConfiguration)
.AddConsole(opt =>
{
opt.TimestampFormat = "dd-MM-yyyy H:mm:ss.ffff ";
})
.AddEventLog(); // Not supported on non-Windows targets!
}).CreateLogger<BtSqlBackupService>();
private readonly Mailer _mailer;
/// <summary>
/// Quartz <see cref="IScheduler" /> instance.
/// </summary>
private readonly IScheduler _scheduler;
/// <summary>
/// A list of the <see cref="SqlCommandEntity" /> that will be
/// scheduled to run by this service.
/// </summary>
private readonly List<SqlCommandEntity> _sqlCommands;
public BtSqlBackupService()
{
_sqlCommands = new List<SqlCommandEntity>();
_mailer = new Mailer(SmtpClientSettings, Logger);
_scheduler = new StdSchedulerFactory()
.GetScheduler()
.GetAwaiter()
.GetResult();
}
/// <summary>
/// Starts the service.
/// </summary>
/// <param name="hostControl"></param>
/// <returns>true if started successfully</returns>
public bool Start(HostControl hostControl)
{
Logger.LogInformation("Starting service.");
// Queue this task to run on a separate thread so the service isn't killed
// by the service startup timeout limit.
Task.Run(() => StartAsync(hostControl).ContinueWith(startTask =>
{
if (!startTask.Result)
// Gracefully stop the service because we failed to start
// asynchronously.
Stop(hostControl);
}));
return true;
}
/// <summary>
/// Stops the service.
/// </summary>
/// <param name="hostControl"></param>
/// <returns>true if stopped successfully</returns>
public bool Stop(HostControl hostControl)
{
Logger.LogInformation("Stopping service.");
return StopAsync(hostControl).GetAwaiter().GetResult();
}
/// <summary>
/// Schedules <see cref="SqlCommandEntity" />s.
/// </summary>
/// <returns></returns>
private async Task ScheduleSqlCommands()
{
foreach (var command in _sqlCommands)
{
Logger.LogInformation($"Scheduling SQL command job '{command.Name}'.");
// Give the job the SQL command and logger (available in its context).
var data = new JobDataMap
{
{"command", command},
{"logger", Logger},
{"mailer", _mailer}
};
var jobBuilder = JobBuilder
.Create<RunSqlCommandJob>()
.WithIdentity(command.Name, command.ConnectionString)
.WithDescription(command.Description)
.SetJobData(data);
var jobDetail = jobBuilder.Build();
var jobTrigger = TriggerBuilder.Create()
.WithIdentity(command.Name, command.ConnectionString)
.WithDescription(command.Description)
.WithCronSchedule(command.Cron)
.StartNow()
.Build();
await _scheduler.ScheduleJob(jobDetail, jobTrigger);
}
}
/// <summary>
/// Loads <see cref="SqlCommandEntity" /> jobs from the file represented by
/// the <see cref="SqlCommandsFilePath" /> property.
/// </summary>
/// <returns>true if successful</returns>
private bool LoadSqlCommandJobs()
{
Logger.LogInformation(
$"Loading SQL command jobs from '{SqlCommandsFilePath}'.");
var serializerOptions = new JsonSerializerOptions
{
AllowTrailingCommas = true,
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
try
{
var json = File.ReadAllText(SqlCommandsFilePath);
var jobs =
JsonSerializer.Deserialize<SqlCommandEntity[]>(json,
serializerOptions);
_sqlCommands.AddRange(jobs);
}
catch (Exception e)
{
Logger.LogError(
$"Failed to load SQL command jobs from '{SqlCommandsFilePath}': {e.Message}");
return false;
}
return true;
}
/// <summary>
/// Loads the jobs and starts the scheduler.
/// </summary>
/// <param name="hostControl"></param>
/// <returns>true if successful</returns>
private async Task<bool> StartAsync(HostControl hostControl)
{
await _mailer.SendEmailAsync
(
SmtpClientSettings.AdminEmail,
$"[{Environment.MachineName}] Starting SQL Backup Service",
$"{DateTime.Now.ToString(CultureInfo.CurrentCulture)}:<br/>Starting SQL Backup Service on {Environment.MachineName} running {Environment.OSVersion}."
);
if (!LoadSqlCommandJobs())
{
await _mailer.SendEmailAsync
(
SmtpClientSettings.AdminEmail,
$"[{Environment.MachineName}] Failed to start the service: Failed to load jobs.",
$"{DateTime.Now.ToString(CultureInfo.CurrentCulture)}:<br/>Failed to start the service: Failed to load jobs on {Environment.MachineName} running {Environment.OSVersion}."
);
// Failed to load jobs from disk so don't start the service.
return false;
}
await ScheduleSqlCommands();
await _scheduler.Start();
return true;
}
/// <summary>
/// Stops the scheduler.
/// </summary>
/// <param name="hostControl"></param>
/// <returns>Always true</returns>
private async Task<bool> StopAsync(HostControl hostControl)
{
await _mailer.SendEmailAsync
(
SmtpClientSettings.AdminEmail,
$"[{Environment.MachineName}] Stopping SQL Backup Service",
$"{DateTime.Now.ToString(CultureInfo.CurrentCulture)}:<br/>Stopping SQL Backup Service on {Environment.MachineName} running {Environment.OSVersion}."
);
await _scheduler.Shutdown();
return true;
}
}
internal static class Program
{
public static int Main(string[] args)
{
return (int) HostFactory.Run(hostConfigurator =>
{
hostConfigurator.Service<BtSqlBackupService>();
hostConfigurator.RunAsNetworkService();
hostConfigurator.StartAutomatically();
hostConfigurator.SetStartTimeout(TimeSpan.FromSeconds(30));
hostConfigurator.SetDisplayName(Constants.displayName);
hostConfigurator.SetServiceName(Constants.serviceName);
hostConfigurator.SetDescription(Constants.description);
hostConfigurator.EnableServiceRecovery(action =>
action.RestartService(TimeSpan.FromSeconds(60)));
}
);
}
}
}