backup_blob.mjs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // @ts-check
  2. import commandLineArgs from 'command-line-args'
  3. import { backupBlob, downloadBlobToDir } from '../lib/backupBlob.mjs'
  4. import withTmpDir from '../../api/controllers/with_tmp_dir.js'
  5. import {
  6. BlobStore,
  7. GLOBAL_BLOBS,
  8. loadGlobalBlobs,
  9. } from '../lib/blob_store/index.js'
  10. import assert from '../lib/assert.js'
  11. import knex from '../lib/knex.js'
  12. import { client } from '../lib/mongodb.js'
  13. import { setTimeout } from 'node:timers/promises'
  14. import fs from 'node:fs'
  15. await loadGlobalBlobs()
  16. /**
  17. * Gracefully shutdown the process
  18. * @return {Promise<void>}
  19. */
  20. async function gracefulShutdown() {
  21. console.log('Gracefully shutting down')
  22. await knex.destroy()
  23. await client.close()
  24. await setTimeout(100)
  25. process.exit()
  26. }
  27. /**
  28. *
  29. * @param {string} row
  30. * @return {BackupBlobJob}
  31. */
  32. function parseCSVRow(row) {
  33. const [historyId, hash] = row.split(',')
  34. validateBackedUpBlobJob({ historyId, hash })
  35. return { historyId, hash }
  36. }
  37. /**
  38. *
  39. * @param {BackupBlobJob} job
  40. */
  41. function validateBackedUpBlobJob(job) {
  42. assert.projectId(job.historyId)
  43. assert.blobHash(job.hash)
  44. }
  45. /**
  46. *
  47. * @param {string} path
  48. * @return {Promise<Array<BackupBlobJob>>}
  49. */
  50. async function readCSV(path) {
  51. let fh
  52. /** @type {Array<BackupBlobJob>} */
  53. const rows = []
  54. try {
  55. fh = await fs.promises.open(path, 'r')
  56. } catch (error) {
  57. console.error(`Could not open file: ${error}`)
  58. throw error
  59. }
  60. for await (const line of fh.readLines()) {
  61. try {
  62. const row = parseCSVRow(line)
  63. if (GLOBAL_BLOBS.has(row.hash)) {
  64. console.log(`Skipping global blob: ${line}`)
  65. continue
  66. }
  67. rows.push(row)
  68. } catch (error) {
  69. console.error(error instanceof Error ? error.message : error)
  70. console.log(`Skipping invalid row: ${line}`)
  71. }
  72. }
  73. return rows
  74. }
  75. /**
  76. * @typedef {Object} BackupBlobJob
  77. * @property {string} hash
  78. * @property {string} historyId
  79. */
  80. /**
  81. * @param {Object} options
  82. * @property {string} [options.historyId]
  83. * @property {string} [options.hash]
  84. * @property {string} [options.input]
  85. * @return {Promise<Array<BackupBlobJob>>}
  86. */
  87. async function initialiseJobs({ historyId, hash, input }) {
  88. if (input) {
  89. return await readCSV(input)
  90. }
  91. if (!historyId) {
  92. console.error('historyId is required')
  93. process.exitCode = 1
  94. await gracefulShutdown()
  95. }
  96. if (!hash) {
  97. console.error('hash is required')
  98. process.exitCode = 1
  99. await gracefulShutdown()
  100. }
  101. validateBackedUpBlobJob({ historyId, hash })
  102. if (GLOBAL_BLOBS.has(hash)) {
  103. console.error(`Blob ${hash} is a global blob; not backing up`)
  104. process.exitCode = 1
  105. await gracefulShutdown()
  106. }
  107. return [{ hash, historyId }]
  108. }
  109. /**
  110. *
  111. * @param {string} historyId
  112. * @param {string} hash
  113. * @return {Promise<void>}
  114. */
  115. export async function downloadAndBackupBlob(historyId, hash) {
  116. const blobStore = new BlobStore(historyId)
  117. const blob = await blobStore.getBlob(hash)
  118. if (!blob) {
  119. throw new Error(`Blob ${hash} could not be loaded`)
  120. }
  121. await withTmpDir(`blob-${hash}`, async tmpDir => {
  122. const filePath = await downloadBlobToDir(historyId, blob, tmpDir)
  123. console.log(`Downloaded blob ${hash} to ${filePath}`)
  124. await backupBlob(historyId, blob, filePath)
  125. console.log('Backed up blob')
  126. })
  127. }
  128. let jobs
  129. const options = commandLineArgs([
  130. { name: 'historyId', type: String },
  131. { name: 'hash', type: String },
  132. { name: 'input', type: String },
  133. ])
  134. try {
  135. jobs = await initialiseJobs(options)
  136. } catch (error) {
  137. console.error(error)
  138. await gracefulShutdown()
  139. }
  140. if (!Array.isArray(jobs)) {
  141. // This is mostly to satisfy typescript
  142. process.exitCode = 1
  143. await gracefulShutdown()
  144. process.exit(1)
  145. }
  146. for (const { historyId, hash } of jobs) {
  147. try {
  148. await downloadAndBackupBlob(historyId, hash)
  149. } catch (error) {
  150. console.error(error)
  151. process.exitCode = 1
  152. }
  153. }
  154. await gracefulShutdown()