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 NavigationManager NavigationManager
|
||||||
@inject IUrlMappingService UrlMappingService
|
@inject IUrlMappingService UrlMappingService
|
||||||
@inject ISiteService SiteService
|
@inject ISiteService SiteService
|
||||||
|
@inject ISettingService SettingService
|
||||||
@inject IStringLocalizer<Index> Localizer
|
@inject IStringLocalizer<Index> Localizer
|
||||||
@inject IStringLocalizer<SharedResources> SharedLocalizer
|
@inject IStringLocalizer<SharedResources> SharedLocalizer
|
||||||
|
|
||||||
|
@ -62,6 +63,12 @@ else
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</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>
|
</div>
|
||||||
<br />
|
<br />
|
||||||
<button type="button" class="btn btn-success" @onclick="SaveSiteSettings">@SharedLocalizer["Save"]</button>
|
<button type="button" class="btn btn-success" @onclick="SaveSiteSettings">@SharedLocalizer["Save"]</button>
|
||||||
|
@ -73,6 +80,7 @@ else
|
||||||
private bool _mapped = true;
|
private bool _mapped = true;
|
||||||
private List<UrlMapping> _urlMappings;
|
private List<UrlMapping> _urlMappings;
|
||||||
private string _capturebrokenurls;
|
private string _capturebrokenurls;
|
||||||
|
private int _retention = 30;
|
||||||
|
|
||||||
public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Admin;
|
public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Admin;
|
||||||
|
|
||||||
|
@ -80,6 +88,9 @@ else
|
||||||
{
|
{
|
||||||
await GetUrlMappings();
|
await GetUrlMappings();
|
||||||
_capturebrokenurls = PageState.Site.CaptureBrokenUrls.ToString();
|
_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)
|
private async void MappedChanged(ChangeEventArgs e)
|
||||||
|
@ -124,6 +135,11 @@ else
|
||||||
var site = PageState.Site;
|
var site = PageState.Site;
|
||||||
site.CaptureBrokenUrls = bool.Parse(_capturebrokenurls);
|
site.CaptureBrokenUrls = bool.Parse(_capturebrokenurls);
|
||||||
await SiteService.UpdateSiteAsync(site);
|
await SiteService.UpdateSiteAsync(site);
|
||||||
|
|
||||||
|
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);
|
AddModuleMessage(Localizer["Success.SaveSiteSettings"], MessageType.Success);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
|
@ -162,4 +162,10 @@
|
||||||
<data name="Edit.Text" xml:space="preserve">
|
<data name="Edit.Text" xml:space="preserve">
|
||||||
<value>Edit</value>
|
<value>Edit</value>
|
||||||
</data>
|
</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>
|
</root>
|
|
@ -32,6 +32,7 @@ namespace Oqtane.Infrastructure
|
||||||
var logRepository = provider.GetRequiredService<ILogRepository>();
|
var logRepository = provider.GetRequiredService<ILogRepository>();
|
||||||
var visitorRepository = provider.GetRequiredService<IVisitorRepository>();
|
var visitorRepository = provider.GetRequiredService<IVisitorRepository>();
|
||||||
var notificationRepository = provider.GetRequiredService<INotificationRepository>();
|
var notificationRepository = provider.GetRequiredService<INotificationRepository>();
|
||||||
|
var urlMappingRepository = provider.GetRequiredService<IUrlMappingRepository>();
|
||||||
var installationManager = provider.GetRequiredService<IInstallationManager>();
|
var installationManager = provider.GetRequiredService<IInstallationManager>();
|
||||||
|
|
||||||
// iterate through sites for current tenant
|
// iterate through sites for current tenant
|
||||||
|
@ -95,6 +96,22 @@ namespace Oqtane.Infrastructure
|
||||||
{
|
{
|
||||||
log += $"Error Purging Notifications - {ex.Message}<br />";
|
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
|
// register assemblies
|
||||||
|
|
|
@ -13,5 +13,6 @@ namespace Oqtane.Repository
|
||||||
UrlMapping GetUrlMapping(int urlMappingId, bool tracking);
|
UrlMapping GetUrlMapping(int urlMappingId, bool tracking);
|
||||||
UrlMapping GetUrlMapping(int siteId, string url);
|
UrlMapping GetUrlMapping(int siteId, string url);
|
||||||
void DeleteUrlMapping(int urlMappingId);
|
void DeleteUrlMapping(int urlMappingId);
|
||||||
|
int DeleteUrlMappings(int siteId, int age);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -101,5 +101,24 @@ namespace Oqtane.Repository
|
||||||
db.UrlMapping.Remove(urlMapping);
|
db.UrlMapping.Remove(urlMapping);
|
||||||
db.SaveChanges();
|
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