mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Implementing trimming background task.
This commit is contained in:
@@ -24,12 +24,12 @@ namespace Orchard.AuditTrail.Drivers {
|
||||
|
||||
return ContentShape("Parts_AuditTrailTrimmingSettings_Edit", () => {
|
||||
var viewModel = new AuditTrailTrimmingSettingsViewModel {
|
||||
AutoTrimThreshold = part.AutoTrimThreshold,
|
||||
Threshold = part.Threshold,
|
||||
};
|
||||
|
||||
if (updater != null) {
|
||||
if (updater.TryUpdateModel(viewModel, Prefix, null, null)) {
|
||||
part.AutoTrimThreshold = viewModel.AutoTrimThreshold;
|
||||
part.Threshold = viewModel.Threshold;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,13 +1,22 @@
|
||||
using Orchard.ContentManagement;
|
||||
using System;
|
||||
using Orchard.ContentManagement;
|
||||
|
||||
namespace Orchard.AuditTrail.Models {
|
||||
public class AuditTrailTrimmingSettingsPart : ContentPart {
|
||||
/// <summary>
|
||||
/// Threshold in days.
|
||||
/// </summary>
|
||||
public int AutoTrimThreshold {
|
||||
get { return this.Retrieve(x => x.AutoTrimThreshold, defaultValue: 10); }
|
||||
set { this.Store(x => x.AutoTrimThreshold, value); }
|
||||
public int Threshold {
|
||||
get { return this.Retrieve(x => x.Threshold, defaultValue: 10); }
|
||||
set { this.Store(x => x.Threshold, value); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The timestamp the audit trail was last trimmed.
|
||||
/// </summary>
|
||||
public DateTime? LastRunUtc {
|
||||
get { return this.Retrieve(x => x.LastRunUtc); }
|
||||
set { this.Store(x => x.LastRunUtc, value); }
|
||||
}
|
||||
}
|
||||
}
|
@@ -154,6 +154,7 @@
|
||||
<Compile Include="Providers\Content\DiffNode.cs" />
|
||||
<Compile Include="Providers\Content\DiffType.cs" />
|
||||
<Compile Include="Providers\Content\IDiffGramAnalyzer.cs" />
|
||||
<Compile Include="Services\AuditTrailTrimmingBackgroundTask.cs" />
|
||||
<Compile Include="ViewModels\AuditTrailCategorySettingsViewModel.cs" />
|
||||
<Compile Include="ViewModels\AuditTrailEventSettingsViewModel.cs" />
|
||||
<Compile Include="ViewModels\AuditTrailTrimmingSettingsViewModel.cs" />
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using Orchard.AuditTrail.Helpers;
|
||||
using Orchard.AuditTrail.Models;
|
||||
using Orchard.Caching;
|
||||
@@ -152,5 +153,17 @@ namespace Orchard.AuditTrail.Services {
|
||||
|
||||
return eventDescriptors.First();
|
||||
}
|
||||
|
||||
public IEnumerable<AuditTrailEventRecord> Trim(TimeSpan threshold) {
|
||||
var dateThreshold = _clock.UtcNow.Date - threshold;
|
||||
var query = _auditTrailRepository.Table.Where(x => x.CreatedUtc < dateThreshold);
|
||||
var records = query.ToArray();
|
||||
|
||||
foreach (var record in records) {
|
||||
_auditTrailRepository.Delete(record);
|
||||
}
|
||||
|
||||
return records;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Orchard.AuditTrail.Models;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Environment.Extensions;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Services;
|
||||
using Orchard.Settings;
|
||||
using Orchard.Tasks;
|
||||
|
||||
namespace Orchard.AuditTrail.Services {
|
||||
[OrchardFeature("Orchard.AuditTrail.Trimming")]
|
||||
public class AuditTrailTrimmingBackgroundTask : Component, IBackgroundTask {
|
||||
private static readonly object _sweepLock = new object();
|
||||
private readonly ISiteService _siteService;
|
||||
private readonly IClock _clock;
|
||||
private readonly IAuditTrailManager _auditTrailManager;
|
||||
|
||||
public AuditTrailTrimmingBackgroundTask(ISiteService siteService, IClock clock, IAuditTrailManager auditTrailManager) {
|
||||
_siteService = siteService;
|
||||
_clock = clock;
|
||||
_auditTrailManager = auditTrailManager;
|
||||
}
|
||||
|
||||
public AuditTrailTrimmingSettingsPart Settings {
|
||||
get { return _siteService.GetSiteSettings().As<AuditTrailTrimmingSettingsPart>(); }
|
||||
}
|
||||
|
||||
public void Sweep() {
|
||||
if (Monitor.TryEnter(_sweepLock)) {
|
||||
try {
|
||||
Logger.Debug("Beginning sweep.");
|
||||
|
||||
// We don't need to check the audit trail for events to remove every minute. Let's stick with twice a day.
|
||||
if (!TimeToTrim())
|
||||
return;
|
||||
|
||||
Logger.Debug("Starting audit trail trimming operation.");
|
||||
var deletedRecords = _auditTrailManager.Trim(TimeSpan.FromDays(Settings.Threshold));
|
||||
Logger.Debug("Audit trail trimming operation completed. {0} Records were deleted.", deletedRecords.Count());
|
||||
Settings.LastRunUtc = _clock.UtcNow;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Logger.Error(ex, "Error during sweep.");
|
||||
}
|
||||
finally {
|
||||
Monitor.Exit(_sweepLock);
|
||||
Logger.Debug("Ending sweep.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool TimeToTrim() {
|
||||
var lastRun = Settings.LastRunUtc ?? DateTime.MinValue;
|
||||
var now = _clock.UtcNow;
|
||||
var interval = TimeSpan.FromHours(12);
|
||||
return now - lastRun > interval;
|
||||
}
|
||||
}
|
||||
}
|
@@ -56,5 +56,11 @@ namespace Orchard.AuditTrail.Services {
|
||||
/// <param name="fullyQualifiedEventName">The fully qualified event name to describe.</param>
|
||||
/// <returns>Returns a single audit trail event descriptor.</returns>
|
||||
AuditTrailEventDescriptor Describe(string fullyQualifiedEventName);
|
||||
|
||||
/// <summary>
|
||||
/// Trims the audit trail by deleting all records older than the specified threshold.
|
||||
/// </summary>
|
||||
/// <returns>Returns the deleted records.</returns>
|
||||
IEnumerable<AuditTrailEventRecord> Trim(TimeSpan threshold);
|
||||
}
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
namespace Orchard.AuditTrail.ViewModels {
|
||||
public class AuditTrailTrimmingSettingsViewModel {
|
||||
public int AutoTrimThreshold { get; set; }
|
||||
public int Threshold { get; set; }
|
||||
}
|
||||
}
|
@@ -2,8 +2,8 @@
|
||||
<fieldset>
|
||||
<legend>@T("Auto Trim Settings")</legend>
|
||||
<div id="auditTrailAutoTrimthresholdSetting">
|
||||
@Html.LabelFor(m => m.AutoTrimThreshold, T("Auto-trim threshold"))
|
||||
@Html.TextBoxFor(m => m.AutoTrimThreshold, new { @class = "text small" })
|
||||
@Html.LabelFor(m => m.Threshold, T("Auto-trim threshold"))
|
||||
@Html.TextBoxFor(m => m.Threshold, new { @class = "text small" })
|
||||
<span class="hint">@T("Specify the number of days of audit log data to retain.")</span>
|
||||
</div>
|
||||
</fieldset>
|
Reference in New Issue
Block a user