Copied to clipboard

Flag this post as spam?

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


  • Garret 69 posts 309 karma points
    Mar 06, 2024 @ 18:51
    Garret
    0

    Hangfire task sometimes doesn't return content (caching, messaging issue?)

    Hi all,

    I'm using Hangfire to sent an e-mail as a task from a controller after I created a new contentitem. I call the task with the id of the content item. But on live environment the content sometimes isn't returned for some reason

    Here's the complete code of the task

    public async Task RunTask(PerformContext context, int orderId)
    {
        context.WriteLine("Started with orderId:{0}", orderId);
    
    try
    {
        using (UmbracoContextReference umbracoContentReference = _umbracoContextFactory.EnsureUmbracoContext())
        {
            IUmbracoContext umbracoContext = umbracoContentReference.UmbracoContext;
    
            if(umbracoContext == null)
            {
                context.WriteLine("umbracoContext is null");
                return;
            }
    
            IPublishedContent orderNode = umbracoContext.Content.GetById(orderId);
            if(orderNode == null)
            {
                context.WriteLine("orderNode is null");
                return;
            }
    
            Order order = new Order(orderNode, _publishedValueFallback);
            context.WriteLine("Order found:{0}", order.Id);
    
            if (order.OrdermailSent == DateTime.MinValue || order.Name.StartsWith("D"))
            {
                //are these all necessary 
                System.Globalization.CultureInfo.DefaultThreadCurrentCulture = new System.Globalization.CultureInfo(order.Culture);
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(order.Culture);
                _variationContextAccessor.VariationContext = new VariationContext(order.Culture);
    
                HomePage homePage = new HomePage(umbracoContext.Content.GetAtRoot().FirstOrDefault(), _publishedValueFallback);
                string subject = _dictionaryHelper.GetDictionaryItem("OrderMail_Subject", order.Culture);
    
                context.WriteLine("ordermail should be sent on {0}", DateTime.Now);
                if (await _mailService.SentOrderMail(order, homePage, subject))
                {
                    context.WriteLine("ordermail sent successfully");
                    var content = _contentService.GetById(order.Id);
                    content.SetValue(Order.GetModelPropertyType(_publishedSnapshotAccessor, o => o.OrdermailSent).Alias, DateTime.Now);
                    _contentService.SaveAndPublish(content);
                }
            }
        };
        context.WriteLine("Ended");
    }
    catch (Exception e)
    {
        _logger.Error(e, "Index | Exception: {0} | Message: {1} | Stack Trace: {2}", e.InnerException != null ? e.InnerException.ToString() : "", e.Message != null ? e.Message.ToString() : "", e.StackTrace);
        context.WriteLine("Index | Exception: {0} | Message: {1} | Stack Trace: {2}", e.InnerException != null ? e.InnerException.ToString() : "", e.Message != null ? e.Message.ToString() : "", e.StackTrace);
    }
    }
    

    I've read the forum on almost all topics related to hangfire. The only thing I can think of is that the cache isn't updated yet and therefor doesn't find anything when I call umbracoContext.Content.GetById(orderId);

    Anyone an idea or can point my in the right direction?

  • Garret 69 posts 309 karma points
    Mar 07, 2024 @ 10:32
    Garret
    0

    For extra context, this is where a call the tasks

        private IActionResult ProcessOrder(Order order)
        {
            //ordermail versturen 
            BackgroundJob.Schedule<SentOrderMailTask>(o => o.RunTask(null, order.Id), TimeSpan.FromSeconds(1));
    
            //inschieten naar sendcloud
            BackgroundJob.Schedule<SentToSendCloud>(o => o.RunTask(null, order.Id), TimeSpan.FromSeconds(2));
            //BackgroundJob.Enqueue<SentToSendCloud>(o => o.RunTask(null, order.Id));
    
            Funnel funnel = _funnelHelper.GetFunnelFromSession();
            _funnelHelper.ResetFunnel(funnel);
    
            return Redirect(_cultureHelper.SummaryPage.Url());
        }
    

    The Order in the argument I get with this method, using the umbraco cache

    public Order GetOrderFromSession()
    {
        int? orderId = _httpContextAccessor.HttpContext.Session.GetInt32("orderId");
        if (orderId.HasValue)
        {
            var orderContent = _umbracoHelper.Content(orderId.Value);
            if (orderContent != null)
                return new Order(orderContent, _publishedValueFallback);
        }
        return null;
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft