-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/release/13.0' into v13/feature…
…/webhook-all-the-things
- Loading branch information
Showing
11 changed files
with
188 additions
and
14 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
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
69 changes: 69 additions & 0 deletions
69
src/Umbraco.Infrastructure/BackgroundJobs/Jobs/WebhookLoggingCleanup.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,69 @@ | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Umbraco.Cms.Core; | ||
using Umbraco.Cms.Core.Configuration.Models; | ||
using Umbraco.Cms.Core.Models; | ||
using Umbraco.Cms.Core.Persistence.Repositories; | ||
using Umbraco.Cms.Core.Scoping; | ||
using Umbraco.Extensions; | ||
|
||
namespace Umbraco.Cms.Infrastructure.BackgroundJobs.Jobs; | ||
|
||
/// <summary> | ||
/// Daily background job that removes all webhook log data older than x days as defined by <see cref="WebhookSettings.KeepLogsForDays"/> | ||
/// </summary> | ||
public class WebhookLoggingCleanup : IRecurringBackgroundJob | ||
{ | ||
private readonly ILogger<WebhookLoggingCleanup> _logger; | ||
private readonly WebhookSettings _webhookSettings; | ||
private readonly IWebhookLogRepository _webhookLogRepository; | ||
private readonly ICoreScopeProvider _coreScopeProvider; | ||
|
||
public WebhookLoggingCleanup(ILogger<WebhookLoggingCleanup> logger, IOptionsMonitor<WebhookSettings> webhookSettings, IWebhookLogRepository webhookLogRepository, ICoreScopeProvider coreScopeProvider) | ||
{ | ||
_logger = logger; | ||
_webhookSettings = webhookSettings.CurrentValue; | ||
_webhookLogRepository = webhookLogRepository; | ||
_coreScopeProvider = coreScopeProvider; | ||
} | ||
|
||
/// <inheritdoc /> | ||
// No-op event as the period never changes on this job | ||
public event EventHandler PeriodChanged | ||
{ | ||
add { } remove { } | ||
} | ||
|
||
/// <inheritdoc /> | ||
public TimeSpan Period => TimeSpan.FromDays(1); | ||
|
||
/// <inheritdoc /> | ||
public TimeSpan Delay { get; } = TimeSpan.FromSeconds(20); | ||
|
||
/// <inheritdoc /> | ||
public async Task RunJobAsync() | ||
{ | ||
if (_webhookSettings.EnableLoggingCleanup is false) | ||
{ | ||
_logger.LogInformation("WebhookLoggingCleanup task will not run as it has been globally disabled via configuration"); | ||
return; | ||
} | ||
|
||
IEnumerable<WebhookLog> webhookLogs; | ||
using (ICoreScope scope = _coreScopeProvider.CreateCoreScope()) | ||
{ | ||
scope.ReadLock(Constants.Locks.WebhookLogs); | ||
webhookLogs = await _webhookLogRepository.GetOlderThanDate(DateTime.Now - TimeSpan.FromDays(_webhookSettings.KeepLogsForDays)); | ||
scope.Complete(); | ||
} | ||
|
||
foreach (IEnumerable<WebhookLog> group in webhookLogs.InGroupsOf(Constants.Sql.MaxParameterCount)) | ||
{ | ||
using ICoreScope scope = _coreScopeProvider.CreateCoreScope(); | ||
scope.WriteLock(Constants.Locks.WebhookLogs); | ||
await _webhookLogRepository.DeleteByIds(group.Select(x => x.Id).ToArray()); | ||
|
||
scope.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
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
30 changes: 30 additions & 0 deletions
30
src/Umbraco.Infrastructure/Migrations/Upgrade/V_13_0_0/AddWebhookDatabaseLock.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,30 @@ | ||
using NPoco; | ||
using Umbraco.Cms.Core; | ||
using Umbraco.Cms.Infrastructure.Persistence; | ||
using Umbraco.Cms.Infrastructure.Persistence.Dtos; | ||
using Umbraco.Extensions; | ||
|
||
namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_13_0_0; | ||
|
||
public class AddWebhookDatabaseLock : MigrationBase | ||
{ | ||
public AddWebhookDatabaseLock(IMigrationContext context) | ||
: base(context) | ||
{ | ||
} | ||
|
||
protected override void Migrate() | ||
{ | ||
Sql<ISqlContext> sql = Database.SqlContext.Sql() | ||
.Select<LockDto>() | ||
.From<LockDto>() | ||
.Where<LockDto>(x => x.Id == Constants.Locks.WebhookLogs); | ||
|
||
LockDto? webhookLogsLock = Database.FirstOrDefault<LockDto>(sql); | ||
|
||
if (webhookLogsLock is null) | ||
{ | ||
Database.Insert(Constants.DatabaseSchema.Tables.Lock, "id", false, new LockDto { Id = Constants.Locks.WebhookLogs, Name = "WebhookLogs" }); | ||
} | ||
} | ||
} |
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
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