Base solution for your next web application
Open Closed

I got Tenant error when using hangfire job schedule #8244


User avatar
0
muhittincelik created
public class ScanWinSQLInstanceStagesJob : BackgroundJob<ScanWinSQLInstanceStagesJobArgs>, ITransientDependency
    {
        private readonly IRepository<WindowsHostStage, long> _windowsHostStageRepository;
        private readonly IRepository<WinSQLInstanceStage, long> _winSQLInstanceStageRepository;
        private readonly IObjectMapper _objectMapper;

        public ScanWinSQLInstanceStagesJob(
            IRepository<WindowsHostStage, long> windowsHostStageRepository,
            IRepository<WinSQLInstanceStage, long> winSQLInstanceStageRepository,
            IObjectMapper objectMapper
            )
        {
            _windowsHostStageRepository = windowsHostStageRepository;
            _winSQLInstanceStageRepository = winSQLInstanceStageRepository;
            _objectMapper = objectMapper;
        }

        [UnitOfWork]
        public override void Execute(ScanWinSQLInstanceStagesJobArgs args)
        {
            var windowsHostStage = _windowsHostStageRepository.Get(args.WindowsHostStageId);
            List<GetWMIWinSQLInstanceOutput> winSQLInstances = GetWMI_WinSQLInstances.EnumerateSQLInstances(windowsHostStage.Name);

            foreach (GetWMIWinSQLInstanceOutput winSQLInstance in winSQLInstances)
            {
                var _winSQLInstanceStage = _winSQLInstanceStageRepository.FirstOrDefault(s => s.Hostname == winSQLInstance.Hostname & s.InstanceName == winSQLInstance.InstanceName);

                if (_winSQLInstanceStage != null)
                {
                    _winSQLInstanceStageRepository.Update(_winSQLInstanceStage);
                }
                else
                {
                    _winSQLInstanceStageRepository.Insert(_objectMapper.Map<WinSQLInstanceStage>(winSQLInstance));
                }
            }
        }
    }
[Serializable]
    public class ScanWinSQLInstanceStagesJobArgs
    {
        public long WindowsHostStageId { get; set; }
    }
var windowsHostStage = _windowsHostStageRepository.GetAllList();

            foreach (WindowsHostStage item in windowsHostStage)
            {
                await _backgroundJobManager.EnqueueAsync<ScanWinSQLInstanceStagesJob, ScanWinSQLInstanceStagesJobArgs>(
                new ScanWinSQLInstanceStagesJobArgs
                {
                    WindowsHostStageId = item.Id
                });
            }

I got the following error when insert new record. It can update records.

WARN 2019-12-25 15:20:58,634 [er #1] Hangfire.AutomaticRetryAttribute - Failed to process the job '76': an exception occurred. Retry attempt 4 of 10 will be performed in 00:02:24. Abp.AbpException: Can not set TenantId to 0 for IMustHaveTenant entities!


2 Answer(s)
  • User Avatar
    0
    ryancyq created
    Support Team

    Hi, does your winSqlInstance has IMistHaveTenant interface ? if so you will need to assign tenant_id before inserting it.

  • User Avatar
    0
    muhittincelik created

    Hi,

    I passed TenantId with Job Args and it is ok now.

    await _backgroundJobManager.EnqueueAsync<ScanWinSQLInstanceStagesJob, ScanWinSQLInstanceStagesJobArgs>(
                    new ScanWinSQLInstanceStagesJobArgs
                    {
                        WindowsHostStageId = item.Id,
                        CurrentTenantId = AbpSession.TenantId.GetValueOrDefault()
                    });
    
    if (_winSQLInstanceStage != null)
                    {
                        _winSQLInstanceStageRepository.Update(_winSQLInstanceStage);
                    }
                    else
                    {
                        var _winSQLInstanceStage_Temp = _objectMapper.Map<WinSQLInstanceStage>(winSQLInstance);
                        _winSQLInstanceStage_Temp.TenantId = args.CurrentTenantId;
                        _winSQLInstanceStageRepository.Insert(_winSQLInstanceStage_Temp);
                    }