-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathProgram.cs
More file actions
592 lines (495 loc) · 19.2 KB
/
Program.cs
File metadata and controls
592 lines (495 loc) · 19.2 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
using System;
using Autofac;
using Microsoft.Extensions.Logging;
using Quidjibo;
using Quidjibo.Autofac.Extensions;
using Quidjibo.Autofac.Modules;
using Quidjibo.DataProtection.Extensions;
using Quidjibo.Extensions;
using Quidjibo.Models;
using Quidjibo.SqlServer.Extensions;
using Resgrid.Config;
using Resgrid.Model.Helpers;
using Resgrid.Model.Providers;
using Resgrid.Model.Services;
using Resgrid.Providers.Bus;
using Resgrid.Workers.Console.Commands;
using Resgrid.Workers.Framework;
using System.Configuration;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Resgrid.Workers.Console.Tasks;
using Serilog.Formatting.Json;
using Stripe;
using FluentMigrator.Runner;
using Resgrid.Providers.Migrations.Migrations;
using Resgrid.Model.Repositories;
using System.Reflection;
using Sentry;
using Resgrid.Framework;
using Resgrid.Providers.MigrationsPg.Migrations;
using Quidjibo.Clients;
using Quidjibo.Postgres.Extensions;
namespace Resgrid.Workers.Console
{
public class Program
{
public static IConfigurationRoot Configuration { get; private set; }
static async Task Main(string[] args)
{
#if DEBUG
Resgrid.Config.SystemBehaviorConfig.DoNotBroadcast = true;
#endif
System.Console.WriteLine("Resgrid Worker Engine");
System.Console.WriteLine("-----------------------------------------");
LoadConfiguration(args);
Prime();
var builder = new HostBuilder()
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
config.AddEnvironmentVariables();
if (args != null)
{
config.AddCommandLine(args);
}
})
.ConfigureServices((hostContext, services) =>
{
services.AddOptions();
var upgradeDatabase = Environment.GetEnvironmentVariable("RESGRID__DODBUPGRADE");
var runDatabaseUpgrade = string.Equals(upgradeDatabase, "true", StringComparison.OrdinalIgnoreCase);
services.AddSingleton(new DatabaseUpgradeState(runDatabaseUpgrade));
if (runDatabaseUpgrade)
{
services.AddSingleton<IHostedService, DatabaseUpgradeService>();
}
services.AddSingleton<IHostedService, QueuesProcessingService>();
services.AddSingleton<IHostedService, ScheduledJobsService>();
})
.ConfigureLogging((hostingContext, logging) => {
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
});
Resgrid.Framework.Logging.Initialize(ExternalErrorConfig.ExternalErrorServiceUrlForWebjobs);
if (!String.IsNullOrWhiteSpace(ExternalErrorConfig.ExternalErrorServiceUrlForWebjobs))
{
SentrySdk.Init(options =>
{
options.Dsn = Config.ExternalErrorConfig.ExternalErrorServiceUrlForWebjobs;
options.AttachStacktrace = true;
options.SendDefaultPii = true;
options.AutoSessionTracking = true;
options.TracesSampleRate = ExternalErrorConfig.SentryPerfSampleRate;
options.ProfilesSampleRate = ExternalErrorConfig.SentryProfilingSampleRate;
options.IsGlobalModeEnabled = true;
options.Environment = ExternalErrorConfig.Environment;
options.Release = Assembly.GetEntryAssembly().GetName().Version.ToString();
});
}
await builder.RunConsoleAsync();
}
private static void Prime()
{
System.Console.WriteLine("Initializing Dependencies...");
if (!String.IsNullOrWhiteSpace(Configuration["DOTNET_RUNNING_IN_CONTAINER"]))
ConfigProcessor.LoadAndProcessConfig(System.Configuration.ConfigurationManager.AppSettings["ConfigPath"]);
SetConnectionString();
Bootstrapper.Initialize();
var eventAggragator = Bootstrapper.GetKernel().Resolve<IEventAggregator>();
var workflowProvider = Bootstrapper.GetKernel().Resolve<IWorkflowEventProvider>();
var outbound = Bootstrapper.GetKernel().Resolve<IOutboundEventProvider>();
var coreEventService = Bootstrapper.GetKernel().Resolve<ICoreEventService>();
SerializerHelper.WarmUpProtobufSerializer();
System.Console.WriteLine("Finished Initializing Dependencies.");
}
private static void LoadConfiguration(string[] args)
{
System.Console.WriteLine("Loading Configuration...");
var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddCommandLine(args)
.AddEnvironmentVariables();
Configuration = builder.Build();
System.Console.WriteLine("Finished Loading Configuration.");
}
private static void SetConnectionString()
{
string configPath = Configuration["AppOptions:ConfigPath"];
if (string.IsNullOrWhiteSpace(configPath))
configPath = "C:\\Resgrid\\Config\\ResgridConfig.json";
bool configResult = ConfigProcessor.LoadAndProcessConfig(configPath);
if (configResult)
{
System.Console.WriteLine($"Loaded Config: {configPath}");
}
var settings = System.Configuration.ConfigurationManager.ConnectionStrings;
var element = typeof(ConfigurationElement).GetField("_readOnly", BindingFlags.Instance | BindingFlags.NonPublic);
var collection = typeof(ConfigurationElementCollection).GetField("_readOnly", BindingFlags.Instance | BindingFlags.NonPublic);
element.SetValue(settings, false);
collection.SetValue(settings, false);
if (!configResult)
{
if (settings["ResgridContext"] == null)
{
settings.Add(new System.Configuration.ConnectionStringSettings("ResgridContext", Configuration["ConnectionStrings:ResgridContext"]));
}
}
else
{
if (settings["ResgridContext"] == null)
{
settings.Add(new ConnectionStringSettings("ResgridContext", DataConfig.ConnectionString));
}
}
// Previously this also persisted ConnectionStrings to the on-disk .dll.config via config.Save().
// That fails on read-only / non-root hardened (DHI) containers and is unnecessary: the in-memory
// injection above is what downstream ConfigurationManager.ConnectionStrings consumers read
// (this mirrors the web apps' Startup, which only inject in-memory and never write to disk).
collection.SetValue(settings, true);
element.SetValue(settings, true);
ConfigProcessor.LoadAndProcessEnvVariables(Configuration.AsEnumerable());
}
}
public sealed class DatabaseUpgradeState
{
private readonly TaskCompletionSource<bool> _completionSource = new(TaskCreationOptions.RunContinuationsAsynchronously);
public DatabaseUpgradeState(bool upgradeRequired)
{
if (!upgradeRequired)
_completionSource.TrySetResult(true);
}
public Task WaitForCompletionAsync(CancellationToken cancellationToken)
{
return _completionSource.Task.WaitAsync(cancellationToken);
}
public void MarkCompleted()
{
_completionSource.TrySetResult(true);
}
public void MarkFailed(Exception ex)
{
_completionSource.TrySetException(ex);
}
}
public class QueuesProcessingService : BackgroundService
{
private ILogger _logger;
private readonly DatabaseUpgradeState _databaseUpgradeState;
public QueuesProcessingService(ILogger<QueuesProcessingService> logger, DatabaseUpgradeState databaseUpgradeState)
{
_logger = logger;
_databaseUpgradeState = databaseUpgradeState;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await _databaseUpgradeState.WaitForCompletionAsync(stoppingToken);
_logger.Log(LogLevel.Information, "Starting Queues Event Watcher");
Task.Run(async () =>
{
var queuesTask = new QueuesProcessorTask(_logger);
await queuesTask.ProcessAsync(new QueuesProcessorCommand(4), null, stoppingToken);
}, stoppingToken);
return;
}
}
public class ScheduledJobsService : BackgroundService
{
private ILogger _logger;
private readonly DatabaseUpgradeState _databaseUpgradeState;
private IQuidjiboClient Client { get; set; }
private QuidjiboBuilder Builder { get; set; }
public ScheduledJobsService(ILogger<ScheduledJobsService> logger, DatabaseUpgradeState databaseUpgradeState)
{
_logger = logger;
_databaseUpgradeState = databaseUpgradeState;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await _databaseUpgradeState.WaitForCompletionAsync(stoppingToken);
var aes = Aes.Create();
var key = string.Join(",", aes.Key);
//System.Console.CancelKeyPress += (s, e) => { cancellationToken..Cancel(); };
_logger.Log(LogLevel.Information, "Starting Scheduler");
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
var logger = loggerFactory.CreateLogger<Program>();
// Setup DI
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterModule(new QuidjiboModule(typeof(Program).Assembly));
containerBuilder.RegisterInstance<ILogger>(logger);
var container = containerBuilder.Build();
// Setup Quidjibo
if (WorkerConfig.DatabaseType == DatabaseTypes.Postgres)
{
Builder = new QuidjiboBuilder()
.ConfigureLogging(loggerFactory)
.UseAutofac(container)
.UseAes(Encoding.ASCII.GetBytes(WorkerConfig.PayloadKey))
.UsePostgres(WorkerConfig.WorkerDbConnectionString)
.ConfigurePipeline(pipeline => pipeline.UseDefault());
// Quidjibo Client
Client = Builder.BuildClient();
}
else
{
Builder = new QuidjiboBuilder()
.ConfigureLogging(loggerFactory)
.UseAutofac(container)
.UseAes(Encoding.ASCII.GetBytes(WorkerConfig.PayloadKey))
.UseSqlServer(WorkerConfig.WorkerDbConnectionString)
.ConfigurePipeline(pipeline => pipeline.UseDefault());
// Quidjibo Client
Client = Builder.BuildClient();
}
_logger.Log(LogLevel.Information, "Scheduler Started");
//// Long Running Jobs
////await client.PublishAsync(new SystemQueueProcessorCommand(8), cancellationToken);
////await client.PublishAsync(new QueuesProcessorCommand(4), cancellationToken);
var isEventsOnly = Environment.GetEnvironmentVariable("RESGRID__EVENTSONLY");
if (String.IsNullOrWhiteSpace(isEventsOnly) || isEventsOnly.ToLower() == "false")
{
_logger.Log(LogLevel.Information, "Starting Scheduled Jobs");
// Scheduled Jobs
_logger.Log(LogLevel.Information, "Scheduling Calendar Notifications");
await Client.ScheduleAsync("Calendar Notifications",
new CalendarNotificationCommand(1),
Cron.MinuteIntervals(20),
stoppingToken);
//System.Console.WriteLine("Scheduling Calendar Notifications");
//await client.ScheduleAsync("Call Email Import",
// new CallEmailImportCommand(2),
// Cron.MinuteIntervals(5),
// cancellationToken);
_logger.Log(LogLevel.Information, "Scheduling Call Pruning");
await Client.ScheduleAsync("Call Pruning",
new CallPruneCommand(3),
Cron.MinuteIntervals(60),
stoppingToken);
_logger.Log(LogLevel.Information, "Scheduling Report Delivery");
await Client.ScheduleAsync("Report Delivery",
new ReportDeliveryTaskCommand(5),
Cron.MinuteIntervals(14),
stoppingToken);
_logger.Log(LogLevel.Information, "Scheduling Shift Notifier");
await Client.ScheduleAsync("Shift Notifier",
new ShiftNotiferCommand(6),
Cron.MinuteIntervals(720),
stoppingToken);
_logger.Log(LogLevel.Information, "Scheduling Staffing Schedule");
await Client.ScheduleAsync("Staffing Schedule",
new Commands.StaffingScheduleCommand(7),
Cron.MinuteIntervals(5),
stoppingToken);
_logger.Log(LogLevel.Information, "Scheduling Training Notifier");
await Client.ScheduleAsync("Training Notifier",
new TrainingNotiferCommand(9),
Cron.MinuteIntervals(30),
stoppingToken);
_logger.Log(LogLevel.Information, "Scheduling Status Schedule");
await Client.ScheduleAsync("Status Schedule",
new Commands.StatusScheduleCommand(11),
Cron.MinuteIntervals(5),
stoppingToken);
_logger.Log(LogLevel.Information, "Scheduling Dispatch Scheduled Calls");
await Client.ScheduleAsync("Scheduled Calls",
new Commands.StatusScheduleCommand(12),
Cron.MinuteIntervals(5),
stoppingToken);
_logger.Log(LogLevel.Information, "Scheduling OIDC Token Cleaning");
await Client.ScheduleAsync("Clean OIDC Tokens",
new Commands.CleanOIDCCommand(13),
Cron.MinuteIntervals(30),
stoppingToken);
_logger.Log(LogLevel.Information, "Scheduling System SQL Queue");
await Client.ScheduleAsync("System SQL Queue",
new Commands.SystemSqlQueueCommand(14),
Cron.Daily(3, 0),
stoppingToken);
_logger.Log(LogLevel.Information, "Scheduling Security Refresh");
await Client.ScheduleAsync("Security Refresh",
new Commands.SecurityRefreshScheduleCommand(15),
Cron.Daily(2, 0),
stoppingToken);
_logger.Log(LogLevel.Information, "Scheduling GDPR Data Export");
await Client.ScheduleAsync("GDPR Data Export",
new Commands.GdprExportCommand(16),
Cron.MinuteIntervals(5),
stoppingToken);
_logger.Log(LogLevel.Information, "Scheduling Communication Test");
await Client.ScheduleAsync("Communication Test",
new Commands.CommunicationTestCommand(17),
Cron.MinuteIntervals(15),
stoppingToken);
if (!string.IsNullOrWhiteSpace(TtsConfig.ServiceBaseUrl) && !string.IsNullOrWhiteSpace(TtsConfig.StaticPromptAdminKey))
{
var refreshInterval = TtsConfig.StaticPromptRefreshIntervalMinutes > 0 ? TtsConfig.StaticPromptRefreshIntervalMinutes : 60;
_logger.Log(LogLevel.Information, "Scheduling TTS Static Prompt Refresh");
await Client.ScheduleAsync("TTS Static Prompt Refresh",
new Commands.TtsStaticPromptRefreshCommand(18),
Cron.MinuteIntervals(refreshInterval),
stoppingToken);
}
_logger.Log(LogLevel.Information, "Scheduling Weather Alert Import");
await Client.ScheduleAsync("Weather Alert Import",
new Commands.WeatherAlertImportCommand(20),
Cron.MinuteIntervals(5),
stoppingToken);
_logger.Log(LogLevel.Information, "Scheduling Reporting Rollup");
await Client.ScheduleAsync("Reporting Rollup",
new Commands.ReportingRollupCommand(21),
Cron.Daily(3, 30),
stoppingToken);
}
else
{
_logger.Log(LogLevel.Information, "Starting in Events Only Mode!");
}
// Quidjibo Server
using (var workServer = Builder.BuildServer())
{
// Start Quidjibo
workServer.Start();
stoppingToken.WaitHandle.WaitOne();
}
//var test = "a";
//while (!cancellationToken.IsCancellationRequested)
//{
// await Task.Delay(TimeSpan.FromSeconds(1));
//}
}
}
public class DatabaseUpgradeService : BackgroundService
{
private ILogger _logger;
private readonly DatabaseUpgradeState _databaseUpgradeState;
public DatabaseUpgradeService(ILogger<DatabaseUpgradeService> logger, DatabaseUpgradeState databaseUpgradeState)
{
_logger = logger;
_databaseUpgradeState = databaseUpgradeState;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.Log(LogLevel.Information, "Starting Database Upgrade");
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
var logger = loggerFactory.CreateLogger<Program>();
try
{
var serviceProvider = CreateServices();
// Put the database update into a scope to ensure
// that all resources will be disposed.
using (var scope = serviceProvider.CreateScope())
{
UpdateDatabase(scope.ServiceProvider);
await UpdateOidcDatabaseAsync(logger, scope.ServiceProvider);
await UpdateDocumentDatabaseAsync(logger, scope.ServiceProvider);
}
_databaseUpgradeState.MarkCompleted();
_logger.Log(LogLevel.Information, "Completed updating the Resgrid Database!");
}
catch (Exception ex)
{
_databaseUpgradeState.MarkFailed(ex);
_logger.Log(LogLevel.Error, ex, "There was an error trying to update the Resgrid Database.");
Environment.Exit(1);
}
}
/// <summary>
/// Update the database
/// </summary>
private static void UpdateDatabase(IServiceProvider serviceProvider)
{
// Instantiate the runner
var runner = serviceProvider.GetRequiredService<IMigrationRunner>();
// Execute the migrations
runner.MigrateUp();
}
/// <summary>
/// Update the database
/// </summary>
private static async Task UpdateOidcDatabaseAsync(ILogger<Program> logger, IServiceProvider serviceProvider)
{
logger.Log(LogLevel.Information, "Starting OIDC Database Upgrade");
try
{
var oidcRepository = Bootstrapper.GetKernel().Resolve<IOidcRepository>();
bool result = await oidcRepository.UpdateOidcDatabaseAsync();
if (result)
logger.Log(LogLevel.Information, "Completed updating the OIDC Database!");
else
logger.Log(LogLevel.Warning, "UpdateOidcDatabaseAsync returned false; the OIDC database may not have been fully updated.");
}
catch (Exception ex)
{
logger.Log(LogLevel.Error, ex, "There was an error trying to update the OIDC Database.");
throw;
}
}
/// <summary>
/// Update the document database
/// </summary>
private static async Task UpdateDocumentDatabaseAsync(ILogger<Program> logger, IServiceProvider serviceProvider)
{
if (Config.DataConfig.DocDatabaseType != Config.DatabaseTypes.Postgres)
return;
logger.Log(LogLevel.Information, "Starting Document Database Upgrade");
try
{
var documentDbRepository = Bootstrapper.GetKernel().Resolve<IDocumentDbRepository>();
bool result = await documentDbRepository.UpdateDocumentDatabaseAsync();
if (result)
logger.Log(LogLevel.Information, "Completed updating the Document Database!");
else
throw new InvalidOperationException("UpdateDocumentDatabaseAsync returned false; the document database was not fully updated.");
}
catch (Exception ex)
{
logger.Log(LogLevel.Error, ex, "There was an error trying to update the Document Database.");
throw;
}
}
private static IServiceProvider CreateServices()
{
if (Config.DataConfig.DatabaseType == Config.DatabaseTypes.Postgres)
{
return new ServiceCollection()
.AddOptions()
// Add common FluentMigrator services
.AddFluentMigratorCore()
.ConfigureRunner(rb => rb
// Add SQL Server support to FluentMigrator
.AddPostgres11_0()
// Set the connection string
.WithGlobalConnectionString(Config.DataConfig.CoreConnectionString)
// Define the assembly containing the migrations
.ScanIn(typeof(M0001_InitialMigrationPg).Assembly).For.Migrations().For.EmbeddedResources())
// Enable logging to console in the FluentMigrator way
.AddLogging(lb => lb.AddFluentMigratorConsole())
// Build the service provider
.BuildServiceProvider(false);
}
else
{
return new ServiceCollection()
.AddOptions()
// Add common FluentMigrator services
.AddFluentMigratorCore()
.ConfigureRunner(rb => rb
// Add SQL Server support to FluentMigrator
.AddSqlServer()
// Set the connection string
.WithGlobalConnectionString(Config.DataConfig.CoreConnectionString)
// Define the assembly containing the migrations
.ScanIn(typeof(M0001_InitialMigration).Assembly).For.Migrations().For.EmbeddedResources())
// Enable logging to console in the FluentMigrator way
.AddLogging(lb => lb.AddFluentMigratorConsole())
// Build the service provider
.BuildServiceProvider(false);
}
}
}
}