Skip to content

Commit

Permalink
fix: avoiding duplicate discovery tasks
Browse files Browse the repository at this point in the history
Added locking on the vertex to avoid duplicating a task due to racing. Also catching and throwing the singleton reason, so we don't get any errors on tasks that were canceled for this reason.
  • Loading branch information
tegefaulkes committed Sep 28, 2022
1 parent f38ceee commit 02912dd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 15 additions & 6 deletions src/discovery/Discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class Discovery {
await this.startDiscoveryForVertex(vertex);
return;
}
// Aborting a duplicate task is not an error
if (e === abortSingletonTaskReason) return;
throw e;
}
};
Expand Down Expand Up @@ -451,6 +453,10 @@ class Discovery {
);
}

// Locking on vertex to avoid duplicates
await tran.lock(
[this.constructor.name, this.discoverVertexHandlerId, vertex].join(''),
);
// Check if task exists
let taskExists = false;
for await (const task of this.taskManager.getTasks(
Expand All @@ -467,12 +473,15 @@ class Discovery {
task.cancel(abortSingletonTaskReason);
}
// Create a new task if none exists
await this.taskManager.scheduleTask({
handlerId: this.discoverVertexHandlerId,
parameters: [vertex],
path: [this.constructor.name, this.discoverVertexHandlerId, vertex],
lazy: true,
});
await this.taskManager.scheduleTask(
{
handlerId: this.discoverVertexHandlerId,
parameters: [vertex],
path: [this.constructor.name, this.discoverVertexHandlerId, vertex],
lazy: true,
},
tran,
);
}

/**
Expand Down
2 changes: 0 additions & 2 deletions tests/discovery/Discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,8 @@ describe('Discovery', () => {
const [, claimEncoded] = await nodeB.sigchain.addClaim(identityClaim);
const claim = claimsUtils.decodeClaim(claimEncoded);
await testProvider.publishClaim(identityId, claim);
logger.setLevel(LogLevel.INFO);
});
afterEach(async () => {
logger.setLevel(LogLevel.WARN);
await taskManager.stopProcessing();
await taskManager.stopTasks();
await nodeA.stop();
Expand Down

0 comments on commit 02912dd

Please sign in to comment.