예제 #1
0
        private void ClearGarbage(MailBox mailbox)
        {
            var taskLog = LoggerFactory.GetLogger(LoggerFactory.LoggerType.Log4Net, "Task_" + Task.CurrentId);

            taskLog.Info("Processing MailboxId = {0}, email = '{1}', tenant = '{2}', user = '******'",
                         mailbox.MailBoxId, mailbox.EMail.Address, mailbox.TenantId, mailbox.UserId);

            var type = _tenantMemCache.Get(mailbox.TenantId.ToString(CultureInfo.InvariantCulture));

            if (type == null)
            {
                taskLog.Info("Tenant {0} isn't in cache", mailbox.TenantId);

                taskLog.Debug("GetTenantStatus(OverdueDays={0})", Config.TenantOverdueDays);

                type = mailbox.GetTenantStatus(Config.TenantOverdueDays, Config.HttpContextScheme, _log);

                var cacheItem = new CacheItem(mailbox.TenantId.ToString(CultureInfo.InvariantCulture), type);

                var cacheItemPolicy = new CacheItemPolicy
                {
                    AbsoluteExpiration =
                        DateTimeOffset.UtcNow.AddDays(Config.TenantCacheDays)
                };

                _tenantMemCache.Add(cacheItem, cacheItemPolicy);
            }
            else
            {
                taskLog.Info("Tenant {0} is in cache", mailbox.TenantId);
            }

            taskLog.Info("Tenant {0} has status '{1}'", mailbox.TenantId, type.ToString());

            if (Defines.TariffType.LongDead == (Defines.TariffType)type)
            {
                RemoveMailboxData(mailbox, true, taskLog);
            }
            else if (mailbox.IsUserRemoved())
            {
                taskLog.Info("User has been terminated.");
                RemoveMailboxData(mailbox, true, taskLog);
            }
            else if (mailbox.IsRemoved)
            {
                taskLog.Info("Mailbox is removed.");
                RemoveMailboxData(mailbox, false, taskLog);
            }
            else
            {
                RemoveGarbageMailData(mailbox, Config.GarbageOverdueDays, taskLog);
            }

            taskLog.Info("Mailbox {0} processing complete.", mailbox.MailBoxId);
        }
예제 #2
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);
            }
        }
예제 #3
0
        private bool TryLockMailbox(MailBox mailbox)
        {
            _log.Debug("TryLockMailbox(MailboxId={0} is {1})", mailbox.MailBoxId, mailbox.Active ? "active" : "inactive");

            try
            {
                if (!_tenantMemCache.Contains(mailbox.TenantId.ToString(CultureInfo.InvariantCulture)))
                {
                    _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);

                                var userTerminated = mailbox.HasTerminatedUser();
                                if (userTerminated)
                                {
                                    _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)
                                    };
                                _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);
                }

                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;
            }

        }
예제 #4
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);
            }
        }