private static async Task WatchTeamsAsync() { int count = 0; var deltaCollection = await graphClient.Groups .Delta() .Request() .Select("id,displayName,visibility,resourceProvisioningOptions,CreatedDateTime") .GetAsync(); teamsDeltaCalls++; var watch = new System.Diagnostics.Stopwatch(); watch.Start(); while (true) { if (deltaCollection.CurrentPage.Count <= 0) { Console.WriteLine("No changes on teams..."); await WatchTeamsSitesAsync(); } else { var teamsFiltered = deltaCollection.CurrentPage.Where(w => w.ResourceProvisioningOptions != null && w.ResourceProvisioningOptions.Contains("Team")); Activity activity; if (firstCall) { activity = Activity.Exist; } else { activity = Activity.Added; } var options = new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }; ConcurrentBag <string> resultCollection = new ConcurrentBag <string>(); ParallelLoopResult result = Parallel.ForEach(teamsFiltered, options, team => { if (LogAddedTeam(team, activity)) { resultCollection.Add(team.Id); } }); foreach (var teamId in resultCollection) { if (!teamSitesDeltaLinks.ContainsKey(teamId)) { teamSitesDeltaLinks.Add(teamId, null); } } //if (result&&!teamSitesDeltaLinks.ContainsKey(team.Id)) // teamSitesDeltaLinks.Add(team.Id, null); //foreach (var team in teamsFiltered) //{ // await LogAddedTeamAsync(team, activity); // //count++; // //if (count > 10) // // break; //} } var nextLink = string.Empty; var deltaLink = string.Empty; if (deltaCollection.AdditionalData.ContainsKey("@odata.nextLink") && deltaCollection.AdditionalData["@odata.nextLink"] != null) { nextLink = deltaCollection.AdditionalData["@odata.nextLink"].ToString(); deltaCollection.InitializeNextPageRequest(graphClient, nextLink); deltaCollection = await deltaCollection.NextPageRequest .GetAsync(); teamsDeltaCalls++; continue; } if (deltaCollection.AdditionalData["@odata.deltaLink"] != null) { deltaLink = deltaCollection.AdditionalData["@odata.deltaLink"].ToString(); } watch.Stop(); Console.WriteLine($"Checking Teams completed on {watch.ElapsedMilliseconds / 1000} seconds"); await WatchTeamsSitesAsync(); processTime = (int)DateTime.UtcNow.Subtract(lastProcessTime).TotalSeconds; lastProcessTime = DateTime.UtcNow; var wait = interval - processTime; if (wait < 0) { wait = 0; } await Task.Delay(wait * 1000); await DbOperations.UpdateResourcesAsync(resources); resources.Clear(); firstCall = false; watch.Start(); deltaCollection.InitializeNextPageRequest(graphClient, deltaLink); deltaCollection = await deltaCollection.NextPageRequest .GetAsync(); teamsDeltaCalls++; } }
private static async Task WatchTeamsAsync(int limit) { var watch = new System.Diagnostics.Stopwatch(); watch.Start(); //await GetTeams(); //await DbOperations.AddTeamsToTable(allTeams); var teams = DbOperations.GetTeams(limit);//.Skip(limit*2);//allTeams.OrderBy(o => o.CreatedDateTime).Take(limit); foreach (var team in teams) { teamSitesDeltaLinks.Add(team.TeamId, null); } watch.Stop(); Console.WriteLine($"Checking Teams completed on {watch.ElapsedMilliseconds / 1000} seconds"); while (!noChanges || iterationCounter != MaxIteration) { try { await WatchTeamsSitesAsync(); processTime = (int)DateTime.UtcNow.Subtract(lastProcessTime).TotalSeconds; lastProcessTime = DateTime.UtcNow; var wait = interval - processTime; if (wait < 0) { wait = 0; } await Task.Delay(wait * 1000); await DbOperations.UpdateResourcesAsync(resources); resources.Clear(); firstCall = false; if (teamSitesDeltaLinks.Any(w => w.Value == null)) { noChanges = false; iterationCounter = 0; } else if (teamSitesDeltaLinks.All(w => w.Value.NoChanges)) { noChanges = true; ++iterationCounter; } else { noChanges = false; iterationCounter = 0; } perf.ActivitiesCalls = activitiesCalls; perf.DeltaCalls = libraryDeltaCalls; perf.AverageSyncDuration = DbOperations.GetAverageSync(); await DbOperations.UpdatePerformanceAsync(perf); } catch (Exception exc) { Console.WriteLine(exc.Message); } } }