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?
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;
}
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
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?
For extra context, this is where a call the tasks
The Order in the argument I get with this method, using the umbraco cache
is working on a reply...