22using Microsoft . Extensions . Options ;
33using Newtonsoft . Json ;
44using Newtonsoft . Json . Linq ;
5+ using Umbraco . Cms . Core ;
56using Umbraco . Cms . Core . Cache ;
67using Umbraco . Cms . Core . Configuration . Models ;
78using Umbraco . Cms . Core . Events ;
@@ -77,6 +78,7 @@ public CacheInstructionService(
7778 public bool IsColdBootRequired ( int lastId )
7879 {
7980 using ICoreScope scope = ScopeProvider . CreateCoreScope ( autoComplete : true ) ;
81+ scope . ReadLock ( Constants . Locks . CacheInstructions ) ;
8082 if ( lastId <= 0 )
8183 {
8284 var count = _cacheInstructionRepository . CountAll ( ) ;
@@ -103,6 +105,7 @@ public bool IsColdBootRequired(int lastId)
103105 public bool IsInstructionCountOverLimit ( int lastId , int limit , out int count )
104106 {
105107 using ICoreScope scope = ScopeProvider . CreateCoreScope ( autoComplete : true ) ;
108+ scope . ReadLock ( Constants . Locks . CacheInstructions ) ;
106109 // Check for how many instructions there are to process, each row contains a count of the number of instructions contained in each
107110 // row so we will sum these numbers to get the actual count.
108111 count = _cacheInstructionRepository . CountPendingInstructions ( lastId ) ;
@@ -113,6 +116,7 @@ public bool IsInstructionCountOverLimit(int lastId, int limit, out int count)
113116 public int GetMaxInstructionId ( )
114117 {
115118 using ICoreScope scope = ScopeProvider . CreateCoreScope ( autoComplete : true ) ;
119+ scope . ReadLock ( Constants . Locks . CacheInstructions ) ;
116120 return _cacheInstructionRepository . GetMaxId ( ) ;
117121 }
118122
@@ -123,6 +127,7 @@ public void DeliverInstructions(IEnumerable<RefreshInstruction> instructions, st
123127
124128 using ( ICoreScope scope = ScopeProvider . CreateCoreScope ( ) )
125129 {
130+ scope . WriteLock ( Constants . Locks . CacheInstructions ) ;
126131 _cacheInstructionRepository . Add ( entity ) ;
127132 scope . Complete ( ) ;
128133 }
@@ -134,6 +139,7 @@ public void DeliverInstructionsInBatches(IEnumerable<RefreshInstruction> instruc
134139 // Write the instructions but only create JSON blobs with a max instruction count equal to MaxProcessingInstructionCount.
135140 using ( ICoreScope scope = ScopeProvider . CreateCoreScope ( ) )
136141 {
142+ scope . WriteLock ( Constants . Locks . CacheInstructions ) ;
137143 foreach ( IEnumerable < RefreshInstruction > instructionsBatch in instructions . InGroupsOf (
138144 _globalSettings . DatabaseServerMessenger . MaxProcessingInstructionCount ) )
139145 {
@@ -157,6 +163,7 @@ public ProcessInstructionsResult ProcessInstructions(
157163 using ( _profilingLogger . DebugDuration < CacheInstructionService > ( "Syncing from database..." ) )
158164 using ( ICoreScope scope = ScopeProvider . CreateCoreScope ( ) )
159165 {
166+ scope . ReadLock ( Constants . Locks . CacheInstructions ) ;
160167 var numberOfInstructionsProcessed = ProcessDatabaseInstructions ( cacheRefreshers , cancellationToken , localIdentity , ref lastId ) ;
161168
162169 // Check for pruning throttling.
@@ -251,6 +258,7 @@ private int ProcessDatabaseInstructions(CacheRefresherCollection cacheRefreshers
251258 List < RefreshInstruction > instructionBatch = GetAllInstructions ( jsonInstructions ) ;
252259
253260 // Process as per-normal.
261+
254262 var success = ProcessDatabaseInstructions ( cacheRefreshers , instructionBatch , instruction , processed , cancellationToken , ref lastId ) ;
255263
256264 // If they couldn't be all processed (i.e. we're shutting down) then exit.
0 commit comments