overleaf/services/real-time/app/coffee/DocumentUpdaterController.coffee

51 lines
2.2 KiB
CoffeeScript
Raw Normal View History

logger = require "logger-sharelatex"
settings = require 'settings-sharelatex'
redis = require("redis-sharelatex")
rclient = redis.createClient(settings.redis.documentupdater)
2015-12-01 11:05:49 +00:00
SafeJsonParse = require "./SafeJsonParse"
EventLogger = require "./EventLogger"
2015-11-30 15:40:03 +00:00
MESSAGE_SIZE_LOG_LIMIT = 1024 * 1024 # 1Mb
module.exports = DocumentUpdaterController =
# DocumentUpdaterController is responsible for updates that come via Redis
# Pub/Sub from the document updater.
listenForUpdatesFromDocumentUpdater: (io) ->
rclient.subscribe "applied-ops"
rclient.on "message", (channel, message) ->
DocumentUpdaterController._processMessageFromDocumentUpdater(io, channel, message)
_processMessageFromDocumentUpdater: (io, channel, message) ->
2015-12-01 11:05:49 +00:00
SafeJsonParse.parse message, (error, message) ->
if error?
logger.error {err: error, channel}, "error parsing JSON"
return
if message.op?
if message._id?
2019-03-21 14:56:31 +00:00
EventLogger.checkEventOrder("applied-ops", message._id, message)
2015-12-01 11:05:49 +00:00
DocumentUpdaterController._applyUpdateFromDocumentUpdater(io, message.doc_id, message.op)
else if message.error?
DocumentUpdaterController._processErrorFromDocumentUpdater(io, message.doc_id, message.error, message)
else if message.health_check?
logger.debug {message}, "got health check message in applied ops channel"
_applyUpdateFromDocumentUpdater: (io, doc_id, update) ->
for client in io.sockets.clients(doc_id)
if client.id == update.meta.source
logger.log doc_id: doc_id, version: update.v, source: update.meta?.source, "distributing update to sender"
client.emit "otUpdateApplied", v: update.v, doc: update.doc
else if !update.dup # Duplicate ops should just be sent back to sending client for acknowledgement
logger.log doc_id: doc_id, version: update.v, source: update.meta?.source, client_id: client.id, "distributing update to collaborator"
client.emit "otUpdateApplied", update
_processErrorFromDocumentUpdater: (io, doc_id, error, message) ->
for client in io.sockets.clients(doc_id)
2017-11-10 15:01:23 +00:00
logger.warn err: error, doc_id: doc_id, client_id: client.id, "error from document updater, disconnecting client"
client.emit "otUpdateError", error, message
client.disconnect()