using APCViewer.Models.Stateless.Methods; using IFX.Shared; using Microsoft.AspNetCore.Mvc; using Serilog.Context; using System.Text.Json; namespace APCViewer.Controllers; public class BackgroundController : Controller, Models.Properties.IBackgroundController, IBackgroundController { protected readonly List _Exceptions; protected readonly string _AppSettingsURLs; protected readonly string _IsEnvironmentProfile; protected readonly string _IsPrimaryInstance; protected readonly string _Message; protected readonly string _WorkingDirectory; public List Exceptions => _Exceptions; public string AppSettingsURLs => _AppSettingsURLs; public string IsEnvironmentProfile => _IsEnvironmentProfile; public string IsPrimaryInstance => _IsPrimaryInstance; public string Message => _Message; public string WorkingDirectory => _WorkingDirectory; private readonly Serilog.ILogger _Log; private readonly Models.Methods.IBackground _BackgroundMethods; public BackgroundController(IsEnvironment isEnvironment, Models.AppSettings appSettings, Singleton.Background background) { _Message = background.Message; _BackgroundMethods = background; _AppSettingsURLs = appSettings.URLs; _Exceptions = background.Exceptions; _IsEnvironmentProfile = isEnvironment.Profile; _WorkingDirectory = background.WorkingDirectory; _Log = Serilog.Log.ForContext(); Models.Methods.IBackground backgroundMethods = background; _IsPrimaryInstance = backgroundMethods.IsPrimaryInstance().ToString(); } public override string ToString() { string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); return result; } public ActionResult Index(bool? message_clear = null, bool? exceptions_clear = null, bool? set_is_primary_instance = null) { string? methodName = IMethodName.GetActualAsyncMethodName(); using (LogContext.PushProperty("MethodName", methodName)) { _Log.Debug("() => ..."); if (message_clear.HasValue && message_clear.Value) _BackgroundMethods.ClearMessage(); if (exceptions_clear.HasValue && exceptions_clear.Value) _Exceptions.Clear(); if (set_is_primary_instance.HasValue) { if (set_is_primary_instance.Value) _BackgroundMethods.SetIsPrimaryInstance(); else _BackgroundMethods.ClearIsPrimaryInstance(); } string message; if (string.IsNullOrWhiteSpace(_Message)) message = "N/A"; else message = _Message; List exceptions = new(); foreach (Exception exception in _Exceptions) exceptions.Add(exception); ViewBag.Message = message; ViewBag.Exceptions = exceptions; ViewBag.URLs = _AppSettingsURLs; ViewBag.Profile = _IsEnvironmentProfile; ViewBag.WorkingDirectory = _WorkingDirectory; ViewBag.IsPrimaryInstance = _IsPrimaryInstance; ViewBag.ExceptionsCount = string.Concat("Exception(s) - ", exceptions.Count); return View(); } } }