From dcb687b7058626ddbc17f13eff7c95f7fe4b1404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gutyina=20Gerg=C5=91?= Date: Mon, 15 Jan 2024 12:34:39 +0100 Subject: [PATCH] Host an HTTP server for heartbeat instead of creating a file --- .gitignore | 2 -- Environment.cs | 2 ++ README.md | 2 ++ Services/HeartbeatService.cs | 19 ++++++++++--------- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 47cdf15..3f7a499 100755 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,8 @@ # Sensitive files *.env -token.json storage.db logs backups -.heartbeat # Generated files obj diff --git a/Environment.cs b/Environment.cs index 3b50b5c..2392fb0 100755 --- a/Environment.cs +++ b/Environment.cs @@ -12,6 +12,7 @@ public class Environment public List Owners { get; set; } = default!; public TimeSpan BackupInterval { get; set; } = default!; public int BackupsToKeep { get; set; } = default!; + public int StatusPort { get; set; } = default!; public static async Task Load() { @@ -38,6 +39,7 @@ public static async Task Load() .ToList(), BackupInterval = TimeSpan.FromMinutes(int.Parse(GetEnv("BACKUP_INTERVAL_MINUTES"))), BackupsToKeep = int.Parse(GetEnv("BACKUPS_TO_KEEP")), + StatusPort = int.Parse(GetEnv("STATUS_PORT")), }; } diff --git a/README.md b/README.md index c3c7a52..5761e60 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ SERVERID= OWNERS= BACKUP_INTERVAL_MINUTES=60 BACKUPS_TO_KEEP=50 +STATUS_PORT=8000 ``` Replace the placeholders enclosed in angle brackets with their appropriate value. You can get the **Server ID** by right clicking on a server and choosing Copy ID. Same concept applies to **User ID**s. You probably want to assign your user's ID to **OWNERS**. @@ -98,6 +99,7 @@ To tell the bot which configuration should it use, run it with `--development` o - OWNERS: Comma separated list of User IDs who have full access to the bot. Overrides [modranks](#modranks). - BACKUP_INTERVAL_MINUTES: Minutes between automatic database backups. - BACKUPS_TO_KEEP: Delete old backups after the number of backups exceeds this. +- STATUS_PORT: Start a web server on this port to appear online for status services. Commands (including slash commands and context commands) are registered at guild (discord server) scope when using development mode, and global scope when using production mode. Global command registration may take a few seconds or minutes due to Discord's API, but guild scoped commands are almost instant. In order to register commands, you need to run the bot with `--register-commands` parameter. The reason why registering commands is not the default is Discord can rate limit the bot when it tries to register commands over and over again in a short period of time. This can be annoying when debugging. diff --git a/Services/HeartbeatService.cs b/Services/HeartbeatService.cs index 08e4785..0e73edc 100644 --- a/Services/HeartbeatService.cs +++ b/Services/HeartbeatService.cs @@ -1,3 +1,4 @@ +using System.Net; using Discord; namespace Moe.Services; @@ -8,6 +9,12 @@ public void Init() { Task.Run(async () => { + HttpListener listener = new HttpListener(); + var url = $"http://localhost:{ConfigService.Environment.StatusPort}/"; + listener.Prefixes.Add(url); + listener.Start(); + await LogService.LogToFileAndConsole($"HTTP server started on {url}"); + while (true) { if (DiscordService.Discord.ConnectionState != ConnectionState.Connected) @@ -15,16 +22,10 @@ public void Init() continue; } - var seconds = GetSecondsSinceEpoch().ToString(); - File.WriteAllText(".heartbeat", seconds); - await Task.Delay(1000); + HttpListenerContext context = listener.GetContext(); + // return 200 + context.Response.OutputStream.Close(); } }); } - - private int GetSecondsSinceEpoch() - { - TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1); - return (int)t.TotalSeconds; - } }