public async Task <TimeSpan> UpdateServiceHelth() { // Read saved service providers from the DB List <ServiceProvider> serviceProviders = await serviceProviderRepository.GetAll(); // Check network connectivity status ServiceStatusResult updateResult = await networkService.CheckTcpConnection(serviceProviders); // write new status to the DB Task updateServiceProviderTask = serviceProviderRepository.Upsert(updateResult.ServiceProviders); // send mail notifications Task <TimeSpan> sendMailTask = SendNotifications(updateResult); // DB update, email notification and polling freaquency read can happen asynchronusly // Let the DB query run asynchronously while above two operations runs Task <TimeSpan> pollingFreaquencyTask = serviceProviderRepository.GetMaximumPollingFreaquency(); // wait for DB update and notifications to complete await updateServiceProviderTask; TimeSpan minimumGracePeriod = await sendMailTask; // wait for the query result. This should be already completed TimeSpan pollingFreaquency = await pollingFreaquencyTask; // Next update cycle will be initialized based on the minimum value between polling frequency and minimum grace period of failed services. // From the NetworkService class correct polling frequency will be checked and skipped unnecessary network checks TimeSpan nextUpdateFrequency = minimumGracePeriod < pollingFreaquency ? minimumGracePeriod : pollingFreaquency; return(nextUpdateFrequency); }