2014-08-07 06:45:19 -04:00
|
|
|
Settings = require('settings-sharelatex')
|
|
|
|
logger = require('logger-sharelatex')
|
|
|
|
Keys = require('./RedisKeyBuilder')
|
|
|
|
UpdateManager = require('./UpdateManager')
|
2014-08-07 08:16:11 -04:00
|
|
|
Metrics = require('./Metrics')
|
2014-09-26 12:22:02 -04:00
|
|
|
redis = require("redis-sharelatex")
|
2014-08-07 06:45:19 -04:00
|
|
|
|
2014-08-11 09:16:05 -04:00
|
|
|
module.exports = DispatchManager =
|
|
|
|
createDispatcher: () ->
|
2014-09-26 12:22:02 -04:00
|
|
|
client = redis.createClient(Settings.redis.web)
|
|
|
|
|
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
|
2014-08-07 06:45:19 -04:00
|
|
|
UpdateManager.processOutstandingUpdatesWithLock project_id, doc_id, (error) ->
|
|
|
|
logger.error err: error, project_id: project_id, doc_id: doc_id, "error processing update" if error?
|
2014-08-11 09:16:05 -04:00
|
|
|
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) ->
|
2014-08-07 06:45:19 -04:00
|
|
|
for i in [1..number]
|
2014-08-11 09:16:05 -04:00
|
|
|
worker = DispatchManager.createDispatcher()
|
2014-08-07 06:45:19 -04:00
|
|
|
worker.run()
|