@@ -91,7 +91,10 @@ public async Task<IActionResult> OnPostAsync()
9191 try
9292 {
9393 await CreateNewTemplateVersionAsync ( templateId ) ;
94- InvalidateTemplateCache ( templateId ) ;
94+
95+ await Task . Delay ( 2000 ) ;
96+
97+ await InvalidateTemplateCacheAsync ( templateId ) ;
9598
9699 _logger . LogInformation ( "Successfully created template version {NewVersion} for {TemplateId}" ,
97100 NewVersion , templateId ) ;
@@ -121,14 +124,22 @@ private async Task LoadTemplateDataAsync(string templateId)
121124 {
122125 try
123126 {
127+ _logger . LogDebug ( "Loading template data for {TemplateId}" , templateId ) ;
128+
124129 var apiResponse = await _templatesClient . GetLatestTemplateSchemaAsync ( new Guid ( templateId ) ) ;
125130 CurrentVersionNumber = apiResponse . VersionNumber ;
126131
132+ _logger . LogDebug ( "API returned template version {VersionNumber} for {TemplateId}" ,
133+ CurrentVersionNumber , templateId ) ;
134+
127135 CurrentTemplate = await _formTemplateProvider . GetTemplateAsync ( templateId ) ;
128136 if ( CurrentTemplate != null )
129137 {
130138 var options = new JsonSerializerOptions { WriteIndented = true } ;
131139 CurrentTemplateJson = JsonSerializer . Serialize ( CurrentTemplate , options ) ;
140+
141+ _logger . LogDebug ( "Successfully loaded template {TemplateId} with {TaskGroupCount} task groups" ,
142+ templateId , CurrentTemplate . TaskGroups ? . Count ?? 0 ) ;
132143 }
133144 }
134145 catch ( Exception ex )
@@ -183,20 +194,41 @@ await _templatesClient.CreateTemplateVersionAsync(new Guid(templateId),
183194 new CreateTemplateVersionRequest ( VersionNumber : NewVersion ! , JsonSchema : base64Schema ) ) ;
184195 }
185196
186- private void InvalidateTemplateCache ( string templateId )
197+ private async Task InvalidateTemplateCacheAsync ( string templateId )
187198 {
188199 try
189200 {
190201 var cacheKey = $ "FormTemplate_{ CacheKeyHelper . GenerateHashedCacheKey ( templateId ) } ";
202+ _logger . LogInformation ( "Attempting to invalidate cache for template {TemplateId} with key {CacheKey}" ,
203+ templateId , cacheKey ) ;
204+
191205 _cacheService . Remove ( cacheKey ) ;
192- _logger . LogDebug ( "Invalidated cache for template {TemplateId} with key {CacheKey}" , templateId , cacheKey ) ;
206+ _logger . LogInformation ( "Successfully invalidated cache for template {TemplateId} with key {CacheKey}" ,
207+ templateId , cacheKey ) ;
208+
209+ // Verify the new template version is available by attempting to load it
210+ await VerifyNewTemplateVersionAsync ( templateId ) ;
193211 }
194212 catch ( Exception ex )
195213 {
196214 _logger . LogWarning ( ex , "Failed to invalidate cache for template {TemplateId}" , templateId ) ;
197215 // Don't throw - cache invalidation failure shouldn't break the operation
198216 }
199217 }
218+
219+ private async Task VerifyNewTemplateVersionAsync ( string templateId )
220+ {
221+ try
222+ {
223+ // Try to load the new template version to ensure it's available
224+ var newTemplate = await _formTemplateProvider . GetTemplateAsync ( templateId ) ;
225+ _logger . LogDebug ( "Successfully verified new template version is available for {TemplateId}" , templateId ) ;
226+ }
227+ catch ( Exception ex )
228+ {
229+ _logger . LogWarning ( ex , "Failed to verify new template version for {TemplateId}" , templateId ) ;
230+ }
231+ }
200232
201233 private void HandleApiError ( Exception exception )
202234 {
0 commit comments