Пример #1
0
        public MailQueueItem GetItem()
        {
            try
            {
                while (true)
                {
                    if (!_noTasks)
                    {
                        _log.Info("Getting new Item...");
                    }

                    MailBox mbox = null;
                    var     locked_in_this_thread = false;
                    lock (SyncObject)
                    {
                        if (!_isGetMailboxRunning)
                        {
                            _isGetMailboxRunning  = true;
                            locked_in_this_thread = true;
                        }
                    }
                    try
                    {
                        if (locked_in_this_thread && _isGetMailboxRunning)
                        {
                            mbox = _manager.GetMailboxForProcessing(_tasksConfig);
                        }
                    }
                    finally
                    {
                        if (locked_in_this_thread && _isGetMailboxRunning)
                        {
                            _isGetMailboxRunning = false;
                        }
                    }


                    if (mbox == null)
                    {
                        if (!_noTasks)
                        {
                            _log.Info("Nothing to do.");
                        }
                        _noTasks = true;
                        break;
                    }

                    var absence = false;
                    var type    = HttpRuntime.Cache.Get(mbox.TenantId.ToString(CultureInfo.InvariantCulture));
                    if (type == null)
                    {
                        _log.Info("Tenant {0} isn't in cache", mbox.TenantId);
                        absence = true;
                        try
                        {
                            type = _manager.GetTariffType(mbox.TenantId);
                        }
                        catch (Exception e)
                        {
                            _log.Error("Collector.GetItem() -> GetTariffType Exception: {0}", e.ToString());
                            type = MailBoxManager.TariffType.Active;
                        }
                    }
                    else
                    {
                        _log.Info("Tenant {0} is in cache", mbox.TenantId);
                    }

                    _noTasks = false;
                    _log.Info("MailboxId: {0} is processing. EMail: '{1}'  User: '******' TenantId: {3} ",
                              mbox.MailBoxId,
                              mbox.EMail.Address,
                              mbox.UserId,
                              mbox.TenantId);

                    switch ((MailBoxManager.TariffType)type)
                    {
                    case MailBoxManager.TariffType.LongDead:
                        _log.Info("Tenant {0} is not paid. Disable mailboxes.", mbox.TenantId);
                        _manager.DisableMailboxesForTenant(mbox.TenantId);
                        break;

                    case MailBoxManager.TariffType.Overdue:
                        _log.Info("Tenant {0} is not paid. Stop processing mailboxes.", mbox.TenantId);
                        _manager.SetNextLoginDelayedForTenant(mbox.TenantId, _settings.OverdueAccountDelay);
                        break;

                    default:
                        var user_terminated = false;
                        try
                        {
                            CoreContext.TenantManager.SetCurrentTenant(mbox.TenantId);
                            var user = CoreContext.UserManager.GetUsers(new Guid(mbox.UserId));
                            if (user.Status == EmployeeStatus.Terminated)
                            {
                                user_terminated = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            _log.Error(ex, "Cannot get current user = '******'", mbox.UserId);
                        }

                        if (user_terminated)
                        {
                            _log.Info("User '{0}' was terminated. TenantId: {1}. Stop processing mailboxes.", mbox.UserId, mbox.TenantId);
                            _manager.DisableMailboxesForUser(mbox.TenantId, mbox.UserId);
                            return(null);
                        }

                        if (absence)
                        {
                            var mboxKey = mbox.TenantId.ToString(CultureInfo.InvariantCulture);
                            HttpRuntime.Cache.Remove(mboxKey);
                            HttpRuntime.Cache.Insert(mboxKey, type, null,
                                                     DateTime.UtcNow.Add(_settings.TenantCachingPeriod), Cache.NoSlidingExpiration);
                        }
                        _log.Debug("CreateItemForAccount()...");
                        return(MailItemQueueFactory.CreateItemForAccount(mbox, _itemManger));
                    }
                }
            }
            catch (Exception e)
            {
                _log.Error("Collector.GetItem() Exception: {0}", e.ToString());
            }

            return(null);
        }
Пример #2
0
        public MailQueueItem GetItem()
        {
            MailQueueItem result = null;

            try
            {
                while (true)
                {
                    if (!_noTasks)
                    {
                        _log.Debug("Getting new Item...");
                    }

                    MailBox mbox = null;
                    var     locked_in_this_thread = false;
                    lock (SyncObject)
                    {
                        if (!_isGetMailboxRunning)
                        {
                            _isGetMailboxRunning  = true;
                            locked_in_this_thread = true;
                        }
                    }
                    try
                    {
                        if (locked_in_this_thread && _isGetMailboxRunning)
                        {
                            mbox = _manager.GetMailboxForProcessing(_settings.ActivityTimeout);
                        }
                    }
                    finally
                    {
                        if (locked_in_this_thread && _isGetMailboxRunning)
                        {
                            _isGetMailboxRunning = false;
                        }
                    }


                    if (mbox == null)
                    {
                        if (!_noTasks)
                        {
                            _log.Debug("Nothing to do.");
                        }
                        _noTasks = true;
                        break;
                    }

                    var absence = false;
                    var type    = HttpRuntime.Cache.Get(mbox.TenantId.ToString(CultureInfo.InvariantCulture));
                    if (type == null)
                    {
                        absence = true;
                        try
                        {
                            type = _manager.GetTariffType(mbox.TenantId);
                        }
                        catch (Exception e)
                        {
                            _log.Error("Collector.GetItem() -> GetTariffType Exception: {0}", e.ToString());
                            type = MailBoxManager.TariffType.Active;
                        }
                    }
                    else if (mbox.Active && (MailBoxManager.TariffType)type != MailBoxManager.TariffType.Active)
                    {
                        HttpRuntime.Cache.Remove(mbox.TenantId.ToString(CultureInfo.InvariantCulture));
                        absence = true;
                        type    = MailBoxManager.TariffType.Active;
                    }

                    TimeSpan delay;
                    switch ((MailBoxManager.TariffType)type)
                    {
                    case MailBoxManager.TariffType.LongDead:
                        delay = _settings.LongDeadAccountDelay;
                        break;

                    case MailBoxManager.TariffType.Overdue:
                        delay = _settings.OverdueAccountDelay;
                        break;

                    default:
                        delay = TimeSpan.FromDays(1);
                        break;
                    }
                    if (absence)
                    {
                        HttpRuntime.Cache.Insert(mbox.TenantId.ToString(CultureInfo.InvariantCulture), type, null,
                                                 DateTime.UtcNow.Add(delay), Cache.NoSlidingExpiration);
                    }

                    _noTasks = false;
                    _log.Info("MailboxId: {0} is being processed. EMail: '{1}'  User: '******' TenanntId: {3} ",
                              mbox.MailBoxId,
                              mbox.EMail.Address,
                              mbox.UserId,
                              mbox.TenantId);

                    if ((MailBoxManager.TariffType)type == MailBoxManager.TariffType.LongDead ||
                        (MailBoxManager.TariffType)type == MailBoxManager.TariffType.Overdue)
                    {
                        _log.Info("Tenant {0} is not paid. Stop processing MailboxId: {1} EMail: '{2}'.",
                                  mbox.TenantId,
                                  mbox.MailBoxId,
                                  mbox.EMail.Address);

                        _manager.SetNextLoginDelayedFor(mbox, delay);
                    }
                    else
                    {
                        _log.Debug("CreateItemForAccount()...");
                        result = MailItemQueueFactory.CreateItemForAccount(mbox, _itemManger);
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                _log.Error("Collector.GetItem() Exception: {0}", e.ToString());
            }

            return(result);
        }