Skip to content

Commit c7f8d42

Browse files
committed
Adds auto-pruning to db table but that is configurable with options
1 parent eed492f commit c7f8d42

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

src/Umbraco.Core/Sync/DatabaseServerMessenger.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,19 @@ namespace Umbraco.Core.Sync
1919
{
2020
internal class DatabaseServerMessenger : DefaultServerMessenger
2121
{
22-
internal DatabaseServerMessenger(bool enableDistCalls)
23-
: base(() => enableDistCalls
22+
private readonly ApplicationContext _appContext;
23+
private readonly DatabaseServerMessengerOptions _options;
24+
25+
public DatabaseServerMessenger(ApplicationContext appContext, bool enableDistCalls, DatabaseServerMessengerOptions options)
26+
: base(() => enableDistCalls
2427
//This is simply to ensure that dist calls gets enabled on the base messenger - a bit of a hack but works
25-
? new Tuple<string, string>("empty", "empty")
28+
? new Tuple<string, string>("empty", "empty")
2629
: null)
2730
{
31+
if (appContext == null) throw new ArgumentNullException("appContext");
32+
if (options == null) throw new ArgumentNullException("options");
33+
_appContext = appContext;
34+
_options = options;
2835
_lastUtcTicks = DateTime.UtcNow.Ticks;
2936

3037
ReadLastSynced();
@@ -39,7 +46,7 @@ internal DatabaseServerMessenger(bool enableDistCalls)
3946
}
4047

4148
private readonly object _lock = new object();
42-
private volatile int _lastId = -1;
49+
private int _lastId = -1;
4350
private volatile bool _syncing = false;
4451
//this ensures that only one thread can possibly check for sync operations every 5 seconds
4552
private const int SyncTimeFrameSeconds = 5;
@@ -83,7 +90,7 @@ protected override void PerformDistributedCall(
8390
UtcStamp = DateTime.UtcNow,
8491
JsonInstruction = JsonConvert.SerializeObject(instructions, Formatting.None)
8592
};
86-
ApplicationContext.Current.DatabaseContext.Database.Insert(dto);
93+
_appContext.DatabaseContext.Database.Insert(dto);
8794
}
8895

8996
/// <summary>
@@ -99,7 +106,7 @@ internal void FirstSync()
99106
// cache file.
100107
LogHelper.Warn<DatabaseServerMessenger>("No last synced Id found, this generally means this is a new server/install. The server will adjust it's last synced id to the latest found in the database and will start maintaining cache updates based on that id");
101108
//go get the last id in the db and store it
102-
var lastId = ApplicationContext.Current.DatabaseContext.Database.ExecuteScalar<int>(
109+
var lastId = _appContext.DatabaseContext.Database.ExecuteScalar<int>(
103110
"SELECT MAX(id) FROM umbracoCacheInstruction");
104111
if (lastId > 0)
105112
{
@@ -137,7 +144,7 @@ internal void Sync()
137144
.Where<CacheInstructionDto>(dto => dto.Id > _lastId)
138145
.OrderBy<CacheInstructionDto>(dto => dto.Id);
139146

140-
var list = ApplicationContext.Current.DatabaseContext.Database.Fetch<CacheInstructionDto>(sql);
147+
var list = _appContext.DatabaseContext.Database.Fetch<CacheInstructionDto>(sql);
141148

142149
if (list.Count > 0)
143150
{
@@ -157,6 +164,9 @@ internal void Sync()
157164
SaveLastSynced(list.Max(x => x.Id));
158165
}
159166

167+
//prune old records
168+
_appContext.DatabaseContext.Database.Delete<CacheInstructionDto>("WHERE utcStamp < @pruneDate", new {pruneDate = DateTime.UtcNow.AddDays(_options.DaysToRetainInstructionRecords*-1)});
169+
160170
//reset
161171
_syncing = false;
162172
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Umbraco.Core.Sync
2+
{
3+
internal class DatabaseServerMessengerOptions
4+
{
5+
public DatabaseServerMessengerOptions()
6+
{
7+
DaysToRetainInstructionRecords = 100;
8+
}
9+
10+
public int DaysToRetainInstructionRecords { get; set; }
11+
}
12+
}

src/Umbraco.Core/Umbraco.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,7 @@
11021102
<Compile Include="Strings\Diff.cs" />
11031103
<Compile Include="Sync\CurrentServerEnvironmentStatus.cs" />
11041104
<Compile Include="Sync\DatabaseServerMessenger.cs" />
1105+
<Compile Include="Sync\DatabaseServerMessengerOptions.cs" />
11051106
<Compile Include="Sync\DistributedMessage.cs" />
11061107
<Compile Include="Sync\RefreshInstruction.cs" />
11071108
<Compile Include="Sync\ServerEnvironmentHelper.cs" />

src/Umbraco.Web/BatchedDatabaseServerMessenger.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ namespace Umbraco.Web
1313
{
1414
internal class BatchedDatabaseServerMessenger : DatabaseServerMessenger
1515
{
16-
internal BatchedDatabaseServerMessenger(bool enableDistCalls) : base(enableDistCalls)
16+
public BatchedDatabaseServerMessenger(ApplicationContext appContext, bool enableDistCalls, DatabaseServerMessengerOptions options)
17+
: base(appContext, enableDistCalls, options)
1718
{
1819
UmbracoModule.EndRequest += UmbracoModule_EndRequest;
1920
UmbracoModule.RouteAttempt += UmbracoModule_RouteAttempt;

src/Umbraco.Web/WebBootManager.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,10 @@ protected override void InitializeResolvers()
321321
DefaultRenderMvcControllerResolver.Current = new DefaultRenderMvcControllerResolver(typeof(RenderMvcController));
322322

323323
//Override the ServerMessengerResolver to set a username/password for the distributed calls
324-
ServerMessengerResolver.Current.SetServerMessenger(new BatchedDatabaseServerMessenger(UmbracoConfig.For.UmbracoSettings().DistributedCall.Enabled));
324+
ServerMessengerResolver.Current.SetServerMessenger(new BatchedDatabaseServerMessenger(
325+
ApplicationContext,
326+
UmbracoConfig.For.UmbracoSettings().DistributedCall.Enabled,
327+
new DatabaseServerMessengerOptions()));
325328

326329
//ServerMessengerResolver.Current.SetServerMessenger(new DatabaseServerMessenger(UmbracoConfig.For.UmbracoSettings().DistributedCall.Enabled));
327330
//ServerMessengerResolver.Current.SetServerMessenger(new BatchedServerMessenger(() =>

0 commit comments

Comments
 (0)