2014-11-12 23:07:54 +08:00
|
|
|
_ = require("lodash")
|
2014-11-13 22:03:42 +08:00
|
|
|
async = require("async")
|
2014-11-13 02:35:25 +08:00
|
|
|
redis = require("redis")
|
|
|
|
|
|
|
|
config = require("./config")
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger = require("./logger")
|
2014-11-12 23:07:54 +08:00
|
|
|
|
2014-11-13 04:27:21 +08:00
|
|
|
# The database of mappings. Uses the externalID as key because it changes less than
|
|
|
|
# the internal ID (e.g. the internalID can change for different meetings in the same
|
2014-11-13 22:03:42 +08:00
|
|
|
# room). Used always from memory, but saved to redis for persistence.
|
|
|
|
#
|
|
|
|
# Format:
|
|
|
|
# {
|
|
|
|
# externalMeetingID: {
|
|
|
|
# id: @id
|
|
|
|
# externalMeetingID: @exnternalMeetingID
|
|
|
|
# internalMeetingID: @internalMeetingID
|
|
|
|
# lastActivity: @lastActivity
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# Format on redis:
|
|
|
|
# * a SET "...:mappings" with all ids (not meeting ids, the object id)
|
|
|
|
# * a HASH "...:mapping:<id>" for each mapping with all its attributes
|
2014-11-12 23:07:54 +08:00
|
|
|
db = {}
|
2014-11-13 22:03:42 +08:00
|
|
|
nextID = 1
|
2014-11-12 23:07:54 +08:00
|
|
|
|
|
|
|
# A simple model to store mappings for meeting IDs.
|
2014-11-13 22:15:20 +08:00
|
|
|
module.exports = class IDMapping
|
2014-11-12 23:07:54 +08:00
|
|
|
|
2014-11-13 22:03:42 +08:00
|
|
|
constructor: ->
|
|
|
|
@id = null
|
|
|
|
@externalMeetingID = null
|
|
|
|
@internalMeetingID = null
|
|
|
|
@lastActivity = null
|
2017-09-08 01:27:03 +08:00
|
|
|
@redisClient = config.redis.client
|
2014-11-12 23:07:54 +08:00
|
|
|
|
2014-11-13 22:03:42 +08:00
|
|
|
save: (callback) ->
|
|
|
|
@redisClient.hmset config.redis.keys.mapping(@id), @toRedis(), (error, reply) =>
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger.error "Hook: error saving mapping to redis!", error, reply if error?
|
2014-11-13 22:03:42 +08:00
|
|
|
@redisClient.sadd config.redis.keys.mappings, @id, (error, reply) =>
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger.error "Hook: error saving mapping ID to the list of mappings!", error, reply if error?
|
2014-11-13 22:03:42 +08:00
|
|
|
|
|
|
|
db[@externalMeetingID] = this
|
|
|
|
callback?(error, db[@externalMeetingID])
|
|
|
|
|
|
|
|
destroy: (callback) ->
|
|
|
|
@redisClient.srem config.redis.keys.mappings, @id, (error, reply) =>
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger.error "Hook: error removing mapping ID from the list of mappings!", error, reply if error?
|
2014-11-13 22:03:42 +08:00
|
|
|
@redisClient.del config.redis.keys.mapping(@id), (error) =>
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger.error "Hook: error removing mapping from redis!", error if error?
|
2014-11-13 22:03:42 +08:00
|
|
|
|
|
|
|
if db[@externalMeetingID]
|
|
|
|
delete db[@externalMeetingID]
|
|
|
|
callback?(error, true)
|
|
|
|
else
|
|
|
|
callback?(error, false)
|
|
|
|
|
|
|
|
toRedis: ->
|
|
|
|
r =
|
|
|
|
"id": @id,
|
|
|
|
"internalMeetingID": @internalMeetingID
|
|
|
|
"externalMeetingID": @externalMeetingID
|
|
|
|
"lastActivity": @lastActivity
|
|
|
|
r
|
|
|
|
|
|
|
|
fromRedis: (redisData) ->
|
|
|
|
@id = parseInt(redisData.id)
|
|
|
|
@externalMeetingID = redisData.externalMeetingID
|
|
|
|
@internalMeetingID = redisData.internalMeetingID
|
|
|
|
@lastActivity = redisData.lastActivity
|
|
|
|
|
|
|
|
print: ->
|
|
|
|
JSON.stringify(@toRedis())
|
|
|
|
|
|
|
|
@addOrUpdateMapping = (internalMeetingID, externalMeetingID, callback) ->
|
2014-11-13 22:15:20 +08:00
|
|
|
mapping = new IDMapping()
|
2014-11-13 22:03:42 +08:00
|
|
|
mapping.id = nextID++
|
|
|
|
mapping.internalMeetingID = internalMeetingID
|
|
|
|
mapping.externalMeetingID = externalMeetingID
|
|
|
|
mapping.lastActivity = new Date().getTime()
|
|
|
|
mapping.save (error, result) ->
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger.info "IDMapping: added or changed meeting mapping to the list #{externalMeetingID}:", mapping.print()
|
2014-11-13 22:03:42 +08:00
|
|
|
callback?(error, result)
|
|
|
|
|
|
|
|
@removeMapping = (internalMeetingID, callback) ->
|
|
|
|
for external, mapping of db
|
|
|
|
if mapping.internalMeetingID is internalMeetingID
|
|
|
|
mapping.destroy (error, result) ->
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger.info "IDMapping: removing meeting mapping from the list #{external}:", mapping.print()
|
2014-11-13 22:03:42 +08:00
|
|
|
callback?(error, result)
|
2014-11-12 23:07:54 +08:00
|
|
|
|
|
|
|
@getInternalMeetingID = (externalMeetingID) ->
|
2014-11-13 22:03:42 +08:00
|
|
|
db[externalMeetingID].internalMeetingID
|
2014-11-12 23:07:54 +08:00
|
|
|
|
|
|
|
@getExternalMeetingID = (internalMeetingID) ->
|
2014-11-13 22:15:20 +08:00
|
|
|
mapping = IDMapping.findByInternalMeetingID(internalMeetingID)
|
2014-11-13 22:03:42 +08:00
|
|
|
mapping?.externalMeetingID
|
|
|
|
|
|
|
|
@findByInternalMeetingID = (internalMeetingID) ->
|
|
|
|
if internalMeetingID?
|
|
|
|
for external, mapping of db
|
|
|
|
if mapping.internalMeetingID is internalMeetingID
|
|
|
|
return mapping
|
2014-11-13 04:27:21 +08:00
|
|
|
null
|
2014-11-13 02:35:25 +08:00
|
|
|
|
2014-11-13 22:03:42 +08:00
|
|
|
@allSync = ->
|
|
|
|
arr = Object.keys(db).reduce((arr, id) ->
|
|
|
|
arr.push db[id]
|
|
|
|
arr
|
|
|
|
, [])
|
|
|
|
arr
|
|
|
|
|
|
|
|
# Sets the last activity of the mapping for `internalMeetingID` to now.
|
|
|
|
@reportActivity = (internalMeetingID) ->
|
2014-11-13 22:15:20 +08:00
|
|
|
mapping = IDMapping.findByInternalMeetingID(internalMeetingID)
|
2014-11-13 22:03:42 +08:00
|
|
|
if mapping?
|
|
|
|
mapping.lastActivity = new Date().getTime()
|
|
|
|
mapping.save()
|
|
|
|
|
|
|
|
# Checks all current mappings for their last activity and removes the ones that
|
|
|
|
# are "expired", that had their last activity too long ago.
|
|
|
|
@cleanup = ->
|
|
|
|
now = new Date().getTime()
|
2014-11-13 22:15:20 +08:00
|
|
|
all = IDMapping.allSync()
|
2014-11-13 22:03:42 +08:00
|
|
|
toRemove = _.filter(all, (mapping) ->
|
|
|
|
mapping.lastActivity < now - config.mappings.timeout
|
|
|
|
)
|
|
|
|
unless _.isEmpty(toRemove)
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger.info "IDMapping: expiring the mappings:", _.map(toRemove, (map) -> map.print())
|
2014-11-13 22:03:42 +08:00
|
|
|
toRemove.forEach (mapping) -> mapping.destroy()
|
|
|
|
|
|
|
|
# Initializes global methods for this model.
|
2014-11-13 02:35:25 +08:00
|
|
|
@initialize = (callback) ->
|
2014-11-13 22:15:20 +08:00
|
|
|
IDMapping.resync(callback)
|
|
|
|
IDMapping.cleanupInterval = setInterval(IDMapping.cleanup, config.mappings.cleanupInterval)
|
2014-11-13 02:35:25 +08:00
|
|
|
|
|
|
|
# Gets all mappings from redis to populate the local database.
|
|
|
|
# Calls `callback()` when done.
|
|
|
|
@resync = (callback) ->
|
2017-09-08 01:27:03 +08:00
|
|
|
client = config.redis.client
|
2014-11-13 22:03:42 +08:00
|
|
|
tasks = []
|
2014-11-13 02:35:25 +08:00
|
|
|
|
2014-11-13 22:03:42 +08:00
|
|
|
client.smembers config.redis.keys.mappings, (error, mappings) =>
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger.error "Hook: error getting list of mappings from redis", error if error?
|
2014-11-13 02:35:25 +08:00
|
|
|
|
2014-11-13 22:03:42 +08:00
|
|
|
mappings.forEach (id) =>
|
|
|
|
tasks.push (done) =>
|
|
|
|
client.hgetall config.redis.keys.mapping(id), (error, mappingData) ->
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger.error "Hook: error getting information for a mapping from redis", error if error?
|
2014-11-13 22:03:42 +08:00
|
|
|
|
|
|
|
if mappingData?
|
2014-11-13 22:15:20 +08:00
|
|
|
mapping = new IDMapping()
|
2014-11-13 22:03:42 +08:00
|
|
|
mapping.fromRedis(mappingData)
|
|
|
|
mapping.save (error, hook) ->
|
|
|
|
nextID = mapping.id + 1 if mapping.id >= nextID
|
|
|
|
done(null, mapping)
|
|
|
|
else
|
|
|
|
done(null, null)
|
|
|
|
|
|
|
|
async.series tasks, (errors, result) ->
|
2014-11-13 22:15:20 +08:00
|
|
|
mappings = _.map(IDMapping.allSync(), (m) -> m.print())
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger.info "IDMapping: finished resync, mappings registered:", mappings
|
2014-11-13 22:03:42 +08:00
|
|
|
callback?()
|