private bool NeedRemove(MailBoxData mailbox, ILog taskLog) { var needRemove = false; lock (Locker) { Defines.TariffType type; var memTenantItem = TenantMemCache.Get(mailbox.TenantId.ToString(CultureInfo.InvariantCulture)); if (memTenantItem == null) { taskLog.InfoFormat("Tenant {0} isn't in cache", mailbox.TenantId); taskLog.DebugFormat("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.InfoFormat("Tenant {0} is in cache", mailbox.TenantId); type = (Defines.TariffType)memTenantItem; } taskLog.InfoFormat("Tenant {0} has status '{1}'", mailbox.TenantId, type.ToString()); if (type == Defines.TariffType.LongDead) { needRemove = true; } else { var isUserRemoved = mailbox.IsUserRemoved(); taskLog.InfoFormat("User '{0}' status is '{1}'", mailbox.UserId, isUserRemoved ? "Terminated" : "Not terminated"); if (isUserRemoved) { needRemove = true; } } } return(needRemove); }
private bool TryLockMailbox(MailBoxData mailbox) { _log.DebugFormat("TryLockMailbox(MailboxId={0} is {1})", mailbox.MailBoxId, mailbox.Active ? "active" : "inactive"); try { var contains = _tenantMemCache.Contains(mailbox.TenantId.ToString(CultureInfo.InvariantCulture)); if (!contains) { _log.DebugFormat("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.InfoFormat("Tenant {0} is not paid. Disable mailboxes.", mailbox.TenantId); _engineFactory.MailboxEngine.DisableMailboxes( new TenantMailboxExp(mailbox.TenantId)); var userIds = _engineFactory.MailboxEngine.GetMailUsers(new TenantMailboxExp(mailbox.TenantId)) .ConvertAll(t => t.Item2); _engineFactory.AlertEngine.CreateDisableAllMailboxesAlert(mailbox.TenantId, userIds); RemoveFromQueue(mailbox.TenantId); return(false); case Defines.TariffType.Overdue: _log.InfoFormat("Tenant {0} is not paid. Stop processing mailboxes.", mailbox.TenantId); _engineFactory.MailboxEngine.SetNextLoginDelay(new TenantMailboxExp(mailbox.TenantId), _tasksConfig.OverdueAccountDelay); RemoveFromQueue(mailbox.TenantId); return(false); default: _log.InfoFormat("Tenant {0} is paid.", mailbox.TenantId); var expired = DateTime.UtcNow.Add(_tasksConfig.TenantCachingPeriod); var tenantData = new TenantData { Tenant = mailbox.TenantId, TariffType = type, Expired = expired }; AddTenantToCache(tenantData); break; } } catch (Exception e) { _log.ErrorFormat("TryLockMailbox() -> GetTariffType Exception:\r\n{0}\r\n", e.ToString()); } } else { _log.DebugFormat("Tenant {0} is in cache", mailbox.TenantId); } if (mailbox.IsUserTerminated() || mailbox.IsUserRemoved()) { _log.InfoFormat("User '{0}' was terminated. Tenant = {1}. Disable mailboxes for user.", mailbox.UserId, mailbox.TenantId); _engineFactory.MailboxEngine.DisableMailboxes( new UserMailboxExp(mailbox.TenantId, mailbox.UserId)); _engineFactory.AlertEngine.CreateDisableAllMailboxesAlert(mailbox.TenantId, new List <string> { mailbox.UserId }); RemoveFromQueue(mailbox.TenantId, mailbox.UserId); return(false); } if (mailbox.IsTenantQuotaEnded(_tasksConfig.TenantMinQuotaBalance, _log)) { _log.InfoFormat("Tenant = {0} User = {1}. Quota is ended.", mailbox.TenantId, mailbox.UserId); if (!mailbox.QuotaError) { _engineFactory.AlertEngine.CreateQuotaErrorWarningAlert(mailbox.TenantId, mailbox.UserId); } _engineFactory.MailboxEngine.SetNextLoginDelay(new UserMailboxExp(mailbox.TenantId, mailbox.UserId), _tasksConfig.QuotaEndedDelay); RemoveFromQueue(mailbox.TenantId, mailbox.UserId); return(false); } return(_engineFactory.MailboxEngine.LockMaibox(mailbox.MailBoxId)); } catch (Exception ex) { _log.ErrorFormat("TryLockMailbox(MailboxId={0} is {1}) Exception:\r\n{2}\r\n", mailbox.MailBoxId, mailbox.Active ? "active" : "inactive", ex.ToString()); return(false); } }