Skip to content

Commit

Permalink
refactor: clean up of service registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Thundernerd committed Jan 9, 2024
1 parent a7206e5 commit 08317b6
Show file tree
Hide file tree
Showing 49 changed files with 260 additions and 161 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Mangarr.Backend.Configuration;
namespace Mangarr.Backend.AniList;

public class AniListOptions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Anilist4Net;
using Anilist4Net.Enums;
using FluentResults;
using Mangarr.Backend.Caching;

namespace Mangarr.Backend.Services;
namespace Mangarr.Backend.AniList;

public class AniListService
{
Expand Down
10 changes: 10 additions & 0 deletions src/Mangarr.Backend/AniList/HostExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Mangarr.Backend.AniList;

public static class HostExtensions
{
public static void AddMangarrAniList(this WebApplicationBuilder builder)
{
builder.Services.Configure<AniListOptions>(builder.Configuration.GetSection(AniListOptions.SECTION));
builder.Services.AddSingleton<AniListService>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Newtonsoft.Json;
using StackExchange.Redis;

namespace Mangarr.Backend.Services;
namespace Mangarr.Backend.Caching;

public class CachingService
{
Expand Down
21 changes: 21 additions & 0 deletions src/Mangarr.Backend/Caching/HostExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.Extensions.Options;
using StackExchange.Redis;

namespace Mangarr.Backend.Caching;

public static class HostExtensions
{
public static void AddMangarrCaching(this WebApplicationBuilder builder)
{
builder.Services.Configure<RedisOptions>(builder.Configuration.GetSection(RedisOptions.SECTION));
builder.Services.AddSingleton<CachingService>();
builder.Services.AddSingleton<IConnectionMultiplexer>(provider =>
{
RedisOptions redisOptions = provider.GetRequiredService<IOptions<RedisOptions>>().Value;
return ConnectionMultiplexer.Connect(redisOptions.Host,
options => { options.Password = redisOptions.Password; });
});
builder.Services.AddTransient<IDatabase>(
provider => provider.GetRequiredService<IConnectionMultiplexer>().GetDatabase());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Mangarr.Backend.Configuration;
namespace Mangarr.Backend.Caching;

public class RedisOptions
{
Expand Down
1 change: 1 addition & 0 deletions src/Mangarr.Backend/Configuration/NotificationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public class NotificationOptions
public const string SECTION = "Notifications";

public string Discord { get; set; } = string.Empty;
public bool IsDiscordEnabled => !string.IsNullOrEmpty(Discord);
}
28 changes: 0 additions & 28 deletions src/Mangarr.Backend/Database/Extensions.cs

This file was deleted.

20 changes: 20 additions & 0 deletions src/Mangarr.Backend/Database/HostExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace Mangarr.Backend.Database;

public static class HostExtensions
{
public static void AddMangarrDatabase(this WebApplicationBuilder builder)
{
builder.Services.Configure<MongoOptions>(builder.Configuration.GetSection(MongoOptions.SECTION));
builder.Services.AddTransient<IMongoClient>(provider =>
{
MongoOptions mongoOptions = provider.GetRequiredService<IOptions<MongoOptions>>().Value;
return new MongoClient($"mongodb://{mongoOptions.Username}:{mongoOptions.Password}@{mongoOptions.Host}");
});

builder.Services.AddSingleton<CollectionFactory>();
builder.Services.AddSingleton(typeof(IMongoCollection<>), typeof(MongoCollection<>));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Mangarr.Backend.Configuration;
namespace Mangarr.Backend.Database;

public class MongoOptions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Anilist4Net;
using FluentResults;
using Mangarr.Backend.Services;
using Mangarr.Backend.AniList;
using Mangarr.Backend.Sources;
using Mangarr.Shared.Models;
using Mangarr.Shared.Requests;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Anilist4Net;
using FluentResults;
using Mangarr.Backend.AniList;
using Mangarr.Backend.Jobs;
using Mangarr.Backend.Services;
using Mangarr.Backend.Services.Notifications;
using Mangarr.Backend.Notifications;
using Mangarr.Shared.Requests;
using Mangarr.Shared.Responses;
using MongoDB.Driver;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Anilist4Net;
using Anilist4Net.Enums;
using FluentResults;
using Mangarr.Backend.Configuration;
using Mangarr.Backend.Services;
using Mangarr.Backend.AniList;
using Mangarr.Shared.Models;
using Mangarr.Shared.Requests;
using Mangarr.Shared.Responses;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Anilist4Net;
using FluentResults;
using Mangarr.Backend.Services;
using Mangarr.Backend.AniList;
using Mangarr.Shared.Models;
using Mangarr.Shared.Requests;
using Mangarr.Shared.Responses;
Expand Down
2 changes: 1 addition & 1 deletion src/Mangarr.Backend/Jobs/DownloadChapterJob.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using FluentResults;
using Mangarr.Backend.Data;
using Mangarr.Backend.Notifications;
using Mangarr.Backend.Services;
using Mangarr.Backend.Services.Notifications;
using MongoDB.Driver;
using Quartz;
using ISource = Mangarr.Backend.Sources.ISource;
Expand Down
56 changes: 56 additions & 0 deletions src/Mangarr.Backend/Jobs/HostExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Quartz;

namespace Mangarr.Backend.Jobs;

public static class HostExtensions
{
public static void AddMangarrJobs(this WebApplicationBuilder builder) =>
builder.Services
.AddQuartzHostedService(options =>
{
options.AwaitApplicationStarted = true;
options.WaitForJobsToComplete = true;
})
.AddQuartz(options =>
{
options.AddJob<CacheSourceSchedulerJob>(CacheSourceSchedulerJob.JobKey);
options.AddJob<CacheSourceJob>(CacheSourceJob.JobKey, configure => configure.StoreDurably())
.UseDefaultThreadPool(3);
options.AddJob<IndexMangaSchedulerJob>(IndexMangaSchedulerJob.JobKey);
options.AddJob<IndexMangaJob>(IndexMangaJob.JobKey, configure => configure.StoreDurably())
.UseDefaultThreadPool(5);
options.AddJob<DownloadChapterSchedulerJob>(DownloadChapterSchedulerJob.JobKey);
options.AddJob<DownloadChapterJob>(DownloadChapterJob.JobKey, configure => configure.StoreDurably())
.UseDefaultThreadPool(3);
options.AddTrigger(configure =>
{
configure
.WithIdentity("CacheSourceSchedulerJob")
.WithDescription("Cache enabled sources")
.ForJob(CacheSourceSchedulerJob.JobKey)
.WithCronSchedule("0 0/30 0 ? * * *");
});
options.AddTrigger(configure =>
{
configure
.WithIdentity("IndexMangaSchedulerJob")
.WithDescription("Check for new chapters")
.ForJob(IndexMangaSchedulerJob.JobKey)
.WithCronSchedule("0 0 * ? * * *");
});
options.AddTrigger(configure =>
{
configure
.WithIdentity("DownloadChapterSchedulerJob")
.WithDescription("Download requested chapters")
.ForJob(DownloadChapterSchedulerJob.JobKey)
.StartNow()
.WithCronSchedule("0 10 * ? * * *");
});
});
}
2 changes: 1 addition & 1 deletion src/Mangarr.Backend/Jobs/IndexMangaJob.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FluentResults;
using Mangarr.Backend.Notifications;
using Mangarr.Backend.Services;
using Mangarr.Backend.Services.Notifications;
using MongoDB.Driver;
using Quartz;
using ChapterList = Mangarr.Backend.Sources.Models.Chapter.ChapterList;
Expand Down
31 changes: 31 additions & 0 deletions src/Mangarr.Backend/Logging/HostExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Mangarr.Backend.Configuration;
using Microsoft.Extensions.Options;
using Serilog;
using Serilog.Events;

namespace Mangarr.Backend.Logging;

public static class HostExtensions
{
public static void AddMangarrLogging(this WebApplicationBuilder builder)
{
builder.Services.Configure<SeqOptions>(builder.Configuration.GetSection(SeqOptions.SECTION));
builder.Services.AddSerilog((provider, configuration) =>
{
SeqOptions seqOptions = provider.GetRequiredService<IOptions<SeqOptions>>().Value;
if (!string.IsNullOrEmpty(seqOptions.Host))
{
configuration
.Enrich.WithProperty("Application", "Mangarr")
.Enrich.FromLogContext()
.WriteTo.Seq(seqOptions.Host,
apiKey: seqOptions.Key,
restrictedToMinimumLevel: LogEventLevel.Information);
}
configuration
.WriteTo.Console(LogEventLevel.Debug);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Mangarr.Backend.Configuration;
namespace Mangarr.Backend.Logging;

public class SeqOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
using Discord;
using Discord.Webhook;
using FluentResults;
using Mangarr.Backend.AniList;
using MongoDB.Driver;

namespace Mangarr.Backend.Services.Notifications;
namespace Mangarr.Backend.Notifications;

public class DiscordNotificationProcessor : INotificationProcessor
{
private readonly AniListService _aniListService;
private readonly DiscordWebhookClient _client;
private readonly DiscordWebhookClient? _client;
private readonly IMongoCollection<SourceDocument> _sourceCollection;

public DiscordNotificationProcessor(
DiscordWebhookClient client,
DiscordWebhookClient? client,
AniListService aniListService,
IMongoCollection<SourceDocument> sourceCollection
)
Expand All @@ -26,6 +27,11 @@ IMongoCollection<SourceDocument> sourceCollection

public async Task NotifyNewManga(RequestedMangaDocument requestedMangaDocument)
{
if (_client == null)
{
return;
}

Result<Media?> result = await _aniListService.GetMedia(requestedMangaDocument.SearchId);
if (result.IsFailed)
{
Expand Down Expand Up @@ -71,6 +77,11 @@ public Task NotifyNewChapters(
IEnumerable<RequestedChapterDocument> newChapters
)
{
if (_client == null)
{
return Task.CompletedTask;
}

Embed build = new EmbedBuilder()
.WithTitle("New chapters have been added")
.WithDescription($@"**Title**
Expand All @@ -91,6 +102,11 @@ public Task NotifyChapterDownloadFailed(
RequestedChapterDocument requestedChapterDocument
)
{
if (_client == null)
{
return Task.CompletedTask;
}

Embed build = new EmbedBuilder()
.WithTitle("Failed to download a chapter")
.WithDescription($@"**Title**
Expand All @@ -110,6 +126,11 @@ public Task NotifyChapterDownloadSucceeded(
RequestedChapterDocument requestedChapterDocument
)
{
if (_client == null)
{
return Task.CompletedTask;
}

Embed build = new EmbedBuilder()
.WithTitle("Successfully downloaded a chapter")
.WithDescription($@"**Title**
Expand Down
20 changes: 20 additions & 0 deletions src/Mangarr.Backend/Notifications/HostExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Discord.Webhook;
using Mangarr.Backend.Configuration;
using Microsoft.Extensions.Options;

namespace Mangarr.Backend.Notifications;

public static class HostExtensions
{
public static void AddMangarrNotifications(this WebApplicationBuilder builder)
{
builder.Services.Configure<NotificationOptions>(builder.Configuration.GetSection(NotificationOptions.SECTION));
builder.Services.AddSingleton<NotificationService>();
builder.Services.AddSingleton<INotificationProcessor, DiscordNotificationProcessor>();
builder.Services.AddSingleton<DiscordWebhookClient>(provider =>
{
IOptions<NotificationOptions> options = provider.GetRequiredService<IOptions<NotificationOptions>>();
return options.Value.IsDiscordEnabled ? new DiscordWebhookClient(options.Value.Discord) : null!;
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Mangarr.Backend.Services.Notifications;
namespace Mangarr.Backend.Notifications;

public interface INotificationProcessor
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using RequestedChapterDocument = Mangarr.Backend.Database.Documents.RequestedChapterDocument;
using Mangarr.Backend.AniList;
using RequestedChapterDocument = Mangarr.Backend.Database.Documents.RequestedChapterDocument;
using RequestedMangaDocument = Mangarr.Backend.Database.Documents.RequestedMangaDocument;

namespace Mangarr.Backend.Services.Notifications;
namespace Mangarr.Backend.Notifications;

public class NotificationService
{
Expand Down
Loading

0 comments on commit 08317b6

Please sign in to comment.