[rss] possible fix for empty feed name in logs & RssFetch service cleanup

This commit is contained in:
West14
2022-04-21 14:26:35 +03:00
parent 98cc8b8737
commit 00bfa59d11
2 changed files with 14 additions and 12 deletions
+2
View File
@@ -80,5 +80,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Data
/// All exists subscriber for this feed.
/// </summary>
public virtual ICollection<Subscriber> Subscribers { get; set; }
public override string ToString() => Title;
}
}
+12 -12
View File
@@ -39,7 +39,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
private readonly IServiceScopeFactory _scopeFactory;
private readonly ConcurrentQueue<UserMessage> _queue;
private TimeSpan timerPeriod => TimeSpan.FromSeconds(45);
private static TimeSpan TimerPeriod => TimeSpan.FromSeconds(45);
public RssFetch(ILogger<RssFetch> logger, ConcurrentQueue<UserMessage> queue, IServiceScopeFactory scopeFactory)
{
@@ -57,7 +57,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("RSS fetcher service is starting.");
_timer = new Timer(DoFetch, null, TimeSpan.Zero, timerPeriod);
_timer = new Timer(DoFetch, null, TimeSpan.Zero, TimerPeriod);
return Task.CompletedTask;
}
@@ -99,7 +99,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
using var scope = _scopeFactory.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<RichSiteSummaryContext>();
var period = scope.ServiceProvider.GetRequiredService<IConfiguration>()
.GetValue<UInt16>("rssFetchPeriod");
.GetValue<ushort>("rssFetchPeriod");
var feeds = await dbContext.Feeds.ForFetching(period);
_logger.LogDebug("Received {count} feeds for fetching", new {count = feeds.Length});
@@ -116,7 +116,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
}
finally
{
_timer.Change(timerPeriod, timerPeriod);
_timer.Change(TimerPeriod, TimerPeriod);
}
}
@@ -160,14 +160,14 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
var text = post.MessageText;
foreach (var subscriber in subscribers)
{
var message = new UserMessage()
var message = new UserMessage
{
ChatId = new ChatId(subscriber.SubscriberId), DisableWebPagePreview = true,
ParseMode = ParseMode.Html, Text = text, ImageUrl = postsImages.GetValueOrDefault(post)
};
_queue.Enqueue(message);
_logger.LogDebug($"Enqueued message for {subscriber.SubscriberId}");
_logger.LogDebug("Queued message for {SubscriberId}", subscriber.SubscriberId);
}
}
}
@@ -177,11 +177,11 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
{
try
{
return await FeedReader.ReadAsync(feed.Url.ToString());
return await FeedReader.ReadAsync(feed.Url);
}
catch (Exception e)
{
_logger.LogError($"Feed {feed} can't be fetched: {e.Message}");
_logger.LogError("Feed {feed} can't be fetched: {error}", feed, e.Message);
return null;
}
}
@@ -193,9 +193,9 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
private async Task<string> FetchImageUrl(FeedItem item)
{
var feedItem = item.SpecificItem;
if (feedItem is Rss20FeedItem)
if (feedItem is Rss20FeedItem rss20FeedItem)
{
var enclosure = ((Rss20FeedItem)feedItem).Enclosure;
var enclosure = rss20FeedItem.Enclosure;
if (enclosure != null && IsValidImage(enclosure.MediaType))
{
return enclosure.Url;
@@ -237,7 +237,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
}
catch (Exception e)
{
_logger.LogError($"Caused error when fetching image for RSS post: {e.Message}");
_logger.LogError("Caused error when fetching image for RSS post: {error}", e.Message);
}
}
@@ -251,7 +251,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
/// <returns>True, if Telegram maybe can "use" this image, false if not.</returns>
private static bool IsValidImage(string contentType)
{
return new string[]
return new[]
{
"image/jpeg",
"image/bmp",