Using IContentService within a Hangfire task occasionally throws 'No AmbientContext was found' error
m

Mike Masey

9 months ago
I have a hangfire task that loops through a bunch of data from an API, uses it to either Save, Save & Publish or Unpublish multiple content items. The ContentService itself seesm to be working as expected and is able to update each bit of content correctly, but I'm seeing the following error somewhat sporadically.
prolog
[12:30:03 ERR] Error occurred executing workItem.
System.AggregateException: One or more errors occurred. (No AmbientContext was found.)
 ---> System.InvalidOperationException: No AmbientContext was found.
   at Umbraco.Cms.Infrastructure.Scoping.AmbientScopeContextStack.Pop()
   at Umbraco.Cms.Infrastructure.Scoping.ScopeProvider.PopAmbientScopeContext()
   at Umbraco.Cms.Infrastructure.Scoping.Scope.<>c__DisplayClass56_0.<RobustExit>g__HandleScopeContext|0()
   at Umbraco.Cms.Infrastructure.Scoping.Scope.TryFinally(Action[] actions)
   --- End of inner exception stack trace ---
   at Umbraco.Cms.Infrastructure.Scoping.Scope.TryFinally(Action[] actions)
   at Umbraco.Cms.Infrastructure.Scoping.Scope.RobustExit(Boolean completed, Boolean onException)
   at Umbraco.Cms.Infrastructure.Scoping.Scope.DisposeLastScope()
   at Umbraco.Cms.Infrastructure.Scoping.Scope.Dispose()
   at Umbraco.Cms.Infrastructure.Examine.ExamineUmbracoIndexingHandler.DeferredReIndexForContent.<>c__DisplayClass6_0.<Execute>b__0(CancellationToken cancellationToken)
   at Umbraco.Cms.Infrastructure.HostedServices.QueuedHostedService.BackgroundProcessing(CancellationToken stoppingToken)
This error seems to occur more frequently on the first run, then less so afterwards, although it does still happen. For context, it's currently updated around 20 content items.
how to avoid the `MediaCache` folder getting absolutely massive?
j

jake williamson

about 1 year ago
hey out there, we hit an interesting one today... one of our deployments to a vps running windows and iis locked up for nearly half an hour. when we dug into the logs of the devops pipeline, we noticed hundreds of thousands of entries looking like this:
Deleting file (client-site\umbraco\Data\TEMP\MediaCache\0\0\0\2\d\4\3\c\2574.jpg).
Deleting file (client-site\umbraco\Data\TEMP\MediaCache\0\0\0\2\d\4\3\c\2574.meta).
Deleting directory (client-site\umbraco\Data\TEMP\MediaCache\0\0\0\2\d\4\3\c).
Deleting directory (client-site\umbraco\Data\TEMP\MediaCache\0\0\0\2\d\4\3).
Deleting directory (client-site\umbraco\Data\TEMP\MediaCache\0\0\0\2\d\4).
Deleting directory (client-site\umbraco\Data\TEMP\MediaCache\0\0\0\2\d).
Deleting directory (client-site\umbraco\Data\TEMP\MediaCache\0\0\0\2).
it turns out that at some point a hacking attempt had been run against the server and the
MediaCache
had grown to over 20gb of cache entries. the pipeline was recursively going into each folder and deleting them... we've just taken a look at https://docs.umbraco.com/umbraco-cms/v/13.latest-lts/reference/configuration/imagingsettings and we're going to use
CacheFolder
setting to move the folders/files to another location on disk so they don't get deleteded on a deployment. however we wanted to know more about the
CacheMaxAge
setting:
Specifies how long a processed image may be used from the server cache before it needs to be re-processed again. The default is one year (365 days, formatted as a timespan).
does anyone know if this setting causes umbraco to clean up the files and folders? e.g. if a cache entry is found that's older than the specified timespan it physically removes the files/folders from disk?