File tree Expand file tree Collapse file tree
src/OrchardCoreContrib.Contents Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -7,4 +7,6 @@ public interface ISharedDraftLinkService
77 Task < string > GenerateLinkAsync ( ContentItem contentItem ) ;
88
99 Task < ContentItem > GetDraftContentAsync ( string token ) ;
10+
11+ Task < int > CleanupExpiredLinksAsync ( ) ;
1012}
Original file line number Diff line number Diff line change 1+ using Microsoft . Extensions . DependencyInjection ;
2+ using Microsoft . Extensions . Logging ;
3+ using OrchardCore . BackgroundTasks ;
4+
5+ namespace OrchardCoreContrib . Contents . Services ;
6+
7+ [ BackgroundTask ( Schedule = "0 0 * * *" , Description = "Shared Draft Cleanup Background Service" ) ]
8+ public class SharedDraftCleanupTask ( ISharedDraftLinkService linkService ) : IBackgroundTask
9+ {
10+ public async Task DoWorkAsync ( IServiceProvider serviceProvider , CancellationToken cancellationToken )
11+ {
12+ var count = await linkService . CleanupExpiredLinksAsync ( ) ;
13+
14+ if ( count > 0 )
15+ {
16+ var logger = serviceProvider . GetRequiredService < ILogger < SharedDraftCleanupTask > > ( ) ;
17+
18+ logger . LogInformation ( "Cleaned up {Count} expired draft links." , count ) ;
19+ }
20+ }
21+ }
22+
Original file line number Diff line number Diff line change @@ -50,4 +50,19 @@ public async Task<ContentItem> GetDraftContentAsync(string token)
5050 ? null
5151 : await contentManager . GetAsync ( link . ContentItemId , VersionOptions . Draft ) ;
5252 }
53+
54+ public async Task < int > CleanupExpiredLinksAsync ( )
55+ {
56+ var expiredLinks = await session . Query < SharedDraftLink , SharedDraftLinkIndex > ( )
57+ . Where ( l => l . ExpirationUtc < DateTime . UtcNow )
58+ . ListAsync ( ) ;
59+
60+ foreach ( var link in expiredLinks )
61+ {
62+ session . Delete ( link ) ;
63+ }
64+
65+ return expiredLinks . Count ( ) ;
66+ }
67+
5368}
Original file line number Diff line number Diff line change 11using Microsoft . Extensions . DependencyInjection ;
2+ using OrchardCore . BackgroundTasks ;
23using OrchardCore . ContentManagement . Display . ContentDisplay ;
34using OrchardCore . Data ;
45using OrchardCore . Data . Migration ;
@@ -15,13 +16,15 @@ public sealed class Startup : StartupBase
1516{
1617 public override void ConfigureServices ( IServiceCollection services )
1718 {
18- services . AddScoped < ISharedDraftLinkService , SharedDraftLinkService > ( ) ;
19+ services . AddSingleton < IBackgroundTask , SharedDraftCleanupTask > ( ) ;
1920
2021 services . AddDataMigration < Migrations > ( ) ;
2122
2223 services . AddIndexProvider < SharedDraftLinkIndexProvider > ( ) ;
2324
2425 services . AddScoped < IContentDisplayDriver , ShareDraftContentDriver > ( ) ;
26+ services . AddScoped < ISharedDraftLinkService , SharedDraftLinkService > ( ) ;
27+
2528 services . AddPermissionProvider < Permissions > ( ) ;
2629 }
2730}
You can’t perform that action at this time.
0 commit comments