enhance purge job to trim broken urls based on retention policy
This commit is contained in:
parent
4f4258d532
commit
ed353461da
|
@ -3,6 +3,7 @@
|
|||
@inject NavigationManager NavigationManager
|
||||
@inject IUrlMappingService UrlMappingService
|
||||
@inject ISiteService SiteService
|
||||
@inject ISettingService SettingService
|
||||
@inject IStringLocalizer<Index> Localizer
|
||||
@inject IStringLocalizer<SharedResources> SharedLocalizer
|
||||
|
||||
|
@ -62,7 +63,13 @@ else
|
|||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row mb-1 align-items-center">
|
||||
<Label Class="col-sm-3" For="retention" HelpText="Number of days of broken urls to retain" ResourceKey="Retention">Retention (Days): </Label>
|
||||
<div class="col-sm-9">
|
||||
<input id="retention" class="form-control" type="number" min="0" step="1" @bind="@_retention" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<br />
|
||||
<button type="button" class="btn btn-success" @onclick="SaveSiteSettings">@SharedLocalizer["Save"]</button>
|
||||
</TabPanel>
|
||||
|
@ -73,6 +80,7 @@ else
|
|||
private bool _mapped = true;
|
||||
private List<UrlMapping> _urlMappings;
|
||||
private string _capturebrokenurls;
|
||||
private int _retention = 30;
|
||||
|
||||
public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Admin;
|
||||
|
||||
|
@ -80,7 +88,10 @@ else
|
|||
{
|
||||
await GetUrlMappings();
|
||||
_capturebrokenurls = PageState.Site.CaptureBrokenUrls.ToString();
|
||||
}
|
||||
|
||||
var settings = await SettingService.GetSiteSettingsAsync(PageState.Site.SiteId);
|
||||
_retention = int.Parse(SettingService.GetSetting(settings, "UrlMappingRetention", "30"));
|
||||
}
|
||||
|
||||
private async void MappedChanged(ChangeEventArgs e)
|
||||
{
|
||||
|
@ -124,7 +135,12 @@ else
|
|||
var site = PageState.Site;
|
||||
site.CaptureBrokenUrls = bool.Parse(_capturebrokenurls);
|
||||
await SiteService.UpdateSiteAsync(site);
|
||||
AddModuleMessage(Localizer["Success.SaveSiteSettings"], MessageType.Success);
|
||||
|
||||
var settings = await SettingService.GetSiteSettingsAsync(site.SiteId);
|
||||
settings = SettingService.SetSetting(settings, "UrlMappingRetention", _retention.ToString(), true);
|
||||
await SettingService.UpdateSiteSettingsAsync(settings, site.SiteId);
|
||||
|
||||
AddModuleMessage(Localizer["Success.SaveSiteSettings"], MessageType.Success);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
|
@ -162,4 +162,10 @@
|
|||
<data name="Edit.Text" xml:space="preserve">
|
||||
<value>Edit</value>
|
||||
</data>
|
||||
<data name="Retention.Text" xml:space="preserve">
|
||||
<value>Retention (Days):</value>
|
||||
</data>
|
||||
<data name="Retention.HelpText" xml:space="preserve">
|
||||
<value>Number of days of broken urls to retain</value>
|
||||
</data>
|
||||
</root>
|
|
@ -32,6 +32,7 @@ namespace Oqtane.Infrastructure
|
|||
var logRepository = provider.GetRequiredService<ILogRepository>();
|
||||
var visitorRepository = provider.GetRequiredService<IVisitorRepository>();
|
||||
var notificationRepository = provider.GetRequiredService<INotificationRepository>();
|
||||
var urlMappingRepository = provider.GetRequiredService<IUrlMappingRepository>();
|
||||
var installationManager = provider.GetRequiredService<IInstallationManager>();
|
||||
|
||||
// iterate through sites for current tenant
|
||||
|
@ -95,6 +96,22 @@ namespace Oqtane.Infrastructure
|
|||
{
|
||||
log += $"Error Purging Notifications - {ex.Message}<br />";
|
||||
}
|
||||
|
||||
// purge broken urls
|
||||
retention = 30; // 30 days
|
||||
if (settings.ContainsKey("UrlMappingRetention") && !string.IsNullOrEmpty(settings["UrlMappingRetention"]))
|
||||
{
|
||||
retention = int.Parse(settings["UrlMappingRetention"]);
|
||||
}
|
||||
try
|
||||
{
|
||||
count = urlMappingRepository.DeleteUrlMappings(site.SiteId, retention);
|
||||
log += count.ToString() + " Broken Urls Purged<br />";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
log += $"Error Purging Broken Urls - {ex.Message}<br />";
|
||||
}
|
||||
}
|
||||
|
||||
// register assemblies
|
||||
|
|
|
@ -13,5 +13,6 @@ namespace Oqtane.Repository
|
|||
UrlMapping GetUrlMapping(int urlMappingId, bool tracking);
|
||||
UrlMapping GetUrlMapping(int siteId, string url);
|
||||
void DeleteUrlMapping(int urlMappingId);
|
||||
int DeleteUrlMappings(int siteId, int age);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -101,5 +101,24 @@ namespace Oqtane.Repository
|
|||
db.UrlMapping.Remove(urlMapping);
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
public int DeleteUrlMappings(int siteId, int age)
|
||||
{
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
// delete notifications in batches of 100 records
|
||||
var count = 0;
|
||||
var purgedate = DateTime.UtcNow.AddDays(-age);
|
||||
var urlMappings = db.UrlMapping.Where(item => item.SiteId == siteId && string.IsNullOrEmpty(item.MappedUrl) && item.RequestedOn < purgedate)
|
||||
.OrderBy(item => item.RequestedOn).Take(100).ToList();
|
||||
while (urlMappings.Count > 0)
|
||||
{
|
||||
count += urlMappings.Count;
|
||||
db.UrlMapping.RemoveRange(urlMappings);
|
||||
db.SaveChanges();
|
||||
urlMappings = db.UrlMapping.Where(item => item.SiteId == siteId && string.IsNullOrEmpty(item.MappedUrl) && item.RequestedOn < purgedate)
|
||||
.OrderBy(item => item.RequestedOn).Take(100).ToList();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user