Skip to content

Commit

Permalink
Update logging
Browse files Browse the repository at this point in the history
  • Loading branch information
darunrs committed Nov 28, 2023
1 parent 1dfa9ed commit 444d3f2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
24 changes: 12 additions & 12 deletions runner/src/dml-handler/dml-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ describe('DML Handler tests', () => {
});

const dmlHandler = await DmlHandler.create(ACCOUNT, hasuraClient, mockPgClient);
await dmlHandler.startConnection();
await dmlHandler.startConnection();
await dmlHandler.startConnection();
await dmlHandler.startConnection('test_indexer');
await dmlHandler.startConnection('test_indexer');
await dmlHandler.startConnection('test_indexer');

expect(startConnection).toHaveBeenCalledTimes(1);
});
Expand All @@ -57,13 +57,13 @@ describe('DML Handler tests', () => {
});

const dmlHandler = await DmlHandler.create(ACCOUNT, hasuraClient, mockPgClient);
await dmlHandler.startConnection();
await dmlHandler.endConnection();
await dmlHandler.endConnection();
await dmlHandler.startConnection('test_indexer');
await dmlHandler.endConnection('test_indexer');
await dmlHandler.endConnection('test_indexer');

expect(startConnection).toHaveBeenCalledTimes(1);
expect(endConnection).toHaveBeenCalledTimes(1);
expect(endConnection).toHaveBeenCalledWith(false);
expect(endConnection).toHaveBeenCalledWith(false, 'test_indexer');
});

test('Test close connection with failed queries to database', async () => {
Expand All @@ -80,15 +80,15 @@ describe('DML Handler tests', () => {
});

const dmlHandler = await DmlHandler.create(ACCOUNT, hasuraClient, mockPgClient);
await dmlHandler.startConnection();
await dmlHandler.startConnection();
await dmlHandler.startConnection('test_indexer');
await dmlHandler.startConnection('test_indexer');
dmlHandler.setTransactionFailed();
await dmlHandler.endConnection();
await dmlHandler.endConnection();
await dmlHandler.endConnection('test_indexer');
await dmlHandler.endConnection('test_indexer');

expect(startConnection).toHaveBeenCalledTimes(1);
expect(endConnection).toHaveBeenCalledTimes(1);
expect(endConnection).toHaveBeenCalledWith(true);
expect(endConnection).toHaveBeenCalledWith(true, 'test_indexer');
});

test('Test valid insert one with array', async () => {
Expand Down
8 changes: 4 additions & 4 deletions runner/src/dml-handler/dml-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ export default class DmlHandler {
this.failedTransaction = true;
}

async startConnection (): Promise<void> {
async startConnection (functionName: string): Promise<void> {
if (!this.connected) {
await this.pgClient.startConnection();
await this.pgClient.startConnection(functionName);
this.connected = true;
}
}

async endConnection (): Promise<void> {
async endConnection (functionName: string): Promise<void> {
if (this.connected) {
await this.pgClient.endConnection(this.failedTransaction);
await this.pgClient.endConnection(this.failedTransaction, functionName);
}
this.connected = false;
this.failedTransaction = false;
Expand Down
4 changes: 2 additions & 2 deletions runner/src/indexer/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class Indexer {
const modifiedFunction = this.transformIndexerFunction(indexerFunction.code);
try {
if (modifiedFunction.includes('context.db')) {
await (await dmlHandlerLazyLoader).startConnection();
await (await dmlHandlerLazyLoader).startConnection(functionName);
}
await vm.run(modifiedFunction);
} catch (e) {
Expand All @@ -114,7 +114,7 @@ export default class Indexer {
await this.writeLog(functionName, blockHeight, 'Error running IndexerFunction', error.message);
throw e;
} finally {
await (await dmlHandlerLazyLoader).endConnection();
await (await dmlHandlerLazyLoader).endConnection(functionName);
}
simultaneousPromises.push(this.writeFunctionState(functionName, blockHeight, isHistorical));
} catch (e) {
Expand Down
8 changes: 4 additions & 4 deletions runner/src/pg-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ export default class PgClient {
this.format = pgFormat;
}

async startConnection (): Promise<void> {
async startConnection (functionName: string): Promise<void> {
try {
await this.client.connect();
} catch (error) {
console.error('Failed to connect to client');
throw error;
}
await this.client.query('BEGIN');
console.log('Postgres client connected. Transaction started successfully.');
console.log(`Postgres client connected. Transaction for indexer ${functionName} started successfully.`);
}

async endConnection (failedQuery: boolean): Promise<void> {
async endConnection (failedQuery: boolean, functionName: string): Promise<void> {
await this.client.query(failedQuery ? 'ROLLBACK' : 'COMMIT');
await this.client.end();
console.log(`Transaction ${failedQuery ? 'rolled back' : 'comitted'} successfully. Postgres client disconnected.`);
console.log(`Transaction for indexer ${functionName} ${failedQuery ? 'rolled back' : 'comitted'} successfully. Postgres client disconnected.`);
}

async query<R extends QueryResultRow = any>(query: string, params: any[] = []): Promise<QueryResult<R>> {
Expand Down

0 comments on commit 444d3f2

Please sign in to comment.