private IEnumerable <DnsCheckTaskDto> GetTasks() { var dnsTasks = new List <DnsCheckTaskDto>(); try { var dnsCheckDal = new DalDomainDnsCheck(); dnsTasks = dnsCheckDal.GetOldUnverifiedTasks(_checkUnverifiedInMinutes, _checkTasksLimit); _log.Info("Found {0} unverified tasks to check.", dnsTasks.Count); if (!dnsTasks.Any() || dnsTasks.Count < _checkTasksLimit) { var dnsVerifiedTasks = dnsCheckDal.GetOldVerifiedTasks(_checkVerifiedInMinutes, _checkTasksLimit - dnsTasks.Count); _log.Info("Found {0} verified tasks to check.", dnsVerifiedTasks.Count); if (dnsVerifiedTasks.Any()) { dnsTasks.AddRange(dnsVerifiedTasks); } } } catch (Exception ex) { _log.Error("GetTasks() Exception: \r\n {0} \r\n", ex.ToString()); } return(dnsTasks); }
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(); } }