Skip to content

Commit

Permalink
refactor: refactor error handling and async in durable function
Browse files Browse the repository at this point in the history
- Changed the `UpdateChannel_Durable` function to be async in `Channel.cs` and removed the usage of `Task.Run`
- Consolidated the error handling in the `Fc2Service` class to log and throw on general Exception instead of handling `HttpRequestException` separately.
- In `TwitcastingService.cs`, `TwitchService.cs` and `YoutubeService.cs`, changed logging from "Warning" to "Error" when failing to get channel info and added throwing a `HttpRequestException`.
- In `Fc2Service.cs`, changed the error logging message format to a more general one.

Signed-off-by: 陳鈞 <[email protected]>
  • Loading branch information
jim60105 committed Jun 29, 2024
1 parent 1f1ead9 commit 4d2ed10
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 28 deletions.
25 changes: 11 additions & 14 deletions Functions/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,28 +185,25 @@ await starter.ScheduleNewOrchestrationInstanceAsync(
}

[Function(nameof(UpdateChannel_Durable))]
public bool UpdateChannel_Durable(
public async Task<bool> UpdateChannel_Durable(
[OrchestrationTrigger] TaskOrchestrationContext context)
{
UpdateChannelRequest? data = context.GetInput<UpdateChannelRequest>();
if (null == data) throw new InvalidOperationException("Invalid request body!!");
_ = Task.Run(async () =>
{
logger.Information("Start updating channel {channelId}", data.id);
LivestreamRecorder.DB.Models.Channel? channel =
logger.Information("Start updating channel {channelId}", data.id);
LivestreamRecorder.DB.Models.Channel? channel =
await channelService.GetByChannelIdAndSourceAsync(data.id, data.Source);

if (null == channel)
{
logger.Warning("Channel {channelId} not found when updating", data.id);
throw new EntityNotFoundException(data.id);
}
if (null == channel)
{
logger.Warning("Channel {channelId} not found when updating", data.id);
throw new EntityNotFoundException(data.id);
}

await channelService.UpdateChannelDataAsync(channel);
await channelService.UpdateChannelDataAsync(channel);

await channelService.EditMonitoringAsync(data.id, data.Source, true);
logger.Information("Finish updating channel {channelId}", data.id);
});
await channelService.EditMonitoringAsync(data.id, data.Source, true);
logger.Information("Finish updating channel {channelId}", data.id);

return true;
}
Expand Down
11 changes: 3 additions & 8 deletions Services/PlatformService/FC2Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,10 @@ public class Fc2Service(IHttpClientFactory httpClientFactory,

return (avatarUrl, bannerUrl, channelName);
}
catch (HttpRequestException e)
catch (Exception)
{
logger.Error(e, "Get fc2 info failed with {StatusCode}. {channelId} Be careful if this happens repeatedly.", e.StatusCode, channelId);
return (null, null, null);
}
catch (Exception e)
{
logger.Error(e, "Get fc2 info failed. {channelId} Be careful if this happens repeatedly.", channelId);
return (null, null, null);
logger.Error("Failed to get channel info for {channelId}", channelId);
throw;
}
}
}
5 changes: 3 additions & 2 deletions Services/PlatformService/TwitcastingService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using HtmlAgilityPack;
Expand All @@ -20,8 +21,8 @@ public class TwitcastingService(ILogger logger) : IPlatformService

if (null == htmlDoc)
{
logger.Warning("Failed to get channel page for {channelId}", channelId);
return (null, null, null);
logger.Error("Failed to get channel info for {channelId}", channelId);
throw new HttpRequestException($"Failed to get channel info for {channelId}");
}

string avatarUrl = GetAvatarBlobUrl(htmlDoc);
Expand Down
5 changes: 3 additions & 2 deletions Services/PlatformService/TwitchService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Configuration;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using LivestreamRecorderBackend.Helper;
Expand All @@ -21,8 +22,8 @@ public class TwitchService(ILogger logger,

if (null == usersResponse || usersResponse.Users.Length == 0)
{
logger.Warning("Failed to get channel info for {channelId}", channelId);
return (null, null, null);
logger.Error("Failed to get channel info for {channelId}", channelId);
throw new HttpRequestException($"Failed to get channel info for {channelId}");
}

User user = usersResponse.Users.First();
Expand Down
4 changes: 2 additions & 2 deletions Services/PlatformService/YoutubeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class YoutubeService(ILogger logger) : IPlatformService

if (null == info)
{
logger.Warning("Failed to get channel info for {channelId}", channelId);
return (null, null, null);
logger.Error("Failed to get channel info for {channelId}", channelId);
throw new HttpRequestException($"Failed to get channel info for {channelId}");
}

string? channelName = info.Uploader;
Expand Down

0 comments on commit 4d2ed10

Please sign in to comment.