Open Closed

Issue face while use background job? #10486


0
SRTMDEV created

Prerequisites

  • What is your product version?
    • 8.6.0
  • What is your product type (Angular or MVC)?
    • Angular
  • What is product framework type (.net framework or .net core)?
    • .NET Core

Hello Team, We have configured Hangfire for run background job. Now we face multiple issue while running background Job: 1) Exception: "Execution Timeout Expired..." 2) Exception: "This is usually caused by different threads using the same instance of DbContext..." 3) While updating to table, LastModifierUserId user getting blank.

My requirment is I want to execute code in background and if any exception occured, have to update status in table and also send mail in background because as per current ABP architecture it getting rollback everything.

Currently I have written code as per below and face issue as per describe above:

[AbpAllowAnonymous]
public async Task<string> MyFunction(MyModel myModel)
{
        using (var uow = _unitOfWorkManager.Begin())
        {
            using (_unitOfWorkManager.Current.SetTenantId(myModel.TenantId))
            {
                try
                {
                    _unitOfWorkManager.Current.SaveChanges();
                }
                catch(Exception ex)
                {
                }
                finally
                {
                    await UpdateStatus();
                    BackgroundJob.Enqueue<MyRepoAppService>(t => t.SendMail(myModel));
                }
                uow.Complete();
            }
        }
}

private async Task UpdateStatus(bool IsFail)
{
            myModel.IsFail = IsFail;
            await myRepo.UpdateAsync(myModel);
}

public async Task SendMail(MyModel myModel)
{
    using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
    {
        using (_unitOfWorkManager.Current.SetTenantId(myModel.TenantId))
        {
            .
            .
            .
            Send Mail...
        }
        uow.Complete();
    }
}

Thanks, JJ


1 Answer(s)
  • 0
    ismcagdas created
    Support Team

    Hi,

    Could you try using async version of Methods in youyr MyFunction method ? As I can see,

    _unitOfWorkManager.Current.SaveChanges(); and BackgroundJob.Enqueue has async versions. If you mix sync and async code, such errors might occurd randomly.