diff --git a/packages/mina-service/src/service/task-service.ts b/packages/mina-service/src/service/task-service.ts index eaf55804..6be09d52 100644 --- a/packages/mina-service/src/service/task-service.ts +++ b/packages/mina-service/src/service/task-service.ts @@ -1,6 +1,6 @@ -import { logger } from '@helper'; +import { config, logger } from '@helper'; import { Proof } from '@domain'; -import { withCompoundTransaction } from '@zkdb/storage'; +import { DatabaseEngine, withCompoundTransaction } from '@zkdb/storage'; export class TaskService { private maxRetries: number; @@ -58,5 +58,21 @@ export class TaskService { export const TASK_SERVICE = { clusterName: 'task', - payload: new TaskService().run, + payload: async () => { + // Connect to db + const serverlessDb = DatabaseEngine.getInstance(config.MONGODB_URL); + const proofDb = DatabaseEngine.getInstance(config.PROOF_MONGODB_URL); + + if (!serverlessDb.isConnected()) { + await serverlessDb.connect(); + } + + if (!proofDb.isConnected()) { + await proofDb.connect(); + } + + const taskService = new TaskService(); + + await taskService.run(); + }, }; diff --git a/packages/mina-service/src/service/transaction-service.ts b/packages/mina-service/src/service/transaction-service.ts index 330da52f..0434c699 100644 --- a/packages/mina-service/src/service/transaction-service.ts +++ b/packages/mina-service/src/service/transaction-service.ts @@ -1,8 +1,8 @@ -import { logger } from '@helper'; +import { config, logger } from '@helper'; import { Fill } from '@orochi-network/queue'; import { ETransactionStatus } from '@zkdb/common'; import { MinaNetwork } from '@zkdb/smart-contract'; -import { ModelTransaction } from '@zkdb/storage'; +import { DatabaseEngine, ModelTransaction } from '@zkdb/storage'; import { schedule } from 'node-cron'; // Base on Mina protocol blockscan we divide to 10 @@ -13,6 +13,18 @@ export const SERVICE_TRANSACTION = { payload: async () => { let isRunning = false; + // Connect to db + const serverlessDb = DatabaseEngine.getInstance(config.MONGODB_URL); + const proofDb = DatabaseEngine.getInstance(config.PROOF_MONGODB_URL); + + if (!serverlessDb.isConnected()) { + await serverlessDb.connect(); + } + + if (!proofDb.isConnected()) { + await proofDb.connect(); + } + schedule(CRON_SCHEDULE, async () => { if (isRunning) { logger.debug('Task skipped to prevent overlap:', new Date());