2023-10-24 07:51:29 +00:00
|
|
|
|
using Core;
|
|
|
|
|
using KubernetesWorkflow;
|
2023-11-12 09:07:23 +00:00
|
|
|
|
using KubernetesWorkflow.Types;
|
2024-05-21 10:58:17 +00:00
|
|
|
|
using Utils;
|
2023-10-24 07:51:29 +00:00
|
|
|
|
|
|
|
|
|
namespace CodexDiscordBotPlugin
|
|
|
|
|
{
|
|
|
|
|
public class CodexDiscordBotPlugin : IProjectPlugin, IHasLogPrefix, IHasMetadata
|
|
|
|
|
{
|
2024-05-21 10:58:17 +00:00
|
|
|
|
private const string ExpectedStartupMessage = "Debug option is set. Discord connection disabled!";
|
2023-10-24 07:51:29 +00:00
|
|
|
|
private readonly IPluginTools tools;
|
|
|
|
|
|
|
|
|
|
public CodexDiscordBotPlugin(IPluginTools tools)
|
|
|
|
|
{
|
|
|
|
|
this.tools = tools;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string LogPrefix => "(DiscordBot) ";
|
|
|
|
|
|
|
|
|
|
public void Announce()
|
|
|
|
|
{
|
|
|
|
|
tools.GetLog().Log($"Codex DiscordBot (BiblioTech) loaded.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddMetadata(IAddMetadata metadata)
|
|
|
|
|
{
|
2023-10-24 11:48:24 +00:00
|
|
|
|
metadata.Add("codexdiscordbotid", new DiscordBotContainerRecipe().Image);
|
2023-10-24 07:51:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Decommission()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-13 14:09:17 +00:00
|
|
|
|
public RunningPod Deploy(DiscordBotStartupConfig config)
|
2023-10-24 07:51:29 +00:00
|
|
|
|
{
|
2023-10-24 08:06:07 +00:00
|
|
|
|
var workflow = tools.CreateWorkflow();
|
2023-10-24 11:48:24 +00:00
|
|
|
|
return StartContainer(workflow, config);
|
2023-10-24 08:17:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-13 14:09:17 +00:00
|
|
|
|
public RunningPod DeployRewarder(RewarderBotStartupConfig config)
|
2024-01-31 16:52:02 +00:00
|
|
|
|
{
|
|
|
|
|
var workflow = tools.CreateWorkflow();
|
|
|
|
|
return StartRewarderContainer(workflow, config);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-13 14:09:17 +00:00
|
|
|
|
private RunningPod StartContainer(IStartupWorkflow workflow, DiscordBotStartupConfig config)
|
2023-10-24 08:17:57 +00:00
|
|
|
|
{
|
2023-10-24 08:06:07 +00:00
|
|
|
|
var startupConfig = new StartupConfig();
|
|
|
|
|
startupConfig.NameOverride = config.Name;
|
|
|
|
|
startupConfig.Add(config);
|
2024-05-21 10:58:17 +00:00
|
|
|
|
var pod = workflow.Start(1, new DiscordBotContainerRecipe(), startupConfig).WaitForOnline();
|
|
|
|
|
WaitForStartupMessage(workflow, pod);
|
|
|
|
|
return pod;
|
2023-10-24 08:17:57 +00:00
|
|
|
|
}
|
2024-01-31 16:52:02 +00:00
|
|
|
|
|
2024-04-13 14:09:17 +00:00
|
|
|
|
private RunningPod StartRewarderContainer(IStartupWorkflow workflow, RewarderBotStartupConfig config)
|
2024-01-31 16:52:02 +00:00
|
|
|
|
{
|
|
|
|
|
var startupConfig = new StartupConfig();
|
2024-05-29 12:05:16 +00:00
|
|
|
|
startupConfig.NameOverride = config.Name;
|
2024-01-31 16:52:02 +00:00
|
|
|
|
startupConfig.Add(config);
|
2024-04-09 08:23:07 +00:00
|
|
|
|
return workflow.Start(1, new RewarderBotContainerRecipe(), startupConfig).WaitForOnline();
|
2024-01-31 16:52:02 +00:00
|
|
|
|
}
|
2024-05-21 10:58:17 +00:00
|
|
|
|
|
|
|
|
|
private void WaitForStartupMessage(IStartupWorkflow workflow, RunningPod pod)
|
|
|
|
|
{
|
|
|
|
|
var finder = new LogLineFinder(ExpectedStartupMessage, workflow);
|
|
|
|
|
Time.WaitUntil(() =>
|
|
|
|
|
{
|
|
|
|
|
finder.FindLine(pod);
|
|
|
|
|
return finder.Found;
|
|
|
|
|
}, nameof(WaitForStartupMessage));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class LogLineFinder : LogHandler
|
|
|
|
|
{
|
|
|
|
|
private readonly string message;
|
|
|
|
|
private readonly IStartupWorkflow workflow;
|
|
|
|
|
|
|
|
|
|
public LogLineFinder(string message, IStartupWorkflow workflow)
|
|
|
|
|
{
|
|
|
|
|
this.message = message;
|
|
|
|
|
this.workflow = workflow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void FindLine(RunningPod pod)
|
|
|
|
|
{
|
|
|
|
|
Found = false;
|
|
|
|
|
foreach (var c in pod.Containers)
|
|
|
|
|
{
|
|
|
|
|
workflow.DownloadContainerLog(c, this);
|
|
|
|
|
if (Found) return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Found { get; private set; }
|
|
|
|
|
|
|
|
|
|
protected override void ProcessLine(string line)
|
|
|
|
|
{
|
|
|
|
|
if (!Found && line.Contains(message)) Found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-24 07:51:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|