Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

DB Deadlock when adding hangfire job #2152


User avatar
0
shyamjith created

Hi

In my application I have a list of recurring jobs , which are taken from the data base,

What I have done is , Taken the list of recurring job details from database , Iterated it and added to hangfire recurring job, But when I do this my DB is getting locked and application crashes or a timeout exception arises .

Please have a look at my code and help me figure out the problem

public void AddJob()
{

 var schedulerSettings = _schedulerManager.GetSchedulerSettings();
            List<SchedulerSettingViewModel> schedulerModel = schedulerSettings.Select(x => new SchedulerSettingViewModel()
            {
                Id = x.Id,
                MethodName = x.MethodName,
                ScheduledHour = x.ScheduledHour,
                ScheduledMinute = x.ScheduledMinute,
                SchedulerIntervalId = x.SchedulerIntervalId,
                SchedulerName = x.SchedulerName
            }).ToList();
            if (schedulerModel != null && schedulerModel.Count > 0)
            {
                foreach (var item in schedulerModel)
                {
                    if (item != null)
                    {
                          RecurringJob.RemoveIfExists(item.SchedulerName);
                        string jobInterval = "";
                        if (item.SchedulerIntervalId == (int)Enums.SchedulerInterval.Hourly)
                        {
                            jobInterval = Cron.Hourly(item.ScheduledHour);
                        }
                        else
                        {
                            jobInterval = Cron.Daily(item.ScheduledHour, item.ScheduledMinute);
                        }
                        //Type thisType = this.GetType();
                        //MethodInfo theMethod = thisType.GetMethod(item.MethodName);


                        //MethodInfo method = typeof(IBackGroundJobManager).GetMethod(item.MethodName);


                        //RecurringJob.AddOrUpdate<IBackGroundJobManager>(item.SchedulerName, t => method.Invoke(this, null), jobInterval);
                        switch (item.MethodName)
                        {
                            case "Job1":
                                RecurringJob.AddOrUpdate<IBackGroundJobManager>(item.SchedulerName, t => t.Job1(), jobInterval);
                                break;
                            case "Job2":
                                RecurringJob.AddOrUpdate<IBackGroundJobManager>(item.SchedulerName, t => t.Job2(), jobInterval);
                                break;
                            case "Job3":
                                RecurringJob.AddOrUpdate<IBackGroundJobManager>(item.SchedulerName, t => t.Job3(), jobInterval);
                                break;
                            case "Job4":
                                RecurringJob.AddOrUpdate<IBackGroundJobManager>(item.SchedulerName, t => t.Job4(), jobInterval);
                                break;
                            case "Job5":
                                RecurringJob.AddOrUpdate<IBackGroundJobManager>(item.SchedulerName, t => t.Job5(), jobInterval);
                                break;
                            case "Job6":
                                RecurringJob.AddOrUpdate<IBackGroundJobManager>(item.SchedulerName, t => t.Job6(), jobInterval);
                                break;
                        }

                    }
                }
            }

}


  public List<SchedulerSettingModel> GetSchedulerSettings()
        {

            using (_unitOfWorkManager.Begin())
            {
                _unitOfWorkManager.Current.SetTenantId(null);
                var schedulerSettingsList = _schedulerSettingsRepository.GetAllList();

                return schedulerSettingsList.Select(x => new SchedulerSettingModel()
                {

                    Id = x.Id,
                    MethodName = x.MethodName,
                    ScheduledHour = x.ScheduledHour,
                    ScheduledMinute = x.ScheduledMinute,
                    SchedulerIntervalId = x.SchedulerIntervalId,
                    SchedulerName = x.SchedulerName
                }).ToList();
            }

        }

3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Your code seems fine. Does any of your jobs are connecting to an external database ?

  • User Avatar
    0
    shyamjith created

    No Actually . But it is a multi tenant ( Multiple DB) Application

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I don't have any suggestion at the moment. You can try to remove code partially to find which part causes deadlock like Halil suggested here <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/1685">https://github.com/aspnetboilerplate/as ... ssues/1685</a>.

    Maybe we can find something else when you figure out the location of deadlock.