2021-10-28 21:26:57 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2021-10-28 23:05:07 +01:00
|
|
|
using System.Text.Json;
|
2021-10-28 21:26:57 +01:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
2021-10-29 22:35:34 +01:00
|
|
|
|
2021-10-28 23:05:07 +01:00
|
|
|
using StackExchange.Redis;
|
2021-10-28 21:26:57 +01:00
|
|
|
|
|
|
|
namespace Selector.Cache
|
|
|
|
{
|
2021-10-28 23:05:07 +01:00
|
|
|
public class Publisher : IConsumer
|
2021-10-28 21:26:57 +01:00
|
|
|
{
|
|
|
|
private readonly IPlayerWatcher Watcher;
|
2021-10-28 23:05:07 +01:00
|
|
|
private readonly ISubscriber Subscriber;
|
|
|
|
private readonly ILogger<Publisher> Logger;
|
2021-10-28 21:26:57 +01:00
|
|
|
|
|
|
|
public CancellationToken CancelToken { get; set; }
|
|
|
|
|
2021-10-28 23:05:07 +01:00
|
|
|
public Publisher(
|
2021-10-28 21:26:57 +01:00
|
|
|
IPlayerWatcher watcher,
|
2021-10-28 23:05:07 +01:00
|
|
|
ISubscriber subscriber,
|
|
|
|
ILogger<Publisher> logger = null,
|
2021-10-28 21:26:57 +01:00
|
|
|
CancellationToken token = default
|
|
|
|
){
|
|
|
|
Watcher = watcher;
|
2021-10-28 23:05:07 +01:00
|
|
|
Subscriber = subscriber;
|
|
|
|
Logger = logger ?? NullLogger<Publisher>.Instance;
|
2021-10-28 21:26:57 +01:00
|
|
|
CancelToken = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Callback(object sender, ListeningChangeEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.Current is null) return;
|
|
|
|
|
2021-12-18 23:06:21 +00:00
|
|
|
Task.Run(async () => {
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await AsyncCallback(e);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.LogError(e, "Error occured during callback");
|
|
|
|
}
|
|
|
|
}, CancelToken);
|
2021-10-28 21:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task AsyncCallback(ListeningChangeEventArgs e)
|
|
|
|
{
|
2021-12-21 22:22:52 +00:00
|
|
|
var payload = JsonSerializer.Serialize((CurrentlyPlayingDTO) e, JsonContext.Default.CurrentlyPlayingDTO);
|
2021-10-29 22:35:34 +01:00
|
|
|
|
2021-11-05 18:41:45 +00:00
|
|
|
Logger.LogTrace($"Publishing current for [{e.Id}/{e.SpotifyUsername}]");
|
2021-10-29 22:35:34 +01:00
|
|
|
|
2021-11-05 07:58:48 +00:00
|
|
|
// TODO: currently using spotify username for cache key, use db username
|
2021-11-05 18:41:45 +00:00
|
|
|
var receivers = await Subscriber.PublishAsync(Key.CurrentlyPlaying(e.Id), payload);
|
2021-10-29 22:35:34 +01:00
|
|
|
|
2021-11-05 18:41:45 +00:00
|
|
|
Logger.LogDebug($"Published current for [{e.Id}/{e.SpotifyUsername}], {receivers} receivers");
|
2021-10-28 21:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Subscribe(IWatcher watch = null)
|
|
|
|
{
|
|
|
|
var watcher = watch ?? Watcher ?? throw new ArgumentNullException("No watcher provided");
|
|
|
|
|
|
|
|
if (watcher is IPlayerWatcher watcherCast)
|
|
|
|
{
|
|
|
|
watcherCast.ItemChange += Callback;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Provided watcher is not a PlayerWatcher");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Unsubscribe(IWatcher watch = null)
|
|
|
|
{
|
|
|
|
var watcher = watch ?? Watcher ?? throw new ArgumentNullException("No watcher provided");
|
|
|
|
|
|
|
|
if (watcher is IPlayerWatcher watcherCast)
|
|
|
|
{
|
|
|
|
watcherCast.ItemChange -= Callback;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Provided watcher is not a PlayerWatcher");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|