Ejemplo n.º 1
0
 private void SetDomainChecked(int domainId)
 {
     try
     {
         var dnsCheckDal = new DalDomainDnsCheck();
         dnsCheckDal.SetDomainChecked(domainId);
     }
     catch (Exception ex)
     {
         _log.Error("SetDomainChecked(domainId={0}) Exception: \r\n {1} \r\n",
                    domainId, ex.ToString());
     }
 }
Ejemplo n.º 2
0
        private void CheckDnsSettings(ManualResetEvent resetEvent)
        {
            var dnsTasks = GetTasks();

            foreach (var dnsTask in dnsTasks)
            {
                try
                {
                    var contains = false;
                    lock (locker)
                    {
                        contains = _tenantMemCache.Contains(dnsTask.tenant.ToString(CultureInfo.InvariantCulture));
                    }
                    if (!contains)
                    {
                        _log.Debug("Tenant {0} isn't in cache", dnsTask.tenant);

                        CoreContext.TenantManager.SetCurrentTenant(dnsTask.tenant);

                        var tenantInfo = CoreContext.TenantManager.GetCurrentTenant();

                        SecurityContext.AuthenticateMe(tenantInfo.OwnerId);

                        Defines.TariffType type;

                        try
                        {
                            type = ApiHelper.GetTenantTariff(TENANT_OVERDUE_DAYS);
                        }
                        catch (Exception ex)
                        {
                            _log.Error("GetTenantStatus() Exception:\r\n{0}\r\n", ex.ToString());
                            type = Defines.TariffType.Active;
                        }

                        switch (type)
                        {
                        case Defines.TariffType.LongDead:
                            _log.Info("Tenant {0} is not paid too long. Removing domain with mailboxes, aliases and groups.",
                                      dnsTask.tenant);
                            RemoveDomain(dnsTask.tenant, dnsTask.user, dnsTask.domain_id);
                            continue;

                        case Defines.TariffType.Overdue:
                            _log.Info("Tenant {0} is not paid. Stop processing domain.", dnsTask.tenant);
                            _dnsCheckDal.SetDomainDisabled(dnsTask.domain_id, _disableUnpaidDomainDays);
                            continue;

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

                            var cacheItem = new CacheItem(dnsTask.tenant.ToString(CultureInfo.InvariantCulture),
                                                          type);
                            var cacheItemPolicy = new CacheItemPolicy
                            {
                                RemovedCallback    = CacheEntryRemove,
                                AbsoluteExpiration =
                                    DateTimeOffset.UtcNow.Add(_tenantCachingPeriod)
                            };
                            lock (locker)
                            {
                                _tenantMemCache.Add(cacheItem, cacheItemPolicy);
                            }
                            break;
                        }
                    }
                    else
                    {
                        _log.Debug("Tenant {0} is in cache", dnsTask.tenant);
                    }

                    var isVerified = CheckDomainDns(dnsTask);
                    if (isVerified != dnsTask.domain_is_verified)
                    {
                        _log.Info("Domain '{0}' dns-records changed: they are {1} now.",
                                  dnsTask.domain_name, isVerified ? "verified" : "unverified");
                        _dnsCheckDal.SetDomainVerifiedAndChecked(dnsTask.domain_id, isVerified);
                    }
                    else
                    {
                        _log.Info("Domain '{0}' dns-records not changed.", dnsTask.domain_name);
                        _dnsCheckDal.SetDomainChecked(dnsTask.domain_id);
                    }

                    if (resetEvent.WaitOne(_waitBeforeNextTaskCheckInMilliseconds))
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    _log.Error("Unable to check tasks. Exception:\r\n{0}", ex.ToString());
                }
            }
        }
        void MainThread()
        {
            try
            {
                _log.Info("Start service\r\n" +
                          "Configuration: \r\n" +
                          "\t\t- check dns-records of all domains in every {0} minutes;\r\n" +
                          "\t\t- check no more then {1} domain's dns-records at the same time;\r\n" +
                          "\t\t- check unverified dns-records every {2} minutes;\r\n" +
                          "\t\t- check verified dns-records every {3} minutes;\r\n" +
                          "\t\t- tenant caching period {4} minutes\r\n" +
                          "\t\t- disable unpaid domain checks {5} days\r\n" +
                          "\t\t- wait before next task check {6} milliseconds\r\n",
                          _waitTimeInMinutes,
                          _checkTasksLimit,
                          _checkUnverifiedInMinutes,
                          _checkVerifiedInMinutes,
                          _tenantCachingPeriod.Minutes,
                          _disableUnpaidDomainDays,
                          _waitBeforeNextTaskCheckInMilliseconds);


                var wait_time = TimeSpan.FromMinutes(_waitTimeInMinutes);

                while (true)
                {
                    try
                    {
                        var dns_tasks = _dnsCheckDal.GetOldUnverifiedTasks(_checkUnverifiedInMinutes, _checkTasksLimit);

                        _log.Info("Found {0} unverified tasks to check.", dns_tasks.Count);

                        if (!dns_tasks.Any() || dns_tasks.Count < _checkTasksLimit)
                        {
                            var dns_verified_tasks = _dnsCheckDal.GetOldVerifiedTasks(_checkVerifiedInMinutes, _checkTasksLimit - dns_tasks.Count);

                            _log.Info("Found {0} verified tasks to check.", dns_verified_tasks.Count);

                            if (dns_verified_tasks.Any())
                            {
                                dns_tasks.AddRange(dns_verified_tasks);
                            }
                        }

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

                                switch ((MailBoxManager.TariffType)type)
                                {
                                case MailBoxManager.TariffType.LongDead:
                                    _log.Info("Tenant {0} is not paid. Removing domain with mailboxes and groups.", dns_task.tenant);
                                    RemoveDomain(dns_task.tenant, dns_task.user, dns_task.domain_id);
                                    break;

                                case MailBoxManager.TariffType.Overdue:
                                    _log.Info("Tenant {0} is not paid. Stop processing domain.", dns_task.tenant);
                                    _dnsCheckDal.SetDomainDisabled(dns_task.domain_id, _disableUnpaidDomainDays);
                                    break;

                                default:
                                    if (absence)
                                    {
                                        var tenant_key = dns_task.tenant.ToString(CultureInfo.InvariantCulture);
                                        HttpRuntime.Cache.Remove(tenant_key);
                                        HttpRuntime.Cache.Insert(tenant_key, type, null,
                                                                 DateTime.UtcNow.Add(_tenantCachingPeriod), Cache.NoSlidingExpiration);
                                    }

                                    var is_verified = CheckDomainDns(dns_task);
                                    if (is_verified != dns_task.domain_is_verified)
                                    {
                                        _log.Info("Domain '{0}' dns-records changed: they are {1} now.", dns_task.domain_name, is_verified ? "verified" : "unverified");
                                        _dnsCheckDal.SetDomainVerifiedAndChecked(dns_task.domain_id, is_verified);
                                    }
                                    else
                                    {
                                        _log.Info("Domain '{0}' dns-records not changed.", dns_task.domain_name);
                                        _dnsCheckDal.SetDomainChecked(dns_task.domain_id);
                                    }

                                    break;
                                }

                                if (_mreStop.WaitOne(_waitBeforeNextTaskCheckInMilliseconds))
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            _log.Info("Waiting for {0} minutes for next check...", _waitTimeInMinutes);
                            if (_mreStop.WaitOne(wait_time))
                            {
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        _log.Error("Unable to check tasks. Exception:\r\n{0}", ex.ToString());
                        _log.Info("Waiting for {0} minutes for next check...", _waitTimeInMinutes);
                        if (_mreStop.WaitOne(wait_time))
                        {
                            break;
                        }
                    }

                    if (_mreStop.WaitOne(_waitBeforeNextTaskCheckInMilliseconds))
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.Fatal("Main thread Exception:\r\n{0}", ex.ToString());
            }
            finally
            {
                OnStop();
            }
        }