Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add try/catch to GRPC configuration call #2551

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace EMBC.Responders.API.Controllers
{
Expand All @@ -27,13 +28,15 @@ namespace EMBC.Responders.API.Controllers
public class ConfigurationController : ControllerBase
{
private readonly IConfiguration configuration;
private readonly ILogger<ConfigurationController> logger;
private readonly IMessagingClient client;
private readonly IMapper mapper;
private readonly ICache cache;
private const int cacheDuration = 60 * 1; //1 minute

public ConfigurationController(IConfiguration configuration, IMessagingClient client, IMapper mapper, ICache cache)
public ConfigurationController(IConfiguration configuration, ILogger<ConfigurationController> logger, IMessagingClient client, IMapper mapper, ICache cache)
{
this.logger = logger;
this.configuration = configuration;
this.client = client;
this.mapper = mapper;
Expand Down Expand Up @@ -154,15 +157,37 @@ public async Task<ActionResult<string[]>> GetSecurityQuestions()
}

[HttpGet("outage-info")]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<OutageInformation>> GetOutageInfo()
{
var outageInfo = await cache.GetOrSet(
"outageInfo",
async () => (await client.Send(new OutageQuery { PortalType = PortalType.Responders })).OutageInfo,
TimeSpan.FromSeconds(30));
return Ok(mapper.Map<OutageInformation>(outageInfo));
try
{
var outageInfo = await cache.GetOrSet<ESS.Shared.Contracts.Metadata.OutageInformation>(
"outageInfo",
async () =>
{
try
{
var result = await client.Send(new OutageQuery { PortalType = PortalType.Responders });
return result.OutageInfo;
}
catch (Exception ex)
{
logger.LogInformation(ex, "Failed to get outage information via gRPC");
return new ESS.Shared.Contracts.Metadata.OutageInformation();
}
},
TimeSpan.FromSeconds(30)
);

return Ok(mapper.Map<OutageInformation>(outageInfo));
}
catch (Exception ex)
{
logger.LogInformation(ex, "Unexpected error fetching outage information");
return Ok(new OutageInformation());
}
}

[HttpGet("access-reasons")]
Expand Down
Loading