public async Task ScheduleSourcesAsync() { if (!await ShouldScheduleAsync()) { return; } var seconds = Convert.ToInt32(_configurationValueProvider.GetValue(ConfigurationKeys.ClusterLockDurationSeconds)); var sources = _sourceConfiguration.GetSources(); foreach (var source in sources) { try { var lockToken = new LockToken(source.ToTypeKey()); if (!(await _lockStore.TryLockAsync(lockToken, tries: 0, aquireTimeoutMilliseconds: 100, timeoutMilliseconds: seconds * 1000))) // if tries < 1 it puts to 1 in beehive { TheTrace.TraceInformation("I could NOT be master for {0}", source.ToTypeKey()); continue; } var resultSource = await TryScheduleSourceAsync(source); if (resultSource != null) { _sourceConfiguration.UpdateSource(resultSource); TheTrace.TraceInformation("MasterScheduler - Updated {0}", resultSource.ToTypeKey()); } await _lockStore.ReleaseLockAsync(lockToken); } catch (Exception e) { TheTrace.TraceError(e.ToString()); } } }
public async Task ScheduleSourcesAsync() { var sources = _sourceConfiguration.GetSources(); foreach (var source in sources) { try { TheTrace.TraceInformation("MasterScheduler - Scheduling {0}", source.ToTypeKey()); if (!source.IsActive.HasValue || !source.IsActive.Value) { TheTrace.TraceInformation("MasterScheduler - NOT active: {0}", source.ToTypeKey()); continue; } await SetupMappingsAsync(source); TheTrace.TraceInformation("MasterScheduler - Finished Mapping setup: {0}", source.ToTypeKey()); if (!source.LastScheduled.HasValue) { source.LastScheduled = DateTimeOffset.UtcNow.AddYears(-1); } // if has been recently scheduled if (source.LastScheduled.Value.AddMinutes(source.SchedulingFrequencyMinutes.Value) > DateTimeOffset.UtcNow) { TheTrace.TraceInformation("MasterScheduler - Nothing to do with {0}. LastScheduled in Future {1}", source.ToTypeKey(), source.LastScheduled.Value); continue; } var schedulerType = Assembly.GetExecutingAssembly().GetType(source.SchedulerType) ?? Type.GetType(source.SchedulerType); if (schedulerType == null) { source.ErrorMessage = "Could not find SchedulerType: " + source.SchedulerType; } var scheduler = (ISourceScheduler)_locator.GetService(schedulerType); var result = await scheduler.TryScheduleAsync(source); TheTrace.TraceInformation( "MasterScheduler - Got result for TryScheduleAsync in {0}. Success => {1}", source.ToTypeKey(), result.Item1); if (result.Item2) { await _eventQueueOperator.PushBatchAsync(result.Item1); } source.ErrorMessage = string.Empty; TheTrace.TraceInformation("MasterScheduler - Finished Scheduling {0}", source.ToTypeKey()); source.LastScheduled = DateTimeOffset.UtcNow; } catch (Exception e) { TheTrace.TraceError(e.ToString()); source.ErrorMessage = e.ToString(); } _sourceConfiguration.UpdateSource(source); TheTrace.TraceInformation("MasterScheduler - Updated {0}", source.ToTypeKey()); } }