Skip to content

Commit

Permalink
chore: queue performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed May 4, 2024
1 parent 06ed571 commit a573fee
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
3 changes: 2 additions & 1 deletion packages/graphql/src/application/uc/AddBlockRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ type Input = QueueInputs[QueueNames.ADD_BLOCK_RANGE];

export class AddBlockRange {
async execute({ data }: QueueData<Input>) {
const { blocks } = data;
const { from, to } = data;
const repo = new BlockRepository();
const { blocks } = await repo.blocksFromNode(to - from, from);
await repo.upsertMany(blocks);
await queue.push(QueueNames.SYNC_TRANSACTIONS, { blocks });
}
Expand Down
23 changes: 7 additions & 16 deletions packages/graphql/src/application/uc/SyncBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type Context = Input & {

type EventsReturn = {
endCursor: number | undefined;
hasBlocks: boolean;
};

class Syncer {
Expand All @@ -31,7 +30,7 @@ class Syncer {
return repo.latestBlockFromNode();
}

private getLastBlockHeight(ctx: Context) {
getLastBlockHeight(ctx: Context) {
return Number(ctx.lastBlock?.header.height ?? '0');
}

Expand Down Expand Up @@ -85,20 +84,10 @@ class Syncer {
private async syncBlocksRange({ from, to }: { from: number; to: number }) {
console.log(c.gray(`🔄 Syncing blocks from ${from} to ${to}`));
if (!env.get('IS_DEV_TEST')) {
const repo = new BlockRepository();
const { blocks, endCursor } = await repo.blocksFromNode(to - from, from);
await queue.push(QueueNames.ADD_BLOCK_RANGE, { blocks });
const hasBlocks = blocks.length > 0;

return {
endCursor,
hasBlocks,
};
await queue.push(QueueNames.ADD_BLOCK_RANGE, { from, to });
return { endCursor: to };
}
return {
endCursor: to,
hasBlocks: true,
};
return { endCursor: to };
}

async syncMissingBlocks(ctx: Context) {
Expand Down Expand Up @@ -136,7 +125,9 @@ const machine = setup({
},
guards: {
hasMoreEvents: ({ context }) => {
return context.lastResult?.hasBlocks ?? false;
const endCursor = context.lastResult?.endCursor ?? 0;
const lastBlockHeight = syncer.getLastBlockHeight(context);
return Boolean(endCursor < lastBlockHeight);
},
needToWatch: ({ context }) => {
return Boolean(context.watch);
Expand Down
3 changes: 2 additions & 1 deletion packages/graphql/src/infra/queue/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export type QueueInputs = {
watch?: boolean;
};
[QueueNames.ADD_BLOCK_RANGE]: {
blocks: GQLBlock[];
from: number;
to: number;
};
[QueueNames.SYNC_TRANSACTIONS]: {
blocks: GQLBlock[];
Expand Down

0 comments on commit a573fee

Please sign in to comment.