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 simple logging for beatmap downloads #85

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions Difficalcy/DifficalcyStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using StackExchange.Redis;

Expand Down Expand Up @@ -31,6 +32,14 @@ public void ConfigureServices(IServiceCollection services)
c.SwaggerDoc("v1", new OpenApiInfo { Title = OpenApiTitle, Version = OpenApiVersion });
});

services.AddLogging(options =>
{
options.AddSimpleConsole(console =>
{
console.TimestampFormat = "[HH:mm:ss] ";
});
});

var redisConfig = Configuration["REDIS_CONFIGURATION"];
ICache cache;
if (redisConfig == null)
Expand Down
12 changes: 11 additions & 1 deletion Difficalcy/Services/WebBeatmapProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Difficalcy.Services
{
public class WebBeatmapProvider(IConfiguration configuration) : IBeatmapProvider
public class WebBeatmapProvider(IConfiguration configuration, ILogger<WebBeatmapProvider> logger) : IBeatmapProvider
{
private readonly string _beatmapDirectory = configuration["BEATMAP_DIRECTORY"];
private readonly string _downloadMissingBeatmaps = configuration["DOWNLOAD_MISSING_BEATMAPS"];
Expand All @@ -17,16 +18,25 @@ public async Task EnsureBeatmap(string beatmapId)
if (!File.Exists(beatmapPath))
{
if (_downloadMissingBeatmaps != "true")
{
logger.LogWarning("Beatmap {BeatmapId} not found and downloading is disabled", beatmapId);
throw new BeatmapNotFoundException(beatmapId);
}

logger.LogInformation("Downloading beatmap {BeatmapId}", beatmapId);

using var response = await _httpClient.GetAsync($"https://osu.ppy.sh/osu/{beatmapId}");
if (!response.IsSuccessStatusCode || response.Content.Headers.ContentLength == 0)
{
logger.LogWarning("Failed to download beatmap {BeatmapId}", beatmapId);
throw new BeatmapNotFoundException(beatmapId);
}

using var fs = new FileStream(beatmapPath, FileMode.CreateNew);
await response.Content.CopyToAsync(fs);
if (fs.Length == 0)
{
logger.LogWarning("Downloaded beatmap {BeatmapId} was empty, deleting", beatmapId);
fs.Close();
File.Delete(beatmapPath);
throw new BeatmapNotFoundException(beatmapId);
Expand Down
Loading