import { DatabaseManager } from '@dzlzv/hydra-db-utils' import { Worker, WorkerType } from 'query-node' import { logger, getNextId } from '../src/common' export interface IBootstrapWorkers { storage: IBootstrapWorker[] gateway: IBootstrapWorker[] } export interface IBootstrapWorker { id: string created_at: string } export async function bootWorkers(db: DatabaseManager, workers: IBootstrapWorkers): Promise { await bootWorkersInGroup(db, workers.storage, WorkerType.STORAGE) await bootWorkersInGroup(db, workers.gateway, WorkerType.GATEWAY) } export async function bootWorkersInGroup( db: DatabaseManager, workers: IBootstrapWorker[], workerType: WorkerType ): Promise { if (!workers) { return } for (const rawWorker of workers) { // create new membership const worker = new Worker({ // main data id: await getNextId(db), workerId: rawWorker.id, type: workerType, isActive: true, createdAt: new Date(rawWorker.created_at), updatedAt: new Date(rawWorker.created_at), }) // save worker await db.save(worker) // emit log event logger.info('Worker has been bootstrapped', { id: rawWorker.id, workerType }) } }