-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathBlogPostPublisher.cs
More file actions
86 lines (70 loc) · 2.93 KB
/
Copy pathBlogPostPublisher.cs
File metadata and controls
86 lines (70 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Threading;
using System.Threading.Tasks;
using LinkDotNet.Blog.Domain;
using LinkDotNet.Blog.Infrastructure;
using LinkDotNet.Blog.Infrastructure.Persistence;
using LinkDotNet.Blog.Web.Features.Services;
using NCronJob;
using Microsoft.Extensions.Logging;
namespace LinkDotNet.Blog.Web.Features;
public sealed partial class BlogPostPublisher : IJob
{
private readonly ILogger<BlogPostPublisher> logger;
private readonly IRepository<BlogPost> repository;
private readonly ICacheInvalidator cacheInvalidator;
private readonly TimeProvider timeProvider;
public BlogPostPublisher(
IRepository<BlogPost> repository,
ICacheInvalidator cacheInvalidator,
TimeProvider timeProvider,
ILogger<BlogPostPublisher> logger)
{
this.repository = repository;
this.cacheInvalidator = cacheInvalidator;
this.timeProvider = timeProvider;
this.logger = logger;
}
public async Task RunAsync(IJobExecutionContext context, CancellationToken token)
{
ArgumentNullException.ThrowIfNull(context);
LogPublishStarting();
var publishedPosts = await PublishScheduledBlogPostsAsync();
context.Output = publishedPosts;
LogPublishStopping();
}
private async Task<int> PublishScheduledBlogPostsAsync()
{
LogCheckingForScheduledBlogPosts();
var blogPostsToPublish = await GetScheduledBlogPostsAsync();
foreach (var blogPost in blogPostsToPublish)
{
blogPost.Publish();
await repository.StoreAsync(blogPost);
LogPublishedBlogPost(blogPost.Id);
}
if (blogPostsToPublish.Count > 0)
{
await cacheInvalidator.ClearCacheAsync();
}
return blogPostsToPublish.Count;
}
private async Task<IPagedList<BlogPost>> GetScheduledBlogPostsAsync()
{
var now = timeProvider.GetUtcNow().DateTime;
var scheduledBlogPosts = await repository.GetAllAsync(
filter: b => b.ScheduledPublishDate != null && b.ScheduledPublishDate <= now);
LogFoundScheduledBlogPosts(scheduledBlogPosts.Count);
return scheduledBlogPosts;
}
[LoggerMessage(Level = LogLevel.Information, Message = "BlogPostPublisher is starting")]
private partial void LogPublishStarting();
[LoggerMessage(Level = LogLevel.Information, Message = "BlogPostPublisher is stopping")]
private partial void LogPublishStopping();
[LoggerMessage(Level = LogLevel.Information, Message = "Found {Count} scheduled blog posts")]
private partial void LogFoundScheduledBlogPosts(int count);
[LoggerMessage(Level = LogLevel.Information, Message = "Publishing blog post with ID {BlogPostId}")]
private partial void LogPublishedBlogPost(string blogPostId);
[LoggerMessage(Level = LogLevel.Information, Message = "Checking for scheduled blog posts")]
private partial void LogCheckingForScheduledBlogPosts();
}