private Policy defaultPolicy(Operation operation) { return(Policy.HandleInner <ApiException>().OrInner <HttpRequestException>() .WaitAndRetryForever(retryAttempt => { backoffStrategy.Reconfigure(0); return TimeSpan.FromMilliseconds(backoffStrategy.Calculate()); }, (ex, span) => { Console.WriteLine($"Failure in operation {operation} topic {topicName} with error {ex.GetType().ToString()}. Will try again in {span} seconds..."); } )); }
public void DoPolling() { // Query External Tasks var fetchAndLockBackoff = 0L; try { var fetchExternalTasks = new FetchExternalTasks() { WorkerId = workerId, MaxTasks = topicManagerInfo.MaxTasks, Topics = new List <FetchExternalTaskTopic>() { new FetchExternalTaskTopic(topicManagerInfo.TopicName, topicManagerInfo.LockDurationInMilliseconds) { Variables = topicManagerInfo.VariablesToFetch == null ? topicManagerInfo.VariablesToFetch : null } } }; var tasks = new List <LockedExternalTask>(); policyManager.fetchAndLockPolicy().Execute(() => { tasks = externalTaskService.FetchAndLock(fetchExternalTasks).Result; }); backoffStrategy.Reconfigure(tasks.Count); fetchAndLockBackoff = backoffStrategy.Calculate(); Console.WriteLine($"Fetch and locked {tasks.Count} tasks in topic {topicManagerInfo.TopicName}. Will try again in {TimeSpan.FromMilliseconds(fetchAndLockBackoff)} seconds"); // run them in parallel with a max degree of parallelism Parallel.ForEach( tasks, new ParallelOptions { MaxDegreeOfParallelism = topicManagerInfo.MaxDegreeOfParallelism }, externalTask => { Execute(externalTask); } ); } finally { // schedule next run (if not stopped in between) if (taskQueryTimer != null) { taskQueryTimer.Change(TimeSpan.FromMilliseconds(fetchAndLockBackoff), TimeSpan.FromMilliseconds(Timeout.Infinite)); } } }