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