2019-03-19 06:55:12 -04:00
|
|
|
logger = require 'logger-sharelatex'
|
2019-03-21 10:56:31 -04:00
|
|
|
metrics = require 'metrics-sharelatex'
|
2019-03-19 06:55:12 -04:00
|
|
|
|
|
|
|
# keep track of message counters to detect duplicate and out of order events
|
|
|
|
# messsage ids have the format "UNIQUEHOSTKEY-COUNTER"
|
|
|
|
|
|
|
|
EVENT_LOG_COUNTER = {}
|
|
|
|
EVENT_LOG_TIMESTAMP = {}
|
2019-03-21 10:48:51 -04:00
|
|
|
EVENT_LAST_CLEAN_TIMESTAMP = 0
|
2019-03-19 06:55:12 -04:00
|
|
|
|
|
|
|
module.exports = EventLogger =
|
|
|
|
|
|
|
|
MAX_STALE_TIME_IN_MS: 3600 * 1000
|
|
|
|
|
2019-03-21 10:56:31 -04:00
|
|
|
checkEventOrder: (channel, message_id, message) ->
|
2019-03-19 06:55:12 -04:00
|
|
|
return if typeof(message_id) isnt 'string'
|
|
|
|
[key, count] = message_id.split("-", 2)
|
|
|
|
count = parseInt(count, 10)
|
2019-03-21 10:49:25 -04:00
|
|
|
if !(count >= 0)# ignore checks if counter is not present
|
2019-03-19 06:55:12 -04:00
|
|
|
return
|
|
|
|
# store the last count in a hash for each host
|
|
|
|
previous = EventLogger._storeEventCount(key, count)
|
|
|
|
if !previous? || count == (previous + 1)
|
2019-03-21 11:52:53 -04:00
|
|
|
metrics.inc "event.#{channel}.valid", 0.001
|
2019-03-19 06:55:12 -04:00
|
|
|
return # order is ok
|
|
|
|
if (count == previous)
|
2019-03-21 10:56:31 -04:00
|
|
|
metrics.inc "event.#{channel}.duplicate"
|
|
|
|
# logger.error {key:key, previous: previous, count:count, message:message}, "duplicate event"
|
2019-03-19 06:55:12 -04:00
|
|
|
return "duplicate"
|
|
|
|
else
|
2019-03-21 10:56:31 -04:00
|
|
|
metrics.inc "event.#{channel}.out-of-order"
|
|
|
|
# logger.error {key:key, previous: previous, count:count, message:message}, "events out of order"
|
2019-03-19 06:55:12 -04:00
|
|
|
return # out of order
|
|
|
|
|
|
|
|
_storeEventCount: (key, count) ->
|
|
|
|
previous = EVENT_LOG_COUNTER[key]
|
|
|
|
now = Date.now()
|
|
|
|
EVENT_LOG_COUNTER[key] = count
|
|
|
|
EVENT_LOG_TIMESTAMP[key] = now
|
|
|
|
# periodically remove old counts
|
2019-03-21 10:48:51 -04:00
|
|
|
if (now - EVENT_LAST_CLEAN_TIMESTAMP) > EventLogger.MAX_STALE_TIME_IN_MS
|
2019-03-19 06:55:12 -04:00
|
|
|
EventLogger._cleanEventStream(now)
|
2019-03-21 10:48:51 -04:00
|
|
|
EVENT_LAST_CLEAN_TIMESTAMP = now
|
2019-03-19 06:55:12 -04:00
|
|
|
return previous
|
|
|
|
|
|
|
|
_cleanEventStream: (now) ->
|
|
|
|
for key, timestamp of EVENT_LOG_TIMESTAMP
|
|
|
|
if (now - timestamp) > EventLogger.MAX_STALE_TIME_IN_MS
|
|
|
|
delete EVENT_LOG_COUNTER[key]
|
|
|
|
delete EVENT_LOG_TIMESTAMP[key]
|