overleaf/services/track-changes/fixdangling.coffee

55 lines
1.5 KiB
CoffeeScript
Raw Normal View History

2017-04-20 15:58:26 +00:00
Settings = require "settings-sharelatex"
logger = require "logger-sharelatex"
TrackChangesLogger = logger.initialize("track-changes").logger
async = require "async"
fs = require "fs"
request = require "request"
cli = require "cli"
mongojs = require "mongojs"
bson = require "bson"
db = mongojs(Settings.mongo.url, ["docs"])
ObjectId = mongojs.ObjectId
options = cli.parse({
port: ['p', 'port number for track changes', 'number'],
force: ['f', 'actually make the fix']
});
if cli.args.length < 1
console.log "fixdangling -p PORT file_of_doc_ids"
process.exit()
file = cli.args.pop()
doc_ids = fs.readFileSync(file).toString().trim().split("\n")
missing = 0
errored = 0
success = 0
fixDangling = (doc_id, callback) ->
# look up project id from doc id
db.docs.find {_id:ObjectId(doc_id)}, {project_id:1}, (err, result) ->
2017-04-24 08:49:11 +00:00
#console.log "doc_id", doc_id, "err", err, "result", result
2017-04-20 15:58:26 +00:00
if err?
errored++
return callback()
if !result? or result.length == 0
missing++
return callback()
2017-04-24 08:49:11 +00:00
project_id = result[0].project_id
2017-04-20 15:58:26 +00:00
console.log "found project_id", project_id, "for doc_id", doc_id
url = "http://localhost:#{options.port}/project/#{project_id}/doc/#{doc_id}/flush"
if options.force
request.post url, (err, response, body) ->
if err? then errored++ else success++
callback()
else
console.log "URL:", url
success++
callback()
async.eachSeries doc_ids, fixDangling, (err) ->
console.log "final result", err, "missing", missing, "errored", errored, "success", success
db.close()