Skip to content

Commit

Permalink
Add healthchecks support
Browse files Browse the repository at this point in the history
  • Loading branch information
smallketchup82 committed Aug 17, 2024
1 parent 8ee55cd commit cc25b75
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
21 changes: 21 additions & 0 deletions ketchupbot-framework/API/HealthChecks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Serilog;

namespace ketchupbot_framework.API;

public static class HealthChecks
{
private static readonly HttpClient Client = new();

public static async Task Ping(string url)
{
try
{
HttpResponseMessage response = await Client.GetAsync(url);
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException e)
{
Log.Error(e, "Failed to ping {Url}", url);
}
}
}
4 changes: 4 additions & 0 deletions ketchupbot-updater/Jobs/MassUpdateJob.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ketchupbot_framework;
using ketchupbot_framework.API;
using Quartz;

namespace ketchupbot_updater.Jobs;
Expand All @@ -10,13 +11,16 @@ public async Task Execute(IJobExecutionContext context)
JobDataMap jobDataMap = context.MergedJobDataMap;

var shipUpdater = (ShipUpdater)jobDataMap["shipUpdater"];
string? healthChecksUrl = jobDataMap.GetString("healthChecksUrl");
if (shipUpdater == null) throw new InvalidOperationException("ShipUpdater not found in job data map");

// I was thinking that maybe we should wrap this in a try/catch block. And we probably should, because I don't
// think we'd be able to catch any errors further up in the call stack. Not sure though, so I'll leave it for
// now until I make a decision.
await shipUpdater.UpdateAllShips();

if (healthChecksUrl != null) await HealthChecks.Ping(healthChecksUrl);

Console.WriteLine("Mass update job completed");
if (context.NextFireTimeUtc != null)
Console.WriteLine("Next update is scheduled for " + context.NextFireTimeUtc.Value.LocalDateTime);
Expand Down
6 changes: 3 additions & 3 deletions ketchupbot-updater/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ private static async Task<int> Main(string[] args)
configuration["MWPASSWORD"] ?? throw new InvalidOperationException("MWPASSWORD not set"));
Log.Information("Logged into the Galaxypedia");
var apiManager = new ApiManager(configuration["GIAPI_URL"] ?? throw new InvalidOperationException("GIAPI_URL not set"));
var shipUpdater = new ShipUpdater(mwClient, apiManager);

#region Scheduling Logic

Expand All @@ -159,7 +160,8 @@ private static async Task<int> Main(string[] args)
.WithIdentity("massUpdateJob", "group1")
.Build();

massUpdateJob.JobDataMap.Put("shipUpdater", new ShipUpdater(mwClient, apiManager));
massUpdateJob.JobDataMap.Put("shipUpdater", shipUpdater);
massUpdateJob.JobDataMap.Put("healthCheckUrl", configuration["HEALTHCHECK_URL"]);

ITrigger massUpdateTrigger = TriggerBuilder.Create()
.WithIdentity("massUpdateTrigger", "group1")
Expand Down Expand Up @@ -204,8 +206,6 @@ private static async Task<int> Main(string[] args)
string[]? shipsOptionValue = handler.ParseResult.GetValueForOption(shipsOption);
if (shipsOptionValue != null && shipsOptionValue.First() != "none" && shipScheduleOptionValue == null)
{
var shipUpdater = new ShipUpdater(mwClient, apiManager);

if (shipsOptionValue.First() == "all")
await shipUpdater.UpdateAllShips();
else
Expand Down
6 changes: 6 additions & 0 deletions ketchupbot-updater/appsettings.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"GIAPI_URL": "",
"MWUSERNAME": "",
"MWPASSWORD": "",
"HEALTHCHECK_URL": ""
}
5 changes: 4 additions & 1 deletion ketchupbot-updater/ketchupbot-updater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ This project is a standalone application that builds upon ketchupbot-framework t

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="appsettings.example.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

Expand Down

0 comments on commit cc25b75

Please sign in to comment.