rearchive-all-docs.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. if (startId) {
  56. const validator = new RegExp('^[0-9a-fA-F]{24}$')
  57. if (!validator.test(startId)) {
  58. console.error('Invalid object id')
  59. return
  60. }
  61. query._id = {
  62. $gt: ObjectId(startId)
  63. }
  64. console.log(`Starting from object ID ${startId}`)
  65. } else {
  66. console.log('No object id specified. Starting from the beginning.')
  67. }
  68. const results = (await getCollection('projects'))
  69. .find(query, { _id: 1 })
  70. .sort({ _id: 1 })
  71. let jobCount = 0
  72. // keep going until we run out of projects
  73. while (true) {
  74. // get a new project to run a job with
  75. const project = await results.next()
  76. // if there are no more projects, wait until all the jobs have finished and exit
  77. if (!project) {
  78. // eslint-disable-next-line no-unmodified-loop-condition
  79. while (jobCount) {
  80. await new Promise((resolve) => setTimeout(resolve, 50))
  81. }
  82. return
  83. }
  84. // wait until there are fewer than maxWorkers jobs running
  85. // eslint-disable-next-line no-unmodified-loop-condition
  86. while (jobCount >= maxWorkers) {
  87. await new Promise((resolve) => setTimeout(resolve, 50))
  88. }
  89. // start a new job in the background and then continue the loop
  90. ++jobCount
  91. worker(project._id)
  92. .then(() => --jobCount)
  93. .catch(() => {
  94. console.error('ERROR:', project._id)
  95. --jobCount
  96. })
  97. }
  98. }
  99. if (!module.parent) {
  100. rearchiveAllDocs()
  101. .then(() => {
  102. console.log('Finished!')
  103. process.exit(0)
  104. })
  105. .catch((err) => {
  106. console.error('Something went wrong:', err)
  107. process.exit(1)
  108. })
  109. }