Copied to clipboard

Flag this post as spam?

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


  • JacobS 7 posts 107 karma points c-trib
    Dec 05, 2022 @ 17:16
    JacobS
    0

    Hangfire Umbraco Context Access

    Followed this guide to update content via hangfire:

    https://cultiv.nl/blog/using-hangfire-to-update-umbraco-content/

    I have hangfire installed and running successfully, unfortunatly I cannot seem to access the umbraco context on a hangfire job, everything I have read indicates you can just inject it in and off you go.

    But doing this:

    public void ManipulateContent(PerformContext context)
    {
        using var _ = _umbracoContextFactory.EnsureUmbracoContext();
        using var serviceScope = _serviceProvider.CreateScope();
    
        var query = serviceScope.ServiceProvider.GetRequiredService<IPublishedContentQuery>();
        var rootNode = query.ContentAtRoot().FirstOrDefault();
    
        if (rootNode == null) return;
    
        context.WriteLine($"Root node - Id: {rootNode.Id} | Name: {rootNode.Name}");
    }
    

    The rootNode is always null. I am a bit stumped, anyone got any tips on direction?

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Dec 06, 2022 @ 10:24
    Huw Reddick
    0

    Hi,

    I don't know if this will, help you, but I use a class which contains my Hangfir Jobs.

    public partial class ScheduledJobs
    {
        private readonly IContentService _contentService;
        private readonly ILogger<ScheduledJobs> _logger;
        private readonly IUmbracoContextFactory _umbracoContextFactory;
    
        public ScheduledJobs(IUmbracoContextFactory umbracoContextFactory, IContentService contentservice, ILogger<ScheduledJobs> logger)
        {
            _contentService = contentservice;
            _umbracoContextFactory = umbracoContextFactory;
            _logger = logger;
        }
    
        [AutomaticRetry(Attempts = 2)]
        [JobDisplayName("Synchronise Products")]
        public void SyncProducts(PerformContext hangfire)
        {
            using (var cref = _umbracoContextFactory.EnsureUmbracoContext())
            {
                //get the Id for the shop
                Guid parentGuid;
                var home = _contentService.GetRootContent().Where(x => x.ContentType.Alias == "home").First();
                var children = _contentService.GetPagedChildren(home.Id, 0, 100, out var totalrec);
                if (children != null)
                {
                    parentGuid = children.First(x => x.ContentType.Alias == "shop").Key;
                }
                else
                {
                    _logger.LogError("Error retreiving Product Parent Page {Alias}", "shop");
                    return;
                }
    
                var shop = _contentService.GetById(parentGuid);
    
               ..... code shortened for example.
            }
        }
    }
    

    The jobs are added using a dashboard and API controller, example below

        public class HangfireJobsController : UmbracoApiController 
        {
            private readonly ILogger<ScheduledJobs.ScheduledJobs> _logger;
            private readonly IContentService _contentService;
            private readonly IUmbracoContextFactory _umbracoContextFactory;
    
            public HangfireJobsController( ILogger<ScheduledJobs.ScheduledJobs> logger, IContentService contentService, IUmbracoContextFactory umbracoContextFactory)
            {
                _logger = logger;
                _contentService = contentService;
                _umbracoContextFactory = umbracoContextFactory;
    
            }
    
            public JsonResult SyncProducts(string Cron = "00 01 * * *")
            {
    
                new ScheduledJobs.ScheduledJobs(_umbracoContextFactory, _contentService, _logger).SyncProducts(null);
                RecurringJob.AddOrUpdate<ScheduledJobs.ScheduledJobs>(x => x.SyncProducts(null), Cron);
    
                return new JsonResult("OK");
    
            }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft