workers.ts 1.2 KB

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