Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tiago Braz 8 posts 89 karma points
    26 days ago
    Tiago Braz
    0

    Umbraco 8 and Hangfire

    Hi,

    I'm migrating a project from 7.X to 8 in order to familiarize myself with this brand new Umbraco version!

    Happens that this project relies on background jobs (using Hangfire) for some recurring tasks, and those tasks need to access IPublishedContent.

    In the previous version faking, an UmbracoContext was somewhat easy with a EnsureUmbracoContext method that would create a context using UmbracoContext.EnsureContext if UmbracoContext.Current was null and it worked pretty well.

    With Umbraco8 and all the changes under the hood I have yet to figure out a way to access/create a UmbracoContext and/or IUmbracoContextAccessor for those cases or event setup a LightInject container within my Hangfire server to with all the dependencies resolved.

    I have been fiddling in Umbraco source code in order to try to figure a way but without success so far.

    Can anybody point me in the right direction?

    Thanks

    P.S.: I know that an easy way to solve this would be to have an API endpoint and make my background job call that endpoint, solving my issue. But I'm not interested in such inelegant solution.

  • Sebastiaan Janssen 4805 posts 13980 karma points MVP admin hq
    26 days ago
    Sebastiaan Janssen
    0

    For the quick-and-dirty solution:

    using Umbraco.Web.Composing; and use Current.UmbracoContext.what_you_need.

    Don't forget to Install-Package UmbracoCms.Web, which is a new NuGet package that we didn't have for v7.

  • Tiago Braz 8 posts 89 karma points
    26 days ago
    Tiago Braz
    0

    Thanks for the quick reply! H5YR!

    I will definitively try that.

    But, will the UmbracoContext be setup in this case? Since there is no HttpRequest associated with my Job?

    Well, going to try that out and will be back with an update.

    Thanks again!

  • Sebastiaan Janssen 4805 posts 13980 karma points MVP admin hq
    26 days ago
    Sebastiaan Janssen
    0

    Yeah.. that is a really good question and I'm not sure of that either. Give it a go and see how far you get.

    For future improvements I'm curious as to what exactly you're trying to access that need a request context, might be able to figure it out with you!

  • Sebastiaan Janssen 4805 posts 13980 karma points MVP admin hq
    26 days ago
    Sebastiaan Janssen
    102

    Might be of interest, just blogged about using Hangfire with Umbraco 8: https://cultiv.nl/blog/using-hangfire-for-scheduled-tasks-in-umbraco/

  • Tiago Braz 8 posts 89 karma points
    22 days ago
    Tiago Braz
    0

    Yes thank you, somebody actually already linked that blog post in our slack. And its exactly what I needed! Marking your answer as the accepted one.

    Cheers!

  • Tiago Braz 8 posts 89 karma points
    18 days ago
    Tiago Braz
    0

    Hey Sebastian,

    So I've been playing with this on my spare time and while I now have hangfire working based on the blog you shared I'm still blocked.

    When in jobs (or registering the jobs) Umbraco.Web.Composing.Current has some of the key elements to null (UmbracoContext, UmbracoHelper, ...) and this is kind of expected, since there is no HttpRequest tied to them.

    But Umbraco.Core.Composing.Current has all the values set, and the Factory returns a lot of instances. But the one I really want is not there.

    I really would like my background job to access IPublishContent, for example, I have a settings DocType/Node with some settings that I need to access so I really would like a way to load that Node from NuCache so I know that I have what is actually published (and not IContent that might have changes that I don't want, since they are not published).

    I probably could use IContent and the versions to find the latest one that is/was published (?) ... but is this the better/only way to do it?

    I believe Umbraco.Core.Composing.Current should have a way to access the an instace of IPublishedCache.

    Thanks

  • Stephen 745 posts 2152 karma points hq
    15 days ago
    Stephen
    0

    Quick answer: there is a non-hackish way to access the front-end cache from within a background task - going to document it soon as I can.

  • Tiago Braz 8 posts 89 karma points
    15 days ago
    Tiago Braz
    0

    Thank you Stephan!

    I'll be waiting for your answer!

  • Stephen 745 posts 2152 karma points hq
    15 days ago
    Stephen
    1

    More details: the UmbracoContext is internally managed by an IUmbracoContextFactory - in most cases, you should not bother about it, but in your case, it's going to help.

    There is, unfortunately, no Current.UmbracoContextFactory at the moment, though we may add it. So you will have to get the factory via:

    var umbracoContextFactory = Current.Factory.GetInstance<IUmbracoContextFactory>();
    

    (that is, assuming you cannot inject it - of course, ideally, you should inject it)

    That factory can give you a reference to an UmbracoContext:

    using (var contextReference = umbracoContextFactory.EnsureUmbracoContext())
    {
        var umbracoContext = contextReference.UmbracoContext;
    }
    

    What happens is: if there already is a "current" UmbracoContext, the reference will point to it. Otherwise, a new one will be created, and is registered as the curent one - until the reference is disposed, and then the context is disposed and is not current anymore.

    Therefore, this is safe to use both within normal requests, and within background tasks.

    You can access the context via the contextReference.UmbracoContext property but, since it's also the current one, Current.UmbracoContext will return it, and anything that requires a context will work.

    Then, you can directly access umbracoContext.ContentCache to retrieve whatever you need.

    Making sense?

  • Tiago Braz 8 posts 89 karma points
    15 days ago
    Tiago Braz
    0

    Yes! This should do the trick!

    I'm going to try it later this week and will give you feedback then!

    Thanks a lot for your help!

  • Tiago Braz 8 posts 89 karma points
    1 week ago
    Tiago Braz
    1

    Works flawlessly! Thank you so much!

Please Sign in or register to post replies

Write your reply to:

Draft