New: Return static response to requests while app is starting

Closes #5402
This commit is contained in:
Mark McDowall 2023-02-04 00:03:32 -08:00 committed by Mark McDowall
parent 81435dabc7
commit 303fc5d786
5 changed files with 48 additions and 0 deletions

View File

@ -9,6 +9,7 @@ namespace NzbDrone.Common.EnvironmentInfo
bool IsAdmin { get; } bool IsAdmin { get; }
bool IsWindowsService { get; } bool IsWindowsService { get; }
bool IsWindowsTray { get; } bool IsWindowsTray { get; }
bool IsStarting { get; set; }
bool IsExiting { get; set; } bool IsExiting { get; set; }
bool IsTray { get; } bool IsTray { get; }
RuntimeMode Mode { get; } RuntimeMode Mode { get; }

View File

@ -19,6 +19,7 @@ namespace NzbDrone.Common.EnvironmentInfo
_logger = logger; _logger = logger;
IsWindowsService = hostLifetime is WindowsServiceLifetime; IsWindowsService = hostLifetime is WindowsServiceLifetime;
IsStarting = true;
// net6.0 will return Sonarr.dll for entry assembly, we need the actual // net6.0 will return Sonarr.dll for entry assembly, we need the actual
// executable name (Sonarr on linux). On mono this will return the location of // executable name (Sonarr on linux). On mono this will return the location of
@ -82,6 +83,7 @@ namespace NzbDrone.Common.EnvironmentInfo
public bool IsWindowsService { get; private set; } public bool IsWindowsService { get; private set; }
public bool IsStarting { get; set; }
public bool IsExiting { get; set; } public bool IsExiting { get; set; }
public bool IsTray public bool IsTray
{ {

View File

@ -56,6 +56,7 @@ namespace NzbDrone.Host
private void OnAppStarted() private void OnAppStarted()
{ {
_runtimeInfo.IsStarting = false;
_runtimeInfo.IsExiting = false; _runtimeInfo.IsExiting = false;
if (!_startupContext.Flags.Contains(StartupContext.NO_BROWSER) if (!_startupContext.Flags.Contains(StartupContext.NO_BROWSER)

View File

@ -251,6 +251,7 @@ namespace NzbDrone.Host
app.UseMiddleware<VersionMiddleware>(); app.UseMiddleware<VersionMiddleware>();
app.UseMiddleware<UrlBaseMiddleware>(configFileProvider.UrlBase); app.UseMiddleware<UrlBaseMiddleware>(configFileProvider.UrlBase);
app.UseMiddleware<StartingUpMiddleware>();
app.UseMiddleware<CacheHeaderMiddleware>(); app.UseMiddleware<CacheHeaderMiddleware>();
app.UseMiddleware<IfModifiedMiddleware>(); app.UseMiddleware<IfModifiedMiddleware>();
app.UseMiddleware<BufferingMiddleware>(new List<string> { "/api/v3/command" }); app.UseMiddleware<BufferingMiddleware>(new List<string> { "/api/v3/command" });

View File

@ -0,0 +1,43 @@
using System.Buffers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Messaging.Events;
using Sonarr.Http.Extensions;
namespace Sonarr.Http.Middleware
{
public class StartingUpMiddleware
{
private readonly RequestDelegate _next;
private readonly IRuntimeInfo _runtimeInfo;
private static readonly string MESSAGE = "Sonarr is starting up, please try again later";
public StartingUpMiddleware(RequestDelegate next, IRuntimeInfo runtimeInfo)
{
_next = next;
_runtimeInfo = runtimeInfo;
}
public async Task InvokeAsync(HttpContext context)
{
if (_runtimeInfo.IsStarting)
{
var isJson = context.Request.IsApiRequest();
var message = isJson ? STJson.ToJson(new { ErrorMessage = MESSAGE }) : MESSAGE;
var bytes = Encoding.UTF8.GetBytes(message);
context.Response.StatusCode = 503;
context.Response.ContentType = isJson ? "application/json" : "text/plain";
await context.Response.Body.WriteAsync(bytes, 0, bytes.Length);
return;
}
await _next(context);
}
}
}