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); }
public async Task AddSubscription(string email, string host, int port, int pollingFreaquency, int gracePeriod) { ServiceProvider serviceProvider = await serviceProviderRepository.GetByHostAndPort(host, port); TimeSpan requestedPollingFrequency = GetTimeSpan(pollingFreaquency); // If service provider is not found add new one serviceProvider = serviceProvider == null ? new ServiceProvider() { Host = host, Port = port, PollingFrequency = requestedPollingFrequency } : serviceProvider; // If service provider is existing always update the service provider with the maximum polling freaquency serviceProvider.PollingFrequency = serviceProvider.PollingFrequency > requestedPollingFrequency? requestedPollingFrequency : serviceProvider.PollingFrequency; List <ServiceProvider> serviceProvidersToUpdate = new List <ServiceProvider>(); serviceProvidersToUpdate.Add(serviceProvider); // Update database with service provider details await serviceProviderRepository.Upsert(serviceProvidersToUpdate); // Refreash service provider after inserting to map the subscription for user if (serviceProvider.Id == 0) { serviceProvider = await serviceProviderRepository.GetByHostAndPort(host, port); } // if service provider not found in the user, update the subscription User user = await userRepository.GetUserByEmail(email); if (user.Subscriptions.Where(s => s.Service.Id == serviceProvider.Id).Count() == 0) { ServiceSubscription serviceSubscription = new ServiceSubscription() { Service = serviceProvider, PollingFrequency = requestedPollingFrequency, GracePeriod = GetTimeSpan(gracePeriod) }; user.Subscriptions.Add(serviceSubscription); List <User> usersToUpdate = new List <User>(); usersToUpdate.Add(user); await userRepository.Upsert(usersToUpdate); } }