2014-08-07 06:45:19 -04:00
|
|
|
Settings = require('settings-sharelatex')
|
|
|
|
logger = require('logger-sharelatex')
|
2016-06-07 12:58:18 -04:00
|
|
|
Keys = require('./UpdateKeys')
|
2014-10-07 07:08:36 -04:00
|
|
|
redis = require("redis-sharelatex")
|
2018-04-27 10:45:28 -04:00
|
|
|
Errors = require("./Errors")
|
2014-10-07 07:08:36 -04:00
|
|
|
|
2014-08-07 06:45:19 -04:00
|
|
|
UpdateManager = require('./UpdateManager')
|
2014-08-07 08:16:11 -04:00
|
|
|
Metrics = require('./Metrics')
|
2017-05-26 07:10:57 -04:00
|
|
|
RateLimitManager = require('./RateLimitManager')
|
2014-08-07 06:45:19 -04:00
|
|
|
|
2014-08-11 09:16:05 -04:00
|
|
|
module.exports = DispatchManager =
|
2017-05-26 07:10:57 -04:00
|
|
|
createDispatcher: (RateLimiter) ->
|
2017-05-03 11:27:32 -04:00
|
|
|
client = redis.createClient(Settings.redis.realtime)
|
2014-08-07 06:45:19 -04:00
|
|
|
worker = {
|
|
|
|
client: client
|
2014-08-11 09:16:05 -04:00
|
|
|
_waitForUpdateThenDispatchWorker: (callback = (error) ->) ->
|
2014-08-07 08:16:11 -04:00
|
|
|
timer = new Metrics.Timer "worker.waiting"
|
2014-08-07 06:45:19 -04:00
|
|
|
worker.client.blpop "pending-updates-list", 0, (error, result) ->
|
2014-08-07 08:16:11 -04:00
|
|
|
timer.done()
|
2014-08-07 06:45:19 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
return callback() if !result?
|
|
|
|
[list_name, doc_key] = result
|
|
|
|
[project_id, doc_id] = Keys.splitProjectIdAndDocId(doc_key)
|
2014-08-11 09:16:05 -04:00
|
|
|
# Dispatch this in the background
|
2017-05-26 07:10:57 -04:00
|
|
|
backgroundTask = (cb) ->
|
|
|
|
UpdateManager.processOutstandingUpdatesWithLock project_id, doc_id, (error) ->
|
2018-04-27 10:45:28 -04:00
|
|
|
# log everything except OpRangeNotAvailable errors, these are normal
|
2018-04-27 11:03:46 -04:00
|
|
|
if error?
|
|
|
|
# downgrade OpRangeNotAvailable and "Delete component" errors so they are not sent to sentry
|
2018-04-30 04:56:01 -04:00
|
|
|
logAsWarning = (error instanceof Errors.OpRangeNotAvailableError) || ((typeof error is' string') && error.match(/^Delete component/))
|
2018-04-27 11:03:46 -04:00
|
|
|
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 07:10:57 -04:00
|
|
|
cb()
|
|
|
|
RateLimiter.run backgroundTask, callback
|
2014-08-07 06:45:19 -04:00
|
|
|
|
|
|
|
run: () ->
|
|
|
|
return if Settings.shuttingDown
|
2014-08-11 09:16:05 -04:00
|
|
|
worker._waitForUpdateThenDispatchWorker (error) =>
|
2014-08-07 06:45:19 -04:00
|
|
|
if error?
|
2014-08-11 09:16:05 -04:00
|
|
|
logger.error err: error, "Error in worker process"
|
|
|
|
throw error
|
2014-08-07 06:45:19 -04:00
|
|
|
else
|
|
|
|
worker.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
return worker
|
|
|
|
|
2014-08-11 09:16:05 -04:00
|
|
|
createAndStartDispatchers: (number) ->
|
2017-05-26 07:10:57 -04:00
|
|
|
RateLimiter = new RateLimitManager(number)
|
2014-08-07 06:45:19 -04:00
|
|
|
for i in [1..number]
|
2017-05-26 07:10:57 -04:00
|
|
|
worker = DispatchManager.createDispatcher(RateLimiter)
|
2017-05-24 06:47:06 -04:00
|
|
|
worker.run()
|