@@ -19,12 +19,19 @@ namespace Umbraco.Core.Sync
19
19
{
20
20
internal class DatabaseServerMessenger : DefaultServerMessenger
21
21
{
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
24
27
//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" )
26
29
: null )
27
30
{
31
+ if ( appContext == null ) throw new ArgumentNullException ( "appContext" ) ;
32
+ if ( options == null ) throw new ArgumentNullException ( "options" ) ;
33
+ _appContext = appContext ;
34
+ _options = options ;
28
35
_lastUtcTicks = DateTime . UtcNow . Ticks ;
29
36
30
37
ReadLastSynced ( ) ;
@@ -39,7 +46,7 @@ internal DatabaseServerMessenger(bool enableDistCalls)
39
46
}
40
47
41
48
private readonly object _lock = new object ( ) ;
42
- private volatile int _lastId = - 1 ;
49
+ private int _lastId = - 1 ;
43
50
private volatile bool _syncing = false ;
44
51
//this ensures that only one thread can possibly check for sync operations every 5 seconds
45
52
private const int SyncTimeFrameSeconds = 5 ;
@@ -83,7 +90,7 @@ protected override void PerformDistributedCall(
83
90
UtcStamp = DateTime . UtcNow ,
84
91
JsonInstruction = JsonConvert . SerializeObject ( instructions , Formatting . None )
85
92
} ;
86
- ApplicationContext . Current . DatabaseContext . Database . Insert ( dto ) ;
93
+ _appContext . DatabaseContext . Database . Insert ( dto ) ;
87
94
}
88
95
89
96
/// <summary>
@@ -99,7 +106,7 @@ internal void FirstSync()
99
106
// cache file.
100
107
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" ) ;
101
108
//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 > (
103
110
"SELECT MAX(id) FROM umbracoCacheInstruction" ) ;
104
111
if ( lastId > 0 )
105
112
{
@@ -137,7 +144,7 @@ internal void Sync()
137
144
. Where < CacheInstructionDto > ( dto => dto . Id > _lastId )
138
145
. OrderBy < CacheInstructionDto > ( dto => dto . Id ) ;
139
146
140
- var list = ApplicationContext . Current . DatabaseContext . Database . Fetch < CacheInstructionDto > ( sql ) ;
147
+ var list = _appContext . DatabaseContext . Database . Fetch < CacheInstructionDto > ( sql ) ;
141
148
142
149
if ( list . Count > 0 )
143
150
{
@@ -157,6 +164,9 @@ internal void Sync()
157
164
SaveLastSynced ( list . Max ( x => x . Id ) ) ;
158
165
}
159
166
167
+ //prune old records
168
+ _appContext . DatabaseContext . Database . Delete < CacheInstructionDto > ( "WHERE utcStamp < @pruneDate" , new { pruneDate = DateTime . UtcNow . AddDays ( _options . DaysToRetainInstructionRecords * - 1 ) } ) ;
169
+
160
170
//reset
161
171
_syncing = false ;
162
172
}
0 commit comments