Skip to content

Commit

Permalink
NetworkSyncService リトライを実装
Browse files Browse the repository at this point in the history
とりあえず10回。将来的に「再施行しますか?」は付けても良いかも
TetsuOtter committed Aug 17, 2024
1 parent 1070d25 commit 0d5dd8a
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions TRViS/Services/LocationService.Network.cs
Original file line number Diff line number Diff line change
@@ -4,8 +4,10 @@ namespace TRViS.Services;

public partial class LocationService
{
const int EXCEPTION_MAX = 10;
async Task NetworkSyncServiceTask(NetworkSyncService networkService, CancellationToken token)
{
int exceptionCounter = 0;
while (!token.IsCancellationRequested)
{
logger.Trace("NetworkSyncServiceTask Loop");
@@ -23,25 +25,44 @@ await Task.WhenAll(
// Network経由の場合は負荷が少ないため、頻度が高くても問題ないはず
Task.Delay(Interval / 5, token)
);
exceptionCounter = 0;
}
catch (TaskCanceledException)
catch (TaskCanceledException exTask)
{
logger.Debug("Task is canceled -> break");
break;
if (token.IsCancellationRequested)
{
logger.Debug("Cancellation is requested -> break");
break;
}

logger.Debug("Task is canceled -> treat as exception");
if (exceptionCounter++ <= EXCEPTION_MAX)
{
logger.Warn("Exception Counter: {0}", exceptionCounter);
await Task.Delay(Interval, token);
continue;
}

logger.Warn("Switching to GpsPositioningTask");
SetLonLatLocationService();
ExceptionThrown?.Invoke(this, exTask);
return;
}
catch (Exception ex)
{
logger.Error(ex, "NetworkSyncServiceTask Loop Failed");
IsEnabled = false;
serviceCancellation?.Cancel();

object? o = _CurrentService;
if (ReferenceEquals(o, networkService))
SetLonLatLocationService();
if (ExceptionThrown is null)
throw;
else
ExceptionThrown.Invoke(this, ex);
if (exceptionCounter++ <= EXCEPTION_MAX)
{
logger.Warn("Exception Counter: {0}", exceptionCounter);
await Task.Delay(Interval, token);
continue;
}
logger.Warn("Switching to GpsPositioningTask");
SetLonLatLocationService();
ExceptionThrown?.Invoke(this, ex);
return;
}
}

0 comments on commit 0d5dd8a

Please sign in to comment.