workers.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { DatabaseManager } from '@dzlzv/hydra-db-utils'
  2. import { Worker, WorkerType } from 'query-node'
  3. import {logger} from '../src/common'
  4. export interface IBootstrapWorkers {
  5. storage: IBootstrapWorker[]
  6. gateway: IBootstrapWorker[]
  7. }
  8. export interface IBootstrapWorker {
  9. id: string
  10. }
  11. export async function bootWorkers(db: DatabaseManager, workers: IBootstrapWorkers): Promise<void> {
  12. await bootWorkersInGroup(db, workers.storage, WorkerType.STORAGE)
  13. await bootWorkersInGroup(db, workers.gateway, WorkerType.GATEWAY)
  14. }
  15. export async function bootWorkersInGroup(db: DatabaseManager, workers: IBootstrapWorker[], workerType: WorkerType): Promise<void> {
  16. if (!workers) {
  17. return
  18. }
  19. for (const rawWorker of workers) {
  20. // create new membership
  21. const worker = new Worker({
  22. // main data
  23. workerId: rawWorker.id,
  24. type: workerType,
  25. isActive: true,
  26. })
  27. // save worker
  28. await db.save<Worker>(worker)
  29. // emit log event
  30. logger.info('Worker has been bootstrapped', {id: rawWorker.id, workerType})
  31. }
  32. }