private static async Task WatchTeamsAsync(int limit) { //var watch = new System.Diagnostics.Stopwatch(); //watch.Start(); //await DbOperations.ClearTeamsTable(); var teams = DbOperations.GetTeams(limit); if(teams.Count==0) { Console.WriteLine("Fill Teams Table"); await GetTeams(); await DbOperations.AddTeamsToTable(allTeams); teams = DbOperations.GetTeams(limit); } foreach (var team in teams) teamSitesDeltaLinks.Add(team.TeamId, null); //watch.Stop(); //Console.WriteLine($"Checking Teams completed on {watch.ElapsedMilliseconds / 1000} seconds"); while (true)//(!noChanges || iterationCounter != MaxIteration) { try { var watch = new System.Diagnostics.Stopwatch(); watch.Start(); Console.WriteLine("Start Checking Changes on Team Sites..."); await WatchTeamsSitesAsync(); watch.Stop(); processTime = (int)watch.ElapsedMilliseconds / 1000; Console.WriteLine($"{DateTime.UtcNow} - Checking Changes on Team Sites completed on {processTime} seconds"); //AddTeamSiteDeltaLinksToResources(); //await DbOperations.UpdateResourcesAsync(resources); resources = new List<Resource>(); await DbOperations.AddExceptionsAsync(exceptions); exceptions = new List<Exceptions>(); //processTime = (int)DateTime.UtcNow.Subtract(lastProcessTime).TotalSeconds; lastProcessTime = DateTime.UtcNow; var wait = interval - processTime; if (wait < 0) wait = 0; await Task.Delay(wait * 1000); 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.Duration = (int)DateTime.UtcNow.Subtract(perf.StartOn).TotalSeconds; perf.CompletedOn = DateTime.UtcNow; perf.AverageSyncDuration = DbOperations.GetAverageSync(); totalDuration += processTime; perf.TotalDuration = totalDuration; noOfRuns++; perf.NoOfRuns = noOfRuns; perf.AvgDuration = totalDuration / noOfRuns; await DbOperations.UpdatePerformanceAsync(perf); } catch (Exception exc) { Console.WriteLine(exc.Message); AddException(exc, "WatchTeamsAsync"); } } }
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..."); 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"); 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++; } }