rearchive-all-docs.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const DocArchiveManager = require('../app/js/DocArchiveManager').promises
  2. const MongoManager = require('../app/js/MongoManager').promises
  3. const { getCollection, ObjectId } = require('../app/js/mongodb')
  4. const minimist = require('minimist')
  5. async function worker(projectId) {
  6. try {
  7. // see if the project needs to be unarchived, and unarchive it
  8. const archivedDocs = await MongoManager.getArchivedProjectDocs(projectId)
  9. if (archivedDocs.length) {
  10. await DocArchiveManager.unArchiveAllDocs(projectId)
  11. }
  12. // get the doc content so we can validate it
  13. const docs = await MongoManager.getProjectsDocs(
  14. projectId,
  15. { include_deleted: false },
  16. { lines: 1 }
  17. )
  18. // start archiving in the background while we check the content, if it was archived to begin with
  19. let archivePromise
  20. if (archivedDocs.length) {
  21. archivePromise = DocArchiveManager.archiveAllDocs(projectId)
  22. }
  23. let warning = false
  24. // validate the doc contents and log any warnings to investigate later
  25. for (const doc of docs) {
  26. if (!doc.lines) {
  27. warning = true
  28. console.error('WARN:', projectId, doc._id, 'has no content')
  29. }
  30. // eslint-disable-next-line no-control-regex
  31. if (doc.lines && doc.lines.some((line) => line.match(/(\r|\u0000)/))) {
  32. warning = true
  33. console.error('WARN:', projectId, doc._id, 'has invalid characters')
  34. }
  35. }
  36. // ensure the archive process has finished
  37. if (archivePromise) {
  38. await archivePromise
  39. }
  40. if (!warning) {
  41. // log to stderr along with the other output
  42. console.error('OK:', projectId)
  43. }
  44. } catch (err) {
  45. console.error('ERROR:', projectId, err)
  46. }
  47. }
  48. async function rearchiveAllDocs() {
  49. const params = minimist(process.argv.slice(2))
  50. const maxWorkers = params.w || 1
  51. console.log(`Starting with ${maxWorkers} workers`)
  52. // start from an objectId and run in ascending order, so we can resume later
  53. const query = {}
  54. const startId = params._[0]
  55. const endId = params.e
  56. if (startId) {
  57. if (!new RegExp('^[0-9a-fA-F]{24}$').test(startId)) {
  58. throw new Error('Invalid start object id')
  59. }
  60. query._id = {
  61. $gte: ObjectId(startId)
  62. }
  63. console.log(`Starting from object ID ${startId}`)
  64. } else {
  65. console.log('No object id specified. Starting from the beginning.')
  66. }
  67. if (endId) {
  68. if (!new RegExp('^[0-9a-fA-F]{24}$').test(endId)) {
  69. throw new Error('Invalid end object id')
  70. }
  71. query._id = query._id || {}
  72. query._id.$lte = ObjectId(endId)
  73. console.log(`Stopping at object ID ${endId}`)
  74. }
  75. const results = (await getCollection('projects'))
  76. .find(query, { _id: 1 })
  77. .sort({ _id: 1 })
  78. let jobCount = 0
  79. // keep going until we run out of projects
  80. while (true) {
  81. // get a new project to run a job with
  82. const project = await results.next()
  83. // if there are no more projects, wait until all the jobs have finished and exit
  84. if (!project) {
  85. // eslint-disable-next-line no-unmodified-loop-condition
  86. while (jobCount) {
  87. await new Promise((resolve) => setTimeout(resolve, 50))
  88. }
  89. return
  90. }
  91. // wait until there are fewer than maxWorkers jobs running
  92. // eslint-disable-next-line no-unmodified-loop-condition
  93. while (jobCount >= maxWorkers) {
  94. await new Promise((resolve) => setTimeout(resolve, 50))
  95. }
  96. // start a new job in the background and then continue the loop
  97. ++jobCount
  98. worker(project._id)
  99. .then(() => --jobCount)
  100. .catch(() => {
  101. console.error('ERROR:', project._id)
  102. --jobCount
  103. })
  104. }
  105. }
  106. if (!module.parent) {
  107. rearchiveAllDocs()
  108. .then(() => {
  109. console.log('Finished!')
  110. process.exit(0)
  111. })
  112. .catch((err) => {
  113. console.error('Something went wrong:', err)
  114. process.exit(1)
  115. })
  116. }