Skip to content

Commit

Permalink
add better exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
loukylor committed Dec 21, 2024
1 parent 11cadbb commit 44b2537
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
4 changes: 3 additions & 1 deletion TrickFireDiscordBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private static async Task Main(string[] args)
await Task.Delay(-1);
return;
}

ILogger logger = host.Services.GetRequiredService<ILogger<Program>>();

try
Expand Down Expand Up @@ -73,6 +73,8 @@ private static IHost CreateHost(string baseDir, string[] secrets)
builder.Configuration["BOT_TOKEN"] = secrets[0];
builder.Configuration["NOTION_SECRET"] = secrets[1];

builder.Services.ConfigureTypeSection<HostOptions>(builder.Configuration);

// Get all types
Assembly asm = Assembly.GetExecutingAssembly();
foreach (Type type in asm.GetTypes())
Expand Down
9 changes: 8 additions & 1 deletion TrickFireDiscordBot/Services/RoleSyncer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await SyncRoles(await _pageQueue.Reader.ReadAsync(stoppingToken));
try
{
await SyncRoles(await _pageQueue.Reader.ReadAsync(stoppingToken));
}
catch (Exception ex)
{
Logger.LogError(ex, "Error in RoleSyncer main loop: ");
}
}
}

Expand Down
29 changes: 18 additions & 11 deletions TrickFireDiscordBot/Services/WebhookListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,24 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
// Wait for request
HttpListenerContext ctx;
ctx = await _listener.GetContextAsync().WaitAsync(stoppingToken);

// Read request body
OnWebhookReceived?.Invoke(ctx.Request);

// Return ok response on request
ctx.Response.StatusCode = (int)HttpStatusCode.OK;
ctx.Response.OutputStream.Write(Encoding.UTF8.GetBytes("Ok"));
ctx.Response.OutputStream.Close();
try
{
// Wait for request
HttpListenerContext ctx;
ctx = await _listener.GetContextAsync().WaitAsync(stoppingToken);

// Read request body
OnWebhookReceived?.Invoke(ctx.Request);

// Return ok response on request
ctx.Response.StatusCode = (int)HttpStatusCode.OK;
ctx.Response.OutputStream.Write(Encoding.UTF8.GetBytes("Ok"));
ctx.Response.OutputStream.Close();
}
catch (Exception ex)
{
Logger.LogError(ex, "Exception in WebhookListener main loop: ");
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions TrickFireDiscordBot/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
},
"BotStateOptions": {
"FileLocation": "state.json"
},
"HostOptions": {
"BackgroundServiceExceptionBehavior": "Ignore"
}
}

0 comments on commit 44b2537

Please sign in to comment.