-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
167 lines (147 loc) · 10 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
using Microsoft.Extensions.Configuration;
using NumberSearch.DataAccess;
using NumberSearch.DataAccess.Models;
using Serilog;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace NumberSearch.Ingest
{
public class Program
{
public static async Task Main()
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddUserSecrets("328593cf-cbb9-48e9-8938-e38a44c8291d")
.Build();
var appConfig = new IngestConfiguration
{
Postgresql = string.IsNullOrWhiteSpace(config.GetConnectionString("PostgresqlProd")) ? throw new Exception("PostgresqlProd config key is blank.") : config.GetConnectionString("PostgresqlProd") ?? string.Empty,
BulkVSAPIKEY = string.IsNullOrWhiteSpace(config.GetConnectionString("BulkVSAPIKEY")) ? throw new Exception("BulkVSAPIKEY config key is blank.") : config.GetConnectionString("BulkVSAPIKEY") ?? string.Empty,
BulkVSAPISecret = string.IsNullOrWhiteSpace(config.GetConnectionString("BulkVSAPISecret")) ? throw new Exception("BulkVSAPISecret config key is blank.") : config.GetConnectionString("BulkVSAPISecret") ?? string.Empty,
BulkVSUsername = string.IsNullOrWhiteSpace(config.GetConnectionString("BulkVSUsername")) ? throw new Exception("BulkVSUsername config key is blank.") : config.GetConnectionString("BulkVSUsername") ?? string.Empty,
BulkVSPassword = string.IsNullOrWhiteSpace(config.GetConnectionString("BulkVSPassword")) ? throw new Exception("BulkVSPassword config key is blank.") : config.GetConnectionString("BulkVSPassword") ?? string.Empty,
PComNetUsername = string.IsNullOrWhiteSpace(config.GetConnectionString("PComNetUsername")) ? throw new Exception("PComNetUsername config key is blank.") : config.GetConnectionString("PComNetUsername") ?? string.Empty,
PComNetPassword = string.IsNullOrWhiteSpace(config.GetConnectionString("PComNetPassword")) ? throw new Exception("PComNetPassword config key is blank.") : config.GetConnectionString("PComNetPassword") ?? string.Empty,
SmtpUsername = string.IsNullOrWhiteSpace(config.GetConnectionString("SmtpUsername")) ? throw new Exception("SmtpUsername config key is blank.") : config.GetConnectionString("SmtpUsername") ?? string.Empty,
SmtpPassword = string.IsNullOrWhiteSpace(config.GetConnectionString("SmtpPassword")) ? throw new Exception("SmtpPassword config key is blank.") : config.GetConnectionString("SmtpPassword") ?? string.Empty,
EmailOrders = string.IsNullOrWhiteSpace(config.GetConnectionString("EmailOrders")) ? throw new Exception("EmailOrders config key is blank.") : config.GetConnectionString("EmailOrders") ?? string.Empty,
EmailDan = string.IsNullOrWhiteSpace(config.GetConnectionString("EmailDan")) ? throw new Exception("EmailDan config key is blank.") : config.GetConnectionString("EmailDan") ?? string.Empty,
EmailTom = string.IsNullOrWhiteSpace(config.GetConnectionString("EmailTom")) ? throw new Exception("EmailTom config key is blank.") : config.GetConnectionString("EmailTom") ?? string.Empty,
InvoiceNinjaToken = string.IsNullOrWhiteSpace(config.GetConnectionString("EmailTom")) ? throw new Exception("InvoiceNinjaToken config key is blank.") : config.GetConnectionString("InvoiceNinjaToken") ?? string.Empty,
};
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.Async(x => x.File(
$"{DateTime.Now:yyyyMMdd}_NumberSearch.Ingest.txt",
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true,
buffered: true
))
.CreateLogger();
Log.Information($"[Heartbeat] Ingest scheduling loop is starting. {Environment.ProcessorCount} threads detected.");
Stopwatch priorityTimer = new();
Stopwatch dailyTimer = new();
TimeSpan dailyCycle = TimeSpan.FromDays(1);
TimeSpan priorityCycle = TimeSpan.FromMinutes(30);
try
{
if (!dailyTimer.IsRunning)
{
dailyTimer.Start();
}
if (!priorityTimer.IsRunning)
{
priorityTimer.Start();
}
// To infinity and beyond.
while (true)
{
var start = DateTime.Now;
// Priority Ingest
if (priorityTimer.Elapsed >= priorityCycle)
{
priorityTimer.Restart();
var bulkVS = await Provider.BulkVSPriorityAsync(appConfig);
var firstPointCom = await Provider.FirstPointComPriorityAsync(appConfig);
// Verify that all the Executive numbers are still purchasable for the priority area codes.
await Provider.VerifyAddToCartAsync(AreaCode.Priority, "Executive", appConfig.Postgresql, appConfig.BulkVSUsername, appConfig.BulkVSPassword,
appConfig.PComNetUsername, appConfig.PComNetPassword);
await Owned.MatchOwnedNumbersToFusionPBXAsync(appConfig.Postgresql, appConfig.FusionPBXUsername, appConfig.FusionPBXPassword);
await Orders.CheckForQuoteConversionsAsync(appConfig.Postgresql, appConfig.InvoiceNinjaToken, appConfig.SmtpUsername, appConfig.SmtpPassword);
await Orders.CheckForInvoicePaymentAsync(appConfig.Postgresql, appConfig.InvoiceNinjaToken, appConfig.SmtpUsername, appConfig.SmtpPassword);
}
// Daily Ingest
if (dailyTimer.Elapsed >= dailyCycle || DateTime.Now == DateTime.Today.AddDays(1).AddSeconds(-1))
{
dailyTimer.Restart();
var bulkVS = await Provider.BulkVSDailyAsync(appConfig);
var firstPointCom = await Provider.FirstPointComDailyAsync(appConfig);
await Owned.OwnedDailyAsync(appConfig);
var email = await Orders.EmailDailyAsync(appConfig);
}
Log.Information("[Heartbeat] Cycle complete. Daily Timer {Elapsed:000} ms.", dailyTimer.ElapsedMilliseconds);
// Limit this to 1 request every 10 seconds to the database.
await Task.Delay(10000).ConfigureAwait(false);
}
}
catch (Exception ex)
{
Log.Fatal(ex.Message);
Log.Fatal(ex.StackTrace ?? "No stack trace found.");
Log.Fatal(ex.InnerException?.Message ?? "No inner exception message found.");
Log.Fatal(ex.InnerException?.StackTrace ?? "No inner exception stack trace found.");
}
finally
{
// Hopefully we never get here.
Log.Fatal("[Heartbeat] This is a complete application failure. We've broken out of the infinite loop.");
// Notify someone that there's been a failure.
var notificationEmail = new Email
{
PrimaryEmailAddress = appConfig.EmailTom,
CarbonCopy = appConfig.EmailDan,
DateSent = DateTime.Now,
Subject = $"[Ingest] App is down.",
MessageBody = $"Something has gone wrong and the ingest app is down at {DateTime.Now}. Please capture the logs and then restart or redeploy the ingest application to restore service.",
OrderId = new Guid(),
Completed = true
};
var checkSend = await notificationEmail.SendEmailAsync(appConfig.SmtpUsername, appConfig.SmtpPassword).ConfigureAwait(false);
var checkSave = await notificationEmail.PostAsync(appConfig.Postgresql).ConfigureAwait(false);
// Save the log.
await Log.CloseAndFlushAsync();
}
}
public class IngestConfiguration
{
public string CallFlow { get; set; } = string.Empty;
public string ChannelGroup { get; set; } = string.Empty;
public string PComNetUsername { get; set; } = string.Empty;
public string PComNetPassword { get; set; } = string.Empty;
public string BulkVSAPIKEY { get; set; } = string.Empty;
public string BulkVSAPISecret { get; set; } = string.Empty;
public string BulkVSUsername { get; set; } = string.Empty;
public string BulkVSPassword { get; set; } = string.Empty;
public string Postgresql { get; set; } = string.Empty;
public string PostgresqlProd { get; set; } = string.Empty;
public string SmtpUsername { get; set; } = string.Empty;
public string SmtpPassword { get; set; } = string.Empty;
public string MicrosoftClientId { get; set; } = string.Empty;
public string MicrosoftClientSecret { get; set; } = string.Empty;
public string InvoiceNinjaToken { get; set; } = string.Empty;
public string Data247Username { get; set; } = string.Empty;
public string Data247Password { get; set; } = string.Empty;
public string EmailOrders { get; set; } = string.Empty;
public string EmailDan { get; set; } = string.Empty;
public string EmailTom { get; set; } = string.Empty;
public string AzureStorageAccount { get; set; } = string.Empty;
public string TeleDynamicsUsername { get; set; } = string.Empty;
public string TeleDynamicsPassword { get; set; } = string.Empty;
public string CallWithUsAPIKEY { get; set; } = string.Empty;
public string FusionPBXUsername { get; set; } = string.Empty;
public string FusionPBXPassword { get; set; } = string.Empty;
}
}
}