We are getting time outs from the variantservice when trying to delete a variant. We want the deletion to happen in the background. The purpose of these variants is only to hold bundled variants which is why we clear all the bundled variants before we put the variants in the deletion queue
foreach (var variant in _variantService.GetByBaseProduct(baseProduct.SystemId))
{
// We should remove the link immediately so that we can keep on working
// with the UI. But the actual deletion of the, no longer needed, variant
// must happen in the background. Litium takes forever when deleting a variant
var variantClone = variant.MakeWritableClone();
variantClone.BundledVariants.Clear();
_variantService.Update(variantClone);
await _variantDeleteQueue.AddToQueueAsync(variant.SystemId);
}
[Service(ServiceType = typeof(IVariantDeleteQueue), Lifetime = DependencyLifetime.Singleton)]
public class VariantDeleteQueue : IVariantDeleteQueue
{
[NotNull] private readonly VariantService _variantService;
[NotNull] private readonly ServiceBusQueue<Guid> _deleteVariantQueue;
public VariantDeleteQueue(ServiceBusFactory serviceBusFactory, VariantService variantService)
{
_variantService = variantService;
_deleteVariantQueue = serviceBusFactory.CreateQueue(new ServiceBusOptions<Guid>("DeleteCustomBundleVariantQueue", OnVariantQueuedForDeletion));
}
private void OnVariantQueuedForDeletion([NotNull] ServiceBusMessage<Guid> arg)
{
try
{
if (arg.Message != null)
{
using (Solution.Instance.SystemToken.Use("Custombundle delete queue"))
{
var variant = _variantService.Get(arg.Message);
if (variant != null)
{
_variantService.Delete(variant);
}
}
}
}
catch (Exception exception)
{
// This is most likely no issue. The case is probably that we have gotten this
// message while we're already working on deleting the variant
this.Log().Error($"Unable to delete variant '{arg.Message}'", exception);
}
}
public async Task AddToQueueAsync(Guid variantSystemId)
{
await _deleteVariantQueue.SendAsync(variantSystemId);
}
}
Litium version: 7