1_doc_lines.coffee 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. Settings = require "settings-sharelatex"
  2. fs = require("fs")
  3. mongojs = require("mongojs")
  4. ObjectId = mongojs.ObjectId
  5. db = mongojs(Settings.mongo.url, ['projects', 'docs'])
  6. _ = require("lodash")
  7. async = require("async")
  8. exec = require("child_process").exec
  9. finished_projects_path = "/tmp/finished-projects"
  10. all_projects_path = "/tmp/all-projects"
  11. printProgress = ->
  12. exec "wc #{finished_projects_path}", (error, results) ->
  13. #console.log results
  14. setTimeout printProgress, 1000 * 30
  15. checkIfFileHasBeenProccessed = (project_id, callback)->
  16. exec "grep #{project_id} #{finished_projects_path}", (error, results) ->
  17. hasBeenProcessed = _.include(results, project_id)
  18. #console.log hasBeenProcessed, project_id
  19. callback(error, hasBeenProcessed)
  20. loadProjectIds = (callback)->
  21. console.log "loading project ids from #{all_projects_path}"
  22. fs.readFile all_projects_path, "utf-8", (err, data)->
  23. ids = data.split("\n")
  24. console.log "loaded #{ids.length} project ids from #{all_projects_path}"
  25. callback err, ids
  26. getAndWriteProjectids = (callback)->
  27. console.log "finding all project id's - #{new Date().toString()}"
  28. db.projects.find {}, {_id:1}, (err, ids)->
  29. console.log "total found projects in mongo #{ids.length} - #{new Date().toString()}"
  30. ids = _.pluck ids, '_id'
  31. ids = _.filter ids, (id)-> id?
  32. fileData = ids.join("\n")
  33. fs.writeFile all_projects_path, fileData, ->
  34. callback(err, ids)
  35. getProjectIds = (callback)->
  36. exists = fs.existsSync all_projects_path
  37. if exists
  38. loadProjectIds callback
  39. else
  40. getAndWriteProjectids callback
  41. markProjectAsProcessed = (project_id, callback)->
  42. fs.appendFile finished_projects_path, "#{project_id}\n", callback
  43. getAllDocs = (project_id, callback = (error, docs) ->) ->
  44. db.projects.findOne _id:ObjectId(project_id), (error, project) ->
  45. return callback(error) if error?
  46. if !project?
  47. return callback("no such project #{project_id}")
  48. findAllDocsInProject project, (error, docs) ->
  49. return callback(error) if error?
  50. return callback null, docs
  51. findAllDocsInProject = (project, callback = (error, docs) ->) ->
  52. callback null, _findAllDocsInFolder project.rootFolder[0]
  53. _findDocInFolder = (folder = {}, doc_id, currentPath) ->
  54. for doc, i in folder.docs or []
  55. if doc?._id? and doc._id.toString() == doc_id.toString()
  56. return {
  57. doc: doc
  58. mongoPath: "#{currentPath}.docs.#{i}"
  59. }
  60. for childFolder, i in folder.folders or []
  61. result = _findDocInFolder childFolder, doc_id, "#{currentPath}.folders.#{i}"
  62. return result if result?
  63. return null
  64. _findAllDocsInFolder = (folder = {}) ->
  65. docs = folder.docs or []
  66. for childFolder in folder.folders or []
  67. docs = docs.concat _findAllDocsInFolder childFolder
  68. return docs
  69. insertDocIntoDocCollection = (project_id, doc_id, lines, oldRev, callback)->
  70. if !project_id?
  71. return callback("no project id")
  72. if !doc_id?
  73. return callback("no doc id. project=#{project_id}")
  74. if !lines?
  75. return callback("no lines")
  76. update = {}
  77. update["_id"] = ObjectId(doc_id.toString())
  78. update["lines"] = lines
  79. update["project_id"] = ObjectId(project_id)
  80. update["rev"] = oldRev || 0
  81. # console.log update
  82. db.docs.insert update, callback
  83. saveDocsIntoMongo = (project_id, docs, callback)->
  84. jobs = _.map docs, (doc)->
  85. (cb)->
  86. if !doc?
  87. console.error "null doc in project #{project_id}"
  88. return cb()
  89. insertDocIntoDocCollection project_id, doc._id, doc.lines, doc.rev, (err)->
  90. if err?.code == 11000 #duplicate key, doc already in there so its not a problem.
  91. err = undefined
  92. if err?
  93. console.log "error inserting doc into doc collection", err
  94. cb(err)
  95. async.series jobs, callback
  96. processNext = (project_id, callback)->
  97. checkIfFileHasBeenProccessed project_id, (err, hasBeenProcessed)->
  98. if hasBeenProcessed
  99. console.log "#{project_id} already procssed, skipping"
  100. return callback()
  101. console.log "#{project_id} processing"
  102. getAllDocs project_id, (err, docs)->
  103. if err?
  104. console.error err, project_id, "could not get all docs"
  105. return callback(err)
  106. else
  107. saveDocsIntoMongo project_id, docs, (err)->
  108. if err?
  109. console.error err, project_id, "could not save docs into mongo"
  110. return callback(err)
  111. markProjectAsProcessed project_id, (err)->
  112. setTimeout(
  113. -> callback(err)
  114. ,100)
  115. exports.migrate = (client, done)->
  116. getProjectIds (err, ids)->
  117. printProgress()
  118. jobs = _.map ids, (id)->
  119. return (cb)->
  120. processNext(id, cb)
  121. async.series jobs, (err)->
  122. if err?
  123. console.error err, "at end of jobs"
  124. else
  125. console.log "finished"
  126. done(err)
  127. exports.rollback = (next)->
  128. next()