bigbluebutton-Github/labs/bbb-callback/meeting_id_map.coffee
Leonardo Crauss Daronco 5e141d324c Webhooks: better intervals for retying failed callbacks
Will try again a number of times for about 5min and then give up. On giving
up, the hook is removed.
And a few fixes for saving and loading data on redis.
2014-11-12 18:07:29 -02:00

55 lines
1.6 KiB
CoffeeScript

_ = require("lodash")
redis = require("redis")
config = require("./config")
# The database of mappings. Format:
# { internalMeetingID: externalMeetingID }
db = {}
# A simple model to store mappings for meeting IDs.
module.exports = class MeetingIDMap
@addOrUpdateMapping = (internalMeetingID, externalMeetingID) ->
db[internalMeetingID] = externalMeetingID
console.log "MeetingIDMap: added or changed meeting mapping to the list { #{internalMeetingID}: #{db[internalMeetingID]} }"
MeetingIDMap.updateRedis()
@removeMapping = (internalMeetingID) ->
if internalMeetingID in _.keys(db)
console.log "MeetingIDMap: removing meeting mapping from the list { #{internalMeetingID}: #{db[internalMeetingID]} }"
delete db[internalMeetingID]
db[internalMeetingID] = null
MeetingIDMap.updateRedis()
@getInternalMeetingID = (externalMeetingID) ->
for internal, external of db
if external is externalMeetingID
return internal
null
@getExternalMeetingID = (internalMeetingID) ->
db[internalMeetingID]
@initialize = (callback) ->
MeetingIDMap.resync(callback)
# Gets all mappings from redis to populate the local database.
# Calls `callback()` when done.
@resync = (callback) ->
client = redis.createClient()
client.hgetall config.redis.keys.mappings, (error, mappings) =>
MeetingIDMap.fromRedis(mappings)
callback?(error, mappings)
@fromRedis = (mappings) ->
if mappings?
db = mappings
else
db = {}
@updateRedis = (callback) ->
client = redis.createClient()
client.hmset config.redis.keys.mappings, db, (error, reply) =>
callback?(error)