2021-10-11 01:06:43 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-11-25 18:26:20 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-10-12 23:29:05 +01:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2021-10-11 01:06:43 +01:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
2021-10-28 23:05:07 +01:00
|
|
|
|
using Selector.Cache;
|
2021-11-25 18:26:20 +00:00
|
|
|
|
using Selector.Model;
|
2022-02-16 23:38:45 +00:00
|
|
|
|
using Selector.Model.Extensions;
|
2021-12-19 18:44:03 +00:00
|
|
|
|
using Selector.Events;
|
|
|
|
|
using System.Collections.Concurrent;
|
2022-09-30 08:41:44 +01:00
|
|
|
|
using Selector.CLI.Consumer;
|
2021-10-28 23:05:07 +01:00
|
|
|
|
|
2021-10-11 01:06:43 +01:00
|
|
|
|
namespace Selector.CLI
|
|
|
|
|
{
|
2021-11-25 18:26:20 +00:00
|
|
|
|
class DbWatcherService : IHostedService
|
2021-10-11 01:06:43 +01:00
|
|
|
|
{
|
2021-11-25 18:26:20 +00:00
|
|
|
|
private const int PollPeriod = 1000;
|
2021-10-12 23:29:05 +01:00
|
|
|
|
|
2021-11-29 21:48:00 +00:00
|
|
|
|
private readonly ILogger<DbWatcherService> Logger;
|
2021-11-25 18:26:20 +00:00
|
|
|
|
private readonly IServiceProvider ServiceProvider;
|
2021-12-19 18:44:03 +00:00
|
|
|
|
private readonly UserEventBus UserEventBus;
|
2021-11-29 21:04:15 +00:00
|
|
|
|
|
2021-10-12 23:29:05 +01:00
|
|
|
|
private readonly IWatcherFactory WatcherFactory;
|
|
|
|
|
private readonly IWatcherCollectionFactory WatcherCollectionFactory;
|
|
|
|
|
private readonly IRefreshTokenFactoryProvider SpotifyFactory;
|
2021-10-28 23:05:07 +01:00
|
|
|
|
|
2021-11-29 21:04:15 +00:00
|
|
|
|
private readonly IAudioFeatureInjectorFactory AudioFeatureInjectorFactory;
|
|
|
|
|
private readonly IPlayCounterFactory PlayCounterFactory;
|
2021-10-11 01:06:43 +01:00
|
|
|
|
|
2021-12-20 23:04:53 +00:00
|
|
|
|
private readonly IUserEventFirerFactory UserEventFirerFactory;
|
|
|
|
|
|
2021-11-29 21:04:15 +00:00
|
|
|
|
private readonly IPublisherFactory PublisherFactory;
|
|
|
|
|
private readonly ICacheWriterFactory CacheWriterFactory;
|
2022-09-30 08:41:44 +01:00
|
|
|
|
|
|
|
|
|
private readonly IMappingPersisterFactory MappingPersisterFactory;
|
|
|
|
|
|
2021-12-19 18:44:03 +00:00
|
|
|
|
private ConcurrentDictionary<string, IWatcherCollection> Watchers { get; set; } = new();
|
2021-10-11 23:19:19 +01:00
|
|
|
|
|
2021-11-25 18:26:20 +00:00
|
|
|
|
public DbWatcherService(
|
2021-10-12 23:29:05 +01:00
|
|
|
|
IWatcherFactory watcherFactory,
|
|
|
|
|
IWatcherCollectionFactory watcherCollectionFactory,
|
|
|
|
|
IRefreshTokenFactoryProvider spotifyFactory,
|
2021-11-29 21:04:15 +00:00
|
|
|
|
|
|
|
|
|
IAudioFeatureInjectorFactory audioFeatureInjectorFactory,
|
|
|
|
|
IPlayCounterFactory playCounterFactory,
|
|
|
|
|
|
2021-12-19 18:44:03 +00:00
|
|
|
|
UserEventBus userEventBus,
|
2021-11-29 21:04:15 +00:00
|
|
|
|
|
2021-11-29 21:48:00 +00:00
|
|
|
|
ILogger<DbWatcherService> logger,
|
2021-12-04 12:49:09 +00:00
|
|
|
|
IServiceProvider serviceProvider,
|
|
|
|
|
|
|
|
|
|
IPublisherFactory publisherFactory = null,
|
2021-12-20 23:04:53 +00:00
|
|
|
|
ICacheWriterFactory cacheWriterFactory = null,
|
|
|
|
|
|
2022-09-30 08:41:44 +01:00
|
|
|
|
IMappingPersisterFactory mappingPersisterFactory = null,
|
|
|
|
|
|
2021-12-20 23:04:53 +00:00
|
|
|
|
IUserEventFirerFactory userEventFirerFactory = null
|
2021-12-04 12:49:09 +00:00
|
|
|
|
)
|
|
|
|
|
{
|
2021-11-29 21:48:00 +00:00
|
|
|
|
Logger = logger;
|
2021-11-29 21:04:15 +00:00
|
|
|
|
ServiceProvider = serviceProvider;
|
2021-12-19 18:44:03 +00:00
|
|
|
|
UserEventBus = userEventBus;
|
2021-11-29 21:04:15 +00:00
|
|
|
|
|
2021-10-12 23:29:05 +01:00
|
|
|
|
WatcherFactory = watcherFactory;
|
|
|
|
|
WatcherCollectionFactory = watcherCollectionFactory;
|
|
|
|
|
SpotifyFactory = spotifyFactory;
|
2021-11-29 21:04:15 +00:00
|
|
|
|
|
|
|
|
|
AudioFeatureInjectorFactory = audioFeatureInjectorFactory;
|
|
|
|
|
PlayCounterFactory = playCounterFactory;
|
|
|
|
|
|
2021-12-20 23:04:53 +00:00
|
|
|
|
UserEventFirerFactory = userEventFirerFactory;
|
|
|
|
|
|
2021-11-29 21:04:15 +00:00
|
|
|
|
PublisherFactory = publisherFactory;
|
|
|
|
|
CacheWriterFactory = cacheWriterFactory;
|
2022-09-30 08:41:44 +01:00
|
|
|
|
|
|
|
|
|
MappingPersisterFactory = mappingPersisterFactory;
|
2021-10-11 01:06:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 23:29:05 +01:00
|
|
|
|
public async Task StartAsync(CancellationToken cancellationToken)
|
2021-10-11 01:06:43 +01:00
|
|
|
|
{
|
2021-11-25 18:26:20 +00:00
|
|
|
|
Logger.LogInformation("Starting database watcher service...");
|
2021-10-11 01:06:43 +01:00
|
|
|
|
|
2021-11-25 18:26:20 +00:00
|
|
|
|
var watcherIndices = await InitInstances();
|
2021-12-19 18:44:03 +00:00
|
|
|
|
AttachEventBus();
|
2021-10-11 23:19:19 +01:00
|
|
|
|
|
2021-12-18 17:45:34 +00:00
|
|
|
|
Logger.LogInformation("Starting {count} affected watcher collection(s)...", watcherIndices.Count());
|
2021-10-12 23:29:05 +01:00
|
|
|
|
StartWatcherCollections(watcherIndices);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 18:26:20 +00:00
|
|
|
|
private async Task<IEnumerable<string>> InitInstances()
|
2021-10-12 23:29:05 +01:00
|
|
|
|
{
|
2021-11-25 18:26:20 +00:00
|
|
|
|
using var scope = ServiceProvider.CreateScope();
|
|
|
|
|
var db = scope.ServiceProvider.GetService<ApplicationDbContext>();
|
|
|
|
|
|
2021-10-12 23:29:05 +01:00
|
|
|
|
var indices = new HashSet<string>();
|
|
|
|
|
|
2021-12-15 22:04:41 +00:00
|
|
|
|
foreach (var dbWatcher in db.Watcher
|
|
|
|
|
.Include(w => w.User)
|
|
|
|
|
.Where(w => !string.IsNullOrWhiteSpace(w.User.SpotifyRefreshToken)))
|
2021-10-12 23:29:05 +01:00
|
|
|
|
{
|
2021-11-25 18:26:20 +00:00
|
|
|
|
var watcherCollectionIdx = dbWatcher.UserId;
|
2021-10-12 23:29:05 +01:00
|
|
|
|
indices.Add(watcherCollectionIdx);
|
|
|
|
|
|
2021-12-19 18:44:03 +00:00
|
|
|
|
await InitInstance(dbWatcher);
|
|
|
|
|
}
|
2021-10-12 23:29:05 +01:00
|
|
|
|
|
2021-12-19 18:44:03 +00:00
|
|
|
|
return indices;
|
|
|
|
|
}
|
2021-10-12 23:29:05 +01:00
|
|
|
|
|
2021-12-19 18:44:03 +00:00
|
|
|
|
private async Task<IWatcherContext> InitInstance(Watcher dbWatcher)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogInformation("Creating new [{type}] watcher", dbWatcher.Type);
|
2021-10-12 23:29:05 +01:00
|
|
|
|
|
2021-12-19 18:44:03 +00:00
|
|
|
|
var watcherCollectionIdx = dbWatcher.UserId;
|
2021-11-25 18:26:20 +00:00
|
|
|
|
|
2021-12-19 18:44:03 +00:00
|
|
|
|
if (!Watchers.ContainsKey(watcherCollectionIdx))
|
|
|
|
|
Watchers[watcherCollectionIdx] = WatcherCollectionFactory.Get();
|
2021-10-12 23:29:05 +01:00
|
|
|
|
|
2021-12-19 18:44:03 +00:00
|
|
|
|
var watcherCollection = Watchers[watcherCollectionIdx];
|
|
|
|
|
|
|
|
|
|
Logger.LogDebug("Getting Spotify factory");
|
|
|
|
|
var spotifyFactory = await SpotifyFactory.GetFactory(dbWatcher.User.SpotifyRefreshToken);
|
|
|
|
|
|
|
|
|
|
IWatcher watcher = null;
|
|
|
|
|
List<IConsumer> consumers = new();
|
|
|
|
|
|
|
|
|
|
switch (dbWatcher.Type)
|
|
|
|
|
{
|
|
|
|
|
case WatcherType.Player:
|
|
|
|
|
watcher = await WatcherFactory.Get<PlayerWatcher>(spotifyFactory, id: dbWatcher.UserId, pollPeriod: PollPeriod);
|
|
|
|
|
|
|
|
|
|
consumers.Add(await AudioFeatureInjectorFactory.Get(spotifyFactory));
|
|
|
|
|
if (CacheWriterFactory is not null) consumers.Add(await CacheWriterFactory.Get());
|
|
|
|
|
if (PublisherFactory is not null) consumers.Add(await PublisherFactory.Get());
|
|
|
|
|
|
2022-09-30 08:41:44 +01:00
|
|
|
|
if (MappingPersisterFactory is not null) consumers.Add(await MappingPersisterFactory.Get());
|
|
|
|
|
|
2021-12-20 23:04:53 +00:00
|
|
|
|
if (UserEventFirerFactory is not null) consumers.Add(await UserEventFirerFactory.Get());
|
|
|
|
|
|
2022-02-16 23:38:45 +00:00
|
|
|
|
if (dbWatcher.User.LastFmConnected())
|
2021-12-19 18:44:03 +00:00
|
|
|
|
{
|
|
|
|
|
consumers.Add(await PlayCounterFactory.Get(creds: new() { Username = dbWatcher.User.LastFmUsername }));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.LogDebug("[{username}] No Last.fm username, skipping play counter", dbWatcher.User.UserName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case WatcherType.Playlist:
|
|
|
|
|
throw new NotImplementedException("Playlist watchers not implemented");
|
|
|
|
|
// break;
|
2021-10-12 23:29:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 18:44:03 +00:00
|
|
|
|
return watcherCollection.Add(watcher, consumers);
|
2021-10-12 23:29:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StartWatcherCollections(IEnumerable<string> indices)
|
|
|
|
|
{
|
|
|
|
|
foreach (var index in indices)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-12-18 17:45:34 +00:00
|
|
|
|
Logger.LogInformation("Starting watcher collection [{index}]", index);
|
2021-10-12 23:29:05 +01:00
|
|
|
|
Watchers[index].Start();
|
|
|
|
|
}
|
|
|
|
|
catch (KeyNotFoundException)
|
|
|
|
|
{
|
2021-12-18 17:45:34 +00:00
|
|
|
|
Logger.LogError("Unable to retrieve watcher collection [{index}] when starting", index);
|
2021-10-12 23:29:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-11 01:06:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogInformation("Shutting down");
|
2021-10-11 23:19:19 +01:00
|
|
|
|
|
|
|
|
|
foreach((var key, var watcher) in Watchers)
|
|
|
|
|
{
|
2021-12-18 17:45:34 +00:00
|
|
|
|
Logger.LogInformation("Stopping watcher collection [{key}]", key);
|
2021-10-11 23:19:19 +01:00
|
|
|
|
watcher.Stop();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 18:44:03 +00:00
|
|
|
|
DetachEventBus();
|
|
|
|
|
|
2021-10-11 01:06:43 +01:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2021-12-19 18:44:03 +00:00
|
|
|
|
|
|
|
|
|
private void AttachEventBus()
|
|
|
|
|
{
|
|
|
|
|
UserEventBus.SpotifyLinkChange += SpotifyChangeCallback;
|
|
|
|
|
UserEventBus.LastfmCredChange += LastfmChangeCallback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DetachEventBus()
|
|
|
|
|
{
|
|
|
|
|
UserEventBus.SpotifyLinkChange -= SpotifyChangeCallback;
|
|
|
|
|
UserEventBus.LastfmCredChange -= LastfmChangeCallback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async void SpotifyChangeCallback(object sender, SpotifyLinkChange change)
|
|
|
|
|
{
|
|
|
|
|
if(Watchers.ContainsKey(change.UserId))
|
|
|
|
|
{
|
|
|
|
|
Logger.LogDebug("Setting new Spotify link state for [{username}], [{}]", change.UserId, change.NewLinkState);
|
|
|
|
|
|
|
|
|
|
var watcherCollection = Watchers[change.UserId];
|
|
|
|
|
|
|
|
|
|
if(change.NewLinkState)
|
|
|
|
|
{
|
|
|
|
|
watcherCollection.Start();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
watcherCollection.Stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
using var scope = ServiceProvider.CreateScope();
|
|
|
|
|
var db = scope.ServiceProvider.GetService<ApplicationDbContext>();
|
|
|
|
|
|
|
|
|
|
var watcherEnum = db.Watcher
|
|
|
|
|
.Include(w => w.User)
|
|
|
|
|
.Where(w => w.UserId == change.UserId);
|
|
|
|
|
|
|
|
|
|
foreach (var dbWatcher in watcherEnum)
|
|
|
|
|
{
|
|
|
|
|
var context = await InitInstance(dbWatcher);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Watchers[change.UserId].Start();
|
|
|
|
|
|
|
|
|
|
Logger.LogDebug("Started {} watchers for [{username}]", watcherEnum.Count(), change.UserId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void LastfmChangeCallback(object sender, LastfmChange change)
|
|
|
|
|
{
|
|
|
|
|
if (Watchers.ContainsKey(change.UserId))
|
|
|
|
|
{
|
|
|
|
|
Logger.LogDebug("Setting new username for [{}], [{}]", change.UserId, change.NewUsername);
|
|
|
|
|
|
|
|
|
|
var watcherCollection = Watchers[change.UserId];
|
|
|
|
|
|
|
|
|
|
foreach(var watcher in watcherCollection.Consumers)
|
|
|
|
|
{
|
|
|
|
|
if(watcher is PlayCounter counter)
|
|
|
|
|
{
|
|
|
|
|
counter.Credentials.Username = change.NewUsername;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Logger.LogDebug("No watchers running for [{username}], skipping Spotify event", change.UserId);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-11 01:06:43 +01:00
|
|
|
|
}
|
|
|
|
|
}
|