2022-02-18 00:08:42 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2022-02-25 21:07:22 +00:00
|
|
|
|
using Quartz;
|
2022-02-18 00:08:42 +00:00
|
|
|
|
using Selector.Cache.Extensions;
|
2022-02-25 21:07:22 +00:00
|
|
|
|
using Selector.CLI.Jobs;
|
2022-02-18 00:08:42 +00:00
|
|
|
|
using Selector.Extensions;
|
|
|
|
|
using Selector.Model;
|
|
|
|
|
using Selector.Model.Services;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Selector.CLI.Extensions
|
|
|
|
|
{
|
|
|
|
|
public static class ServiceExtensions
|
|
|
|
|
{
|
|
|
|
|
public static IServiceCollection ConfigureLastFm(this IServiceCollection services, RootOptions config)
|
|
|
|
|
{
|
|
|
|
|
if (config.LastfmClient is not null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Adding Last.fm credentials...");
|
|
|
|
|
|
|
|
|
|
services.AddLastFm(config.LastfmClient, config.LastfmSecret);
|
|
|
|
|
|
|
|
|
|
if (config.RedisOptions.Enabled)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Adding caching Last.fm consumers...");
|
|
|
|
|
services.AddCachingLastFm();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> No Last.fm credentials, skipping init...");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-25 21:07:22 +00:00
|
|
|
|
public static IServiceCollection ConfigureJobs(this IServiceCollection services, RootOptions config)
|
|
|
|
|
{
|
|
|
|
|
if (config.JobOptions.Enabled)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Adding Jobs...");
|
|
|
|
|
|
|
|
|
|
services.AddQuartz(options => {
|
|
|
|
|
|
|
|
|
|
options.UseMicrosoftDependencyInjectionJobFactory();
|
|
|
|
|
|
|
|
|
|
options.UseSimpleTypeLoader();
|
|
|
|
|
options.UseInMemoryStore();
|
|
|
|
|
options.UseDefaultThreadPool(tp =>
|
|
|
|
|
{
|
|
|
|
|
tp.MaxConcurrency = 5;
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-26 23:42:29 +00:00
|
|
|
|
if (config.JobOptions.Scrobble.Enabled)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Adding Scrobble Jobs...");
|
|
|
|
|
|
|
|
|
|
var scrobbleKey = new JobKey("scrobble-watcher-agile", "scrobble");
|
|
|
|
|
|
|
|
|
|
options.AddJob<ScrobbleWatcherJob>(j => j
|
|
|
|
|
.WithDescription("Watch recent scrobbles and mirror to database")
|
|
|
|
|
.WithIdentity(scrobbleKey)
|
|
|
|
|
.UsingJobData("IsFull", false)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
options.AddTrigger(t => t
|
|
|
|
|
.WithIdentity("scrobble-watcher-agile-trigger")
|
|
|
|
|
.ForJob(scrobbleKey)
|
|
|
|
|
.StartNow()
|
|
|
|
|
.WithSimpleSchedule(x => x.WithInterval(config.JobOptions.Scrobble.InterJobDelay).RepeatForever())
|
|
|
|
|
.WithDescription("Periodic trigger for scrobble watcher")
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var fullScrobbleKey = new JobKey("scrobble-watcher-full", "scrobble");
|
|
|
|
|
|
|
|
|
|
options.AddJob<ScrobbleWatcherJob>(j => j
|
|
|
|
|
.WithDescription("Check all scrobbles and mirror to database")
|
|
|
|
|
.WithIdentity(fullScrobbleKey)
|
|
|
|
|
.UsingJobData("IsFull", true)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
options.AddTrigger(t => t
|
|
|
|
|
.WithIdentity("scrobble-watcher-full-trigger")
|
|
|
|
|
.ForJob(fullScrobbleKey)
|
|
|
|
|
.WithCronSchedule(config.JobOptions.Scrobble.FullScrobbleCron)
|
|
|
|
|
.WithDescription("Periodic trigger for scrobble watcher")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Skipping Scrobble Jobs...");
|
|
|
|
|
}
|
2022-02-25 21:07:22 +00:00
|
|
|
|
});
|
|
|
|
|
|
2022-02-26 23:42:29 +00:00
|
|
|
|
services.AddQuartzHostedService(options => {
|
2022-02-25 21:07:22 +00:00
|
|
|
|
|
|
|
|
|
options.WaitForJobsToComplete = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddTransient<ScrobbleWatcherJob>();
|
|
|
|
|
services.AddTransient<IJob, ScrobbleWatcherJob>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-18 00:08:42 +00:00
|
|
|
|
public static IServiceCollection ConfigureDb(this IServiceCollection services, RootOptions config)
|
|
|
|
|
{
|
|
|
|
|
if (config.DatabaseOptions.Enabled)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("> Adding Databse Context...");
|
|
|
|
|
services.AddDbContext<ApplicationDbContext>(options =>
|
|
|
|
|
options.UseNpgsql(config.DatabaseOptions.ConnectionString)
|
|
|
|
|
);
|
|
|
|
|
|
2022-02-24 00:27:34 +00:00
|
|
|
|
services.AddTransient<IScrobbleRepository, ScrobbleRepository>();
|
2022-02-26 21:28:51 +00:00
|
|
|
|
services.AddTransient<IScrobbleMappingRepository, ScrobbleMappingRepository>();
|
2022-02-24 00:27:34 +00:00
|
|
|
|
|
2022-02-18 00:08:42 +00:00
|
|
|
|
services.AddHostedService<MigratorService>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection ConfigureEqual(this IServiceCollection services, RootOptions config)
|
|
|
|
|
{
|
|
|
|
|
switch (config.Equality)
|
|
|
|
|
{
|
|
|
|
|
case EqualityChecker.Uri:
|
|
|
|
|
Console.WriteLine("> Using Uri Equality");
|
|
|
|
|
services.AddSingleton<IEqual, UriEqual>();
|
|
|
|
|
break;
|
|
|
|
|
case EqualityChecker.String:
|
|
|
|
|
Console.WriteLine("> Using String Equality");
|
|
|
|
|
services.AddSingleton<IEqual, StringEqual>();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|