Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 7c8518c

Browse files
authored
Merge pull request #41 from afrisalyp/issue-277
Local issue creation lock for more reliable lock.
2 parents 2da2c33 + 3af66fc commit 7c8518c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

services/IssueService.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ const eventService = require('./EventService');
2626

2727
const md = new MarkdownIt();
2828

29+
// A variable to store issue creation lock to prevent duplicate creation process.
30+
// The key is `${provider}-${repositoryId}-${number}`. The value is True.
31+
const issueCreationLock = {};
32+
2933
/**
3034
* Generate the contest url, given the challenge id
3135
* @param {String} challengeId The id of the challenge in topcoder
@@ -555,6 +559,13 @@ async function handleIssueCreate(event, issue, forceAssign = false) {
555559
return;
556560
}
557561

562+
const creationLockKey = `${issue.provider}-${issue.repositoryId}-${issue.number}`;
563+
if (issueCreationLock[creationLockKey]) {
564+
throw new Error(
565+
`Issue ${creationLockKey} is creating.`);
566+
}
567+
issueCreationLock[creationLockKey] = true;
568+
558569
// create issue with challenge creation pending
559570
const issueObject = _.assign({}, _.omit(issue, 'assignee'), {
560571
id: helper.generateIdentifier(),
@@ -594,6 +605,7 @@ async function handleIssueCreate(event, issue, forceAssign = false) {
594605
logger.error(`Challenge creation failure: ${e}`);
595606
await dbHelper.removeIssue(models.Issue, issue.repositoryId, issue.number, issue.provider);
596607
await eventService.handleEventGracefully(event, issue, e);
608+
delete issueCreationLock[creationLockKey];
597609
return;
598610
}
599611

@@ -610,6 +622,7 @@ async function handleIssueCreate(event, issue, forceAssign = false) {
610622
await handleIssueAssignment(event, issue, true);
611623
}
612624
}
625+
delete issueCreationLock[creationLockKey];
613626
logger.debug(`new challenge created with id ${issue.challengeId} for issue ${issue.number}`);
614627
}
615628

0 commit comments

Comments
 (0)