Handled flushing of cache instruction messages when created from handling events in a background thread #11757
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR relates to this issue raised on the Deploy tracker. The problem and cause is well described there, and I've replicated with a load balanced setup and found the same behaviour.
It looks like we need to fix this in CMS rather than Deploy though, as Deploy is raising the expected events following a content deployment operation.
What's going wrong though is that when the events are handled in
DistributedCacheBinder
, we ensure an Umbraco context, using theHttpContext
if available, but creating one if not. For the events triggered from Deploy, we don't have anHttpContext
so one is created. When the events related to cache instructions are handled, theBatchedDatabaseServerMessenger
is used to create a batch of instructions, stored inHttpContext.Items
- which is on our createdHttpContext
.The flushing of the messages though - and the writing of the cache instruction records to the database - happens at the end of an Umbraco request, which will have an
HttpContext
, but a different one to the one we created when handling the events. And hence it doesn't find any messages in theHttpContext.Items
to write.What I've done is to add a dependency to
DistributedCacheBinder
on theBatchedDatabaseServerMessenger
interface,IServerMessenger
. And then called an explicit "flush" operation using the createdHttpContext
. To do that I had to create some method overloads to allow the passing in of anHttpContextBase
instance.This looks to resolve the problem. The only concern I've got really is this additional dependency, and whether from an architecture/semantic point of view the
DistributedCacheBinder
should really have to "know" about flushing server messages. But maybe it's OK, and hopefully there's a clear enough comment about why it seems to be necessary and has been added.To Test (there's a bit of setup needed to prepare this, so not sure if it's necessary to redo or just review what I did):
UmbracoDeploy.OnPrem
web.config
(the custom section, the reference to the Deploy settings, and the API key)config\UmbracoDeploy.config
(referencing one of the live environments from staging)config\UmbracoDeploy.Settings.config
(can be empty).cshtml
file created to both live environments.uda
files created to one of the live environments and run a Deploy extraction