Skip to content

Commit

Permalink
chore: remove workers
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed May 3, 2024
1 parent 5240aa1 commit 1eb6206
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 44 deletions.
1 change: 0 additions & 1 deletion packages/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
"npm-run-all": "^4.1.5",
"pg": "^8.11.5",
"pg-boss": "9.0.3",
"piscina": "4.4.0",
"pm2": "5.3.1",
"shallow-equal-object": "1.1.1",
"tai64": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import type { TransactionEntity } from '~/domain/Transaction/TransactionEntity';
import { TransactionRepository } from '~/domain/Transaction/TransactionRepository';
import type { GQLBlock } from '~/graphql/generated/sdk';
import { db } from '~/infra/database/Db';
import type { QueueData } from '~/infra/queue/Queue';
import type { SyncTransactionEvent } from './SyncTransactions';

type Input = SyncTransactionEvent[];

export class RunTransactionsWorker {
export class AddTransactions {
async execute(input: Input) {
await Promise.all(input.map((event) => this.syncTransaction(event)));
}
Expand Down Expand Up @@ -135,11 +134,11 @@ export class RunTransactionsWorker {
}
}

export const runTransactionsWorker = async ({ data }: QueueData<Input>) => {
export const addTransactions = async (data: Input) => {
await db.connect();
try {
const syncTransactions = new RunTransactionsWorker();
await syncTransactions.execute(data);
const { execute } = new AddTransactions();
await execute(data);
} catch (error) {
console.error(error);
throw new Error('Sync transactions', {
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/src/application/uc/SyncTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import c from 'chalk';
import { uniqBy } from 'lodash';
import type { GQLBlock } from '~/graphql/generated/sdk';
import type { QueueData, QueueInputs, QueueNames } from '~/infra/queue/Queue';
import { pool } from '~/infra/worker/WorkerPool';
import { addTransactions } from './AddTransactions';

type Input = QueueInputs[QueueNames.SYNC_TRANSACTIONS];
export type SyncTransactionEvent = {
Expand All @@ -28,7 +28,7 @@ export class SyncTransactions {
const toBlock = blocks[blocks.length - 1].header.height;
const msg = `# Syncing ${events.length} transactions from ${fromBlock} to ${toBlock}`;
console.log(c.gray(msg));
await pool.run({ data: events });
await addTransactions(events);
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions packages/graphql/src/domain/Block/BlockRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ import { BlockEntity } from './BlockEntity';
import { type BlockItem, BlocksTable } from './BlockModel';

export class BlockRepository {
async findByHash(blockHash: string) {
const first = await db.connection().query.BlocksTable.findFirst({
where: eq(BlocksTable.blockHash, blockHash),
with: {
transactions: true,
},
});

if (!first) return null;
const { transactions, ...block } = first;
return BlockEntity.create(block, transactions);
}

async findByHeight(height: number) {
const first = await db.connection().query.BlocksTable.findFirst({
where: eq(BlocksTable._id, height),
Expand Down
5 changes: 0 additions & 5 deletions packages/graphql/src/infra/worker/Worker.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/graphql/src/infra/worker/WorkerPool.ts

This file was deleted.

25 changes: 0 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1eb6206

Please sign in to comment.