-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add offloader and LikesCounterUpdater
- Loading branch information
1 parent
e4759fa
commit a8b2e11
Showing
16 changed files
with
235 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
Source/WetPicsRebirth/Services/LikesCounterUpdater/ILikesCounterUpdater.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace WetPicsRebirth.Services.LikesCounterUpdater; | ||
|
||
public interface ILikesCounterUpdater | ||
{ | ||
Task Update(MessageToUpdateCounter message); | ||
} | ||
|
||
public record MessageToUpdateCounter(long ChatId, int MessageId); |
49 changes: 49 additions & 0 deletions
49
Source/WetPicsRebirth/Services/LikesCounterUpdater/LikesCounterUpdater.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Text.RegularExpressions; | ||
using Telegram.Bot.Exceptions; | ||
using WetPicsRebirth.Data.Repositories.Abstract; | ||
|
||
namespace WetPicsRebirth.Services.LikesCounterUpdater; | ||
|
||
internal partial class LikesCounterUpdater : ILikesCounterUpdater | ||
{ | ||
[GeneratedRegex("retry after (?<after>\\d+)")] | ||
private static partial Regex RetryAfterRegex(); | ||
|
||
private readonly ITelegramBotClient _telegramBotClient; | ||
private readonly IVotesRepository _votesRepository; | ||
|
||
public LikesCounterUpdater(ITelegramBotClient telegramBotClient, IVotesRepository votesRepository) | ||
{ | ||
_telegramBotClient = telegramBotClient; | ||
_votesRepository = votesRepository; | ||
} | ||
|
||
public async Task Update(MessageToUpdateCounter message) | ||
=> await UpdateMessageWithRetries(message.ChatId, message.MessageId, 0); | ||
|
||
private async Task UpdateMessageWithRetries( | ||
long chatId, | ||
int messageId, | ||
int retryCount, | ||
CancellationToken ct = default) | ||
{ | ||
try | ||
{ | ||
var currentCount = await _votesRepository.GetCountForPost(chatId, messageId); | ||
await _telegramBotClient.EditMessageReplyMarkupAsync( | ||
chatId, | ||
messageId, | ||
Keyboards.WithLikes(currentCount), | ||
ct); | ||
} | ||
catch (ApiRequestException e) when (e.Message.Contains("retry after")) | ||
{ | ||
if (retryCount > 2) | ||
throw; | ||
|
||
var after = int.Parse(RetryAfterRegex().Match(e.Message).Groups["after"].Value); | ||
await Task.Delay(after * 1000, ct); | ||
await UpdateMessageWithRetries(chatId, messageId, retryCount + 1, ct); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...rth/Services/LikesCounterUpdater/LikesCounterUpdaterOffloadServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using WetPicsRebirth.Services.Offload; | ||
|
||
namespace WetPicsRebirth.Services.LikesCounterUpdater; | ||
|
||
public static class LikesCounterUpdaterOffloadServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddLikesCounterUpdaterOffload(this IServiceCollection services) | ||
{ | ||
services.AddTransient<ILikesCounterUpdater, LikesCounterUpdater>(); | ||
services.AddOffload<MessageToUpdateCounter>(options => | ||
{ | ||
options.ItemProcessor | ||
= (x, message) => x.GetRequiredService<ILikesCounterUpdater>().Update(message); | ||
|
||
options.ErrorLogger = (logger, message, exception) => | ||
logger.LogError( | ||
exception, | ||
"Unable to update likes counter for chat {ChatId} message {MessageId}", | ||
message.ChatId, | ||
message.MessageId); | ||
}); | ||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Threading.Channels; | ||
|
||
namespace WetPicsRebirth.Services.Offload; | ||
|
||
internal interface IOffloadReader<T> | ||
{ | ||
ChannelReader<T> Reader { get; } | ||
|
||
void Complete(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace WetPicsRebirth.Services.Offload; | ||
|
||
public interface IOffloader<in T> | ||
{ | ||
Task Offload(T vote); | ||
} |
55 changes: 55 additions & 0 deletions
55
Source/WetPicsRebirth/Services/Offload/OffloadHostedService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace WetPicsRebirth.Services.Offload; | ||
|
||
internal class OffloadHostedService<T> : IHostedService | ||
{ | ||
private readonly IOffloadReader<T> _offload; | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly IOptions<OffloadOptions<T>> _options; | ||
private readonly ILogger<OffloadHostedService<T>> _logger; | ||
|
||
protected OffloadHostedService( | ||
IOffloadReader<T> offload, | ||
IServiceProvider serviceProvider, | ||
IOptions<OffloadOptions<T>> options, | ||
ILogger<OffloadHostedService<T>> logger) | ||
{ | ||
_offload = offload; | ||
_serviceProvider = serviceProvider; | ||
_options = options; | ||
_logger = logger; | ||
} | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
Task.Run(Process, cancellationToken); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private async Task Process() | ||
{ | ||
var reader = _offload.Reader; | ||
|
||
while (await reader.WaitToReadAsync()) | ||
while (reader.TryRead(out var item)) | ||
{ | ||
try | ||
{ | ||
using var scope = _serviceProvider.CreateScope(); | ||
await _options.Value.ItemProcessor(scope.ServiceProvider, item); | ||
} | ||
catch (Exception e) | ||
{ | ||
_options.Value.ErrorLogger(_logger, item, e); | ||
} | ||
} | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
_offload.Complete(); | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace WetPicsRebirth.Services.Offload; | ||
|
||
public class OffloadOptions<T> | ||
{ | ||
public required Func<IServiceProvider, T, Task> ItemProcessor { get; set; } | ||
|
||
public required Action<ILogger, T, Exception> ErrorLogger { get; set; } | ||
} |
17 changes: 17 additions & 0 deletions
17
Source/WetPicsRebirth/Services/Offload/OffloadServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace WetPicsRebirth.Services.Offload; | ||
|
||
public static class OffloadServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddOffload<T>( | ||
this IServiceCollection services, Action<OffloadOptions<T>> configure) | ||
{ | ||
services.Configure(configure); | ||
|
||
services.AddSingleton<Offloader<T>>(); | ||
services.AddTransient<IOffloader<T>>(x => x.GetRequiredService<Offloader<T>>()); | ||
services.AddTransient<IOffloadReader<T>>(x => x.GetRequiredService<Offloader<T>>()); | ||
services.AddHostedService<OffloadHostedService<T>>(); | ||
|
||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Threading.Channels; | ||
|
||
namespace WetPicsRebirth.Services.Offload; | ||
|
||
/// <remarks> | ||
/// Should be registered as a singleton. | ||
/// </remarks>> | ||
internal class Offloader<T> : IOffloader<T>, IOffloadReader<T> | ||
{ | ||
private Channel<T> VotesToTranslate { get; } = Channel.CreateUnbounded<T>(); | ||
|
||
public async Task Offload(T vote) => await VotesToTranslate.Writer.WriteAsync(vote); | ||
|
||
public ChannelReader<T> Reader => VotesToTranslate.Reader; | ||
|
||
public void Complete() => VotesToTranslate.Writer.Complete(); | ||
} |
11 changes: 0 additions & 11 deletions
11
Source/WetPicsRebirth/Services/UserAccounts/ILikesToFavoritesTranslatorScheduler.cs
This file was deleted.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
...etPicsRebirth/Services/UserAccounts/LikesToFavoritesOffloadServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using WetPicsRebirth.Data.Entities; | ||
using WetPicsRebirth.Services.Offload; | ||
|
||
namespace WetPicsRebirth.Services.UserAccounts; | ||
|
||
public static class LikesToFavoritesOffloadServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddLikesToFavoritesOffload(this IServiceCollection services) | ||
{ | ||
services.AddTransient<ILikesToFavoritesTranslator, LikesToFavoritesTranslator>(); | ||
services.AddOffload<Vote>(options => | ||
{ | ||
options.ItemProcessor = (x, vote) => x.GetRequiredService<ILikesToFavoritesTranslator>().Translate(vote); | ||
|
||
options.ErrorLogger = (logger, vote, exception) => | ||
logger.LogError( | ||
exception, | ||
"Unable to fav post chat {ChatId} message {MessageId}", | ||
vote.ChatId, | ||
vote.MessageId); | ||
}); | ||
return services; | ||
} | ||
} |
Oops, something went wrong.