Skip to content

Commit

Permalink
Correcció "Number of requests inside batch exceed the limit"
Browse files Browse the repository at this point in the history
  • Loading branch information
mollerentornos committed Sep 20, 2024
1 parent d708d56 commit 633a9c1
Showing 1 changed file with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,7 @@ public boolean addOwnerToTeam(String userId, String teamId) throws MicrosoftCred
public SynchronizationStatus addUsersToTeamOrGroup(String teamId, List<MicrosoftUser> members, SynchronizationStatus status, LinkedList<String> roles) throws MicrosoftCredentialsException {
boolean res = false;
String dataKey = roles.contains(MicrosoftUser.OWNER) ? "ownerId" : "memberId";
boolean generalError = false;

ConversationMemberCollectionRequest postMembers = graphClient.teams(teamId).members()
.buildRequest();
Expand All @@ -1078,12 +1079,13 @@ public SynchronizationStatus addUsersToTeamOrGroup(String teamId, List<Microsoft
for (int i = 0; i <= MAX_REQUESTS; i++) {
List<MicrosoftUser> pendingMembers = members.subList(i * MAX_PER_REQUEST, Math.min(MAX_PER_REQUEST * (i +1 ), members.size()));
List<MicrosoftUser> successMembers = new LinkedList<>();
generalError = false;

int retryCount = 0;
while (!pendingMembers.isEmpty() && retryCount < MAX_RETRY) {
BatchRequestContent batchRequestContent = new BatchRequestContent();

members.forEach(member -> {
pendingMembers.forEach(member -> {
ConversationMember memberToAdd = new ConversationMember();

memberToAdd.oDataType = "#microsoft.graph.aadUserConversationMember";
Expand All @@ -1093,16 +1095,28 @@ public SynchronizationStatus addUsersToTeamOrGroup(String teamId, List<Microsoft
batchRequestContent.addBatchRequestStep(postMembers, HttpMethod.POST, memberToAdd);
});

BatchResponseContent responseContent = getGraphClient().batch().buildRequest().post(batchRequestContent);
HashMap<String, ?> membersResponse = parseBatchResponse(responseContent, members);
BatchResponseContent responseContent;

successMembers.addAll((List<MicrosoftUser>) membersResponse.get("success"));
pendingMembers = (List<MicrosoftUser>) membersResponse.get("failed");
List<Map<String, ?>> errors = (List<Map<String, ?>>) membersResponse.get("errors");
handleMicrosoftExceptions(errors);
retryCount++;
try {
responseContent = getGraphClient().batch().buildRequest().post(batchRequestContent);
HashMap<String, ?> membersResponse = parseBatchResponse(responseContent, pendingMembers);

successMembers.addAll((List<MicrosoftUser>) membersResponse.get("success"));
pendingMembers = (List<MicrosoftUser>) membersResponse.get("failed");
List<Map<String, ?>> errors = (List<Map<String, ?>>) membersResponse.get("errors");
handleMicrosoftExceptions(errors);
} catch (GraphServiceException e) {
log.debug("Microsoft General error adding members ", e);
generalError = true;
break;
} finally {
retryCount++;
}
}

if(generalError)
continue;

for (MicrosoftUser pendingMember : pendingMembers) {
if (!res && status != SynchronizationStatus.ERROR) {
//once ERROR status is set, do not check it again
Expand Down

0 comments on commit 633a9c1

Please sign in to comment.