overleaf/services/document-updater/app/coffee/DispatchManager.coffee

56 lines
2.1 KiB
CoffeeScript
Raw Normal View History

Settings = require('settings-sharelatex')
logger = require('logger-sharelatex')
Keys = require('./UpdateKeys')
2014-10-07 11:08:36 +00:00
redis = require("redis-sharelatex")
Errors = require("./Errors")
2014-10-07 11:08:36 +00:00
UpdateManager = require('./UpdateManager')
Metrics = require('./Metrics')
2017-05-26 11:10:57 +00:00
RateLimitManager = require('./RateLimitManager')
module.exports = DispatchManager =
2017-05-26 11:10:57 +00:00
createDispatcher: (RateLimiter) ->
client = redis.createClient(Settings.redis.documentupdater)
worker = {
client: client
_waitForUpdateThenDispatchWorker: (callback = (error) ->) ->
timer = new Metrics.Timer "worker.waiting"
worker.client.blpop "pending-updates-list", 0, (error, result) ->
2019-02-07 16:27:52 +00:00
logger.log("getting pending-updates-list", error, result)
timer.done()
return callback(error) if error?
return callback() if !result?
[list_name, doc_key] = result
[project_id, doc_id] = Keys.splitProjectIdAndDocId(doc_key)
# Dispatch this in the background
2017-05-26 11:10:57 +00:00
backgroundTask = (cb) ->
UpdateManager.processOutstandingUpdatesWithLock project_id, doc_id, (error) ->
# log everything except OpRangeNotAvailable errors, these are normal
if error?
# downgrade OpRangeNotAvailable and "Delete component" errors so they are not sent to sentry
logAsWarning = (error instanceof Errors.OpRangeNotAvailableError) || (error instanceof Errors.DeleteMismatchError)
if logAsWarning
logger.warn err: error, project_id: project_id, doc_id: doc_id, "error processing update"
else
logger.error err: error, project_id: project_id, doc_id: doc_id, "error processing update"
2017-05-26 11:10:57 +00:00
cb()
RateLimiter.run backgroundTask, callback
run: () ->
return if Settings.shuttingDown
worker._waitForUpdateThenDispatchWorker (error) =>
if error?
logger.error err: error, "Error in worker process"
throw error
else
worker.run()
}
return worker
createAndStartDispatchers: (number) ->
2017-05-26 11:10:57 +00:00
RateLimiter = new RateLimitManager(number)
for i in [1..number]
2017-05-26 11:10:57 +00:00
worker = DispatchManager.createDispatcher(RateLimiter)
2017-05-24 10:47:06 +00:00
worker.run()