Skip to content

Commit

Permalink
Use mutex when adding missing transaction event jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
hbriese committed Sep 29, 2023
1 parent f9d6c6e commit be309d2
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions api/src/features/transactions/transactions.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ import { EIP712_TX_TYPE } from 'zksync-web3/build/src/utils';
import { decodeEventLog, getAbiItem, getEventSelector } from 'viem';
import { ProposalsService } from '../proposals/proposals.service';
import { ProposalEvent } from '../proposals/proposals.input';
import { InjectRedis } from '@songkeys/nestjs-redis';
import Redis from 'ioredis';
import { Mutex } from 'redis-semaphore';

@Injectable()
export class TransactionsEvents implements OnModuleInit {
constructor(
@InjectQueue(TRANSACTIONS_QUEUE.name)
private queue: Queue<TransactionEvent>,
private db: DatabaseService,
@InjectRedis() private redis: Redis,
private provider: ProviderService,
private transactionsProcessor: TransactionsProcessor,
private proposals: ProposalsService,
Expand All @@ -38,8 +42,13 @@ export class TransactionsEvents implements OnModuleInit {
this.transactionsProcessor.onTransaction((data) => this.reverted(data));
}

onModuleInit() {
this.addMissingJobs();
async onModuleInit() {
const mutex = new Mutex(this.redis, 'transactions-missing-jobs');
try {
if (await mutex.tryAcquire()) await this.addMissingJobs();
} finally {
await mutex.release();
}
}

private async executed({ log, receipt, block }: TransactionEventData) {
Expand Down

0 comments on commit be309d2

Please sign in to comment.