Skip to content

Commit

Permalink
Add more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
arumie committed Mar 5, 2024
1 parent 66ef62b commit 1fe5aba
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions AiTestimonials/Api/AiTestimonialsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,21 @@ public static void RegisterAiTestimonialsEndpoints(this IEndpointRouteBuilder ro

private static async Task<Ok<Identity>> PostGenerateAiTestimonialAsync(TestimonialInput input, [FromHeader(Name = "OPENAI_KEY")] string? openAIKey, AiTestimonialsService service, VercelPostgresRepository repo, ILoggerFactory loggerFactory)
{
var logger = loggerFactory.CreateLogger("AiTestimonialsApi");
service.SetupOpenAIService(openAIKey);
var testimonialId = (await repo.CreateNewTestimonialAsync(input)).ToString();
try
{
GenerateAiTestimonialAsync(testimonialId, input.Name, input.Skills, service, repo).Forget();
return TypedResults.Ok(new Identity() { Id = testimonialId.ToString() });
}
catch (Exception e)
{
var logger = loggerFactory.CreateLogger("AiTestimonialsApi");
logger.LogError(e, "Unexpected error happened when trying to generate testimonial");

await repo.UpdateTestimonialAsync(TestimonialStatus.FAILED, testimonialId);
throw;
}
GenerateAiTestimonialAsync(testimonialId, input.Name, input.Skills, service, repo, logger).Forget();
return TypedResults.Ok(new Identity() { Id = testimonialId.ToString() });
}

private static async Task<IResult> PostRedoTestimonialsAsync(string id, AiTestimonialsService service, VercelPostgresRepository repo)
private static async Task<IResult> PostRedoTestimonialsAsync(string id, AiTestimonialsService service, VercelPostgresRepository repo, ILoggerFactory loggerFactory)
{
var logger = loggerFactory.CreateLogger("AiTestimonialsApi");
var entity = await repo.GetTestimonialsEntityAsync(id);
if (entity != null && entity.Status != TestimonialStatus.SAVED && entity.Input != null)
{
await repo.UpdateTestimonialAsync(TestimonialStatus.PENDING, id);
GenerateAiTestimonialAsync(id, entity.Input.Name, entity.Input.Skills, service, repo).Forget();
GenerateAiTestimonialAsync(id, entity.Input.Name, entity.Input.Skills, service, repo, logger).Forget();
}
else
{
Expand All @@ -64,7 +55,7 @@ private static async Task<Ok<List<TestimonialResult>>> GetTestimonialsAsync(stri
return TypedResults.Ok(res);
}

private static async Task GenerateAiTestimonialAsync(string id, string name, string skills, AiTestimonialsService service, VercelPostgresRepository repo)
private static async Task GenerateAiTestimonialAsync(string id, string name, string skills, AiTestimonialsService service, VercelPostgresRepository repo, ILogger logger)
{
try
{
Expand All @@ -73,10 +64,10 @@ private static async Task GenerateAiTestimonialAsync(string id, string name, str
await repo.AddTestimonialAsync(res, id);
await repo.UpdateTestimonialAsync(TestimonialStatus.SUCCESSFUL, id);
}
catch
catch (Exception e)
{
logger.LogError(e, "Unexpected error happened when trying to generate testimonial");
await repo.UpdateTestimonialAsync(TestimonialStatus.FAILED, id);

}
}
}

0 comments on commit 1fe5aba

Please sign in to comment.