예제 #1
0
        private bool TryLockMailbox(MailBox mailbox)
        {
            _log.Debug("TryLockMailbox(MailboxId={0} is {1})", mailbox.MailBoxId, mailbox.Active ? "active" : "inactive");

            try
            {
                bool contains;
                lock (_locker)
                {
                    contains = _tenantMemCache.Contains(mailbox.TenantId.ToString(CultureInfo.InvariantCulture));
                }
                if (!contains)
                {
                    _log.Debug("Tenant {0} isn't in cache", mailbox.TenantId);
                    try
                    {
                        var type = mailbox.GetTenantStatus(_tasksConfig.TenantOverdueDays, _log);

                        switch (type)
                        {
                        case Defines.TariffType.LongDead:
                            _log.Info("Tenant {0} is not paid. Disable mailboxes.", mailbox.TenantId);
                            _manager.DisableMailboxesForTenant(mailbox.TenantId);
                            RemoveFromQueue(mailbox.TenantId);
                            return(false);

                        case Defines.TariffType.Overdue:
                            _log.Info("Tenant {0} is not paid. Stop processing mailboxes.", mailbox.TenantId);
                            _manager.SetNextLoginDelayedForTenant(mailbox.TenantId, _tasksConfig.OverdueAccountDelay);
                            RemoveFromQueue(mailbox.TenantId);
                            return(false);

                        default:
                            _log.Info("Tenant {0} is paid.", mailbox.TenantId);

                            if (mailbox.IsUserTerminated() || mailbox.IsUserRemoved())
                            {
                                _log.Info("User '{0}' was terminated. Tenant = {1}. Disable mailboxes for user.",
                                          mailbox.UserId,
                                          mailbox.TenantId);
                                _manager.DisableMailboxesForUser(mailbox.TenantId, mailbox.UserId);
                                RemoveFromQueue(mailbox.TenantId, mailbox.UserId);
                                return(false);
                            }

                            var cacheItem       = new CacheItem(mailbox.TenantId.ToString(CultureInfo.InvariantCulture), type);
                            var cacheItemPolicy = new CacheItemPolicy
                            {
                                RemovedCallback    = CacheEntryRemove,
                                AbsoluteExpiration =
                                    DateTimeOffset.UtcNow.Add(_tasksConfig.TenantCachingPeriod)
                            };
                            lock (_locker)
                            {
                                _tenantMemCache.Add(cacheItem, cacheItemPolicy);
                            }
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        _log.Error("TryLockMailbox() -> GetTariffType Exception:\r\n{0}\r\n", e.ToString());
                    }
                }
                else
                {
                    _log.Debug("Tenant {0} is in cache", mailbox.TenantId);
                }

                if (mailbox.IsTenantQuotaEnded(_tasksConfig.MinQuotaBalance, _log))
                {
                    _log.Info("Tenant = {0} User = {1}. Quota is ended.", mailbox.TenantId, mailbox.UserId);
                    if (!mailbox.QuotaError)
                    {
                        _manager.CreateQuotaErrorWarningAlert(mailbox.TenantId, mailbox.UserId);
                    }
                    _manager.SetNextLoginDelayedForUser(mailbox.TenantId, mailbox.UserId, _tasksConfig.QuotaEndedDelay);
                    RemoveFromQueue(mailbox.TenantId, mailbox.UserId);
                    return(false);
                }

                return(_manager.LockMailbox(mailbox.MailBoxId, true));
            }
            catch (Exception ex)
            {
                _log.Error("TryLockMailbox(MailboxId={0} is {1}) Exception:\r\n{2}\r\n", mailbox.MailBoxId,
                           mailbox.Active ? "active" : "inactive", ex.ToString());

                return(false);
            }
        }
예제 #2
0
        private bool TryLockMailbox(MailBox mailbox)
        {
            _log.Debug("TryLockMailbox(MailboxId={0} is {1})", mailbox.MailBoxId, mailbox.Active ? "active" : "inactive");

            try
            {
                var contains = _tenantMemCache.Contains(mailbox.TenantId.ToString(CultureInfo.InvariantCulture));

                if (!contains)
                {
                    _log.Debug("Tenant {0} isn't in cache", mailbox.TenantId);
                    try
                    {
                        var type = mailbox.GetTenantStatus(_tasksConfig.TenantOverdueDays, _tasksConfig.DefaultApiSchema, _log);

                        switch (type)
                        {
                        case Defines.TariffType.LongDead:
                            _log.Info("Tenant {0} is not paid. Disable mailboxes.", mailbox.TenantId);
                            _manager.DisableMailboxesForTenant(mailbox.TenantId);
                            RemoveFromQueue(mailbox.TenantId);
                            return(false);

                        case Defines.TariffType.Overdue:
                            _log.Info("Tenant {0} is not paid. Stop processing mailboxes.", mailbox.TenantId);
                            _manager.SetNextLoginDelayedForTenant(mailbox.TenantId, _tasksConfig.OverdueAccountDelay);
                            RemoveFromQueue(mailbox.TenantId);
                            return(false);

                        default:
                            _log.Info("Tenant {0} is paid.", mailbox.TenantId);

                            if (mailbox.IsUserTerminated() || mailbox.IsUserRemoved())
                            {
                                _log.Info("User '{0}' was terminated. Tenant = {1}. Disable mailboxes for user.",
                                          mailbox.UserId,
                                          mailbox.TenantId);
                                _manager.DisableMailboxesForUser(mailbox.TenantId, mailbox.UserId);
                                RemoveFromQueue(mailbox.TenantId, mailbox.UserId);
                                return(false);
                            }

                            var expired = DateTime.UtcNow.Add(_tasksConfig.TenantCachingPeriod);

                            var tenantData = new TenantData
                            {
                                TenantId    = mailbox.TenantId,
                                TariffType  = type,
                                ExpiredDate = expired
                            };

                            AddTenantToCache(tenantData);

                            SaveTenantsToDump();

                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        _log.Error("TryLockMailbox() -> GetTariffType Exception:\r\n{0}\r\n", e.ToString());
                    }
                }
                else
                {
                    _log.Debug("Tenant {0} is in cache", mailbox.TenantId);
                }

                if (mailbox.IsTenantQuotaEnded(_tasksConfig.TenantMinQuotaBalance, _log))
                {
                    _log.Info("Tenant = {0} User = {1}. Quota is ended.", mailbox.TenantId, mailbox.UserId);
                    if (!mailbox.QuotaError)
                    {
                        _manager.CreateQuotaErrorWarningAlert(mailbox.TenantId, mailbox.UserId);
                    }
                    _manager.SetNextLoginDelayedForUser(mailbox.TenantId, mailbox.UserId, _tasksConfig.QuotaEndedDelay);
                    RemoveFromQueue(mailbox.TenantId, mailbox.UserId);
                    return(false);
                }

                return(_manager.LockMailbox(mailbox.MailBoxId, true));
            }
            catch (Exception ex)
            {
                _log.Error("TryLockMailbox(MailboxId={0} is {1}) Exception:\r\n{2}\r\n", mailbox.MailBoxId,
                           mailbox.Active ? "active" : "inactive", ex.ToString());

                return(false);
            }
        }