Skip to content

Commit

Permalink
[354] fix Console application failed to convert workout with error: K…
Browse files Browse the repository at this point in the history
…eyNotFoundException (#355)
  • Loading branch information
philosowaffle authored Oct 20, 2022
1 parent 3824f2b commit 58c24f4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
10 changes: 1 addition & 9 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
[![](https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86)](https://github.com/sponsors/philosowaffle) <span class="badge-buymeacoffee"><a href="https://www.buymeacoffee.com/philosowaffle" title="Donate to this project using Buy Me A Coffee"><img src="https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg" alt="Buy Me A Coffee donate button" /></a></span>
---

## Features

-

## Fixes

-

## Changes

-
- [#354] Console application failed to convert workout with error: `KeyNotFoundException`
2 changes: 1 addition & 1 deletion src/Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public static class Constants
public const string ApiName = "p2g_api";
public const string WebUIName = "p2g_webui";

public const string AppVersion = "3.2.0-rc";
public const string AppVersion = "3.1.1";
}
}
45 changes: 36 additions & 9 deletions src/Common/Service/FileBasedSettingsService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Common.Dto.Garmin;
using Common.Observe;
using Common.Stateful;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Serilog;
using System;
Expand All @@ -10,15 +11,22 @@ namespace Common.Service
{
public class FileBasedSettingsService : ISettingsService
{
private static readonly ILogger _logger = LogContext.ForClass<FileBasedSettingsService>();

private readonly IConfiguration _configurationLoader;
private static readonly ILogger _logger = LogContext.ForClass<FileBasedSettingsService>();
private static readonly object _lock = new object();

private const string GarminDeviceInfoKey = "GarminDeviceInfo";

private readonly IConfiguration _configurationLoader;
private readonly IMemoryCache _cache;
private readonly IFileHandling _fileHandler;
private readonly ISettingsService _next;

public FileBasedSettingsService(IConfiguration configurationLoader, ISettingsService next)
public FileBasedSettingsService(IConfiguration configurationLoader, ISettingsService next, IMemoryCache cache, IFileHandling fileHandler)
{
_configurationLoader = configurationLoader;
_next = next;
_next = next;
_cache = cache;
_fileHandler = fileHandler;
}

public void ClearGarminAuthentication(string garminEmail)
Expand Down Expand Up @@ -83,11 +91,30 @@ public Task<AppConfiguration> GetAppConfigurationAsync()
return _next.GetAppConfigurationAsync();
}

public Task<GarminDeviceInfo> GetCustomDeviceInfoAsync(string garminEmail)
public async Task<GarminDeviceInfo> GetCustomDeviceInfoAsync(string garminEmail)
{
using var tracing = Tracing.Trace($"{nameof(FileBasedSettingsService)}.{nameof(GetCustomDeviceInfoAsync)}");

return _next.GetCustomDeviceInfoAsync(garminEmail);
using var tracing = Tracing.Trace($"{nameof(FileBasedSettingsService)}.{nameof(GetCustomDeviceInfoAsync)}");

GarminDeviceInfo userProvidedDeviceInfo = null;

var settings = await GetSettingsAsync();
var userDevicePath = settings.Format.DeviceInfoPath;

if (string.IsNullOrEmpty(userDevicePath))
return null;

lock (_lock)
{
var key = $"{GarminDeviceInfoKey}:{garminEmail}";
return _cache.GetOrCreate(key, (cacheEntry) =>
{
cacheEntry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(5);
if (_fileHandler.TryDeserializeXml(userDevicePath, out userProvidedDeviceInfo))
return userProvidedDeviceInfo;

return null;
});
}
}
}
}
4 changes: 3 additions & 1 deletion src/PelotonToGarminConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ static IHostBuilder CreateHostBuilder(string[] args)
services.AddSingleton<ISettingsService>((serviceProvider) =>
{
var settingService = new SettingsService(serviceProvider.GetService<ISettingsDb>(), serviceProvider.GetService<IMemoryCache>(), serviceProvider.GetService<IConfiguration>(), serviceProvider.GetService<IFileHandling>());
return new FileBasedSettingsService(serviceProvider.GetService<IConfiguration>(), settingService);
var memCache = serviceProvider.GetService<IMemoryCache>();
var fileHandler = serviceProvider.GetService<IFileHandling>();
return new FileBasedSettingsService(serviceProvider.GetService<IConfiguration>(), settingService, memCache, fileHandler);
});

// PELOTON
Expand Down

0 comments on commit 58c24f4

Please sign in to comment.