2014-11-14 21:05:20 +08:00
|
|
|
_ = require('lodash')
|
2014-11-11 05:12:37 +08:00
|
|
|
request = require("request")
|
2014-11-14 21:05:20 +08:00
|
|
|
url = require('url')
|
|
|
|
EventEmitter = require('events').EventEmitter
|
2014-11-11 05:12:37 +08:00
|
|
|
|
2014-11-12 03:36:01 +08:00
|
|
|
config = require("./config")
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger = require("./logger")
|
2014-11-12 03:36:01 +08:00
|
|
|
Utils = require("./utils")
|
|
|
|
|
2014-11-13 04:07:29 +08:00
|
|
|
# Use to perform a callback. Will try several times until the callback is
|
2014-11-11 05:12:37 +08:00
|
|
|
# properly emitted and stop when successful (or after a given number of tries).
|
2014-11-13 04:07:29 +08:00
|
|
|
# Used to emit a single callback. Destroy it and create a new class for a new callback.
|
|
|
|
# Emits "success" on success, "failure" on error and "stopped" when gave up trying
|
|
|
|
# to perform the callback.
|
2014-11-11 05:12:37 +08:00
|
|
|
module.exports = class CallbackEmitter extends EventEmitter
|
|
|
|
|
2014-11-12 03:36:01 +08:00
|
|
|
constructor: (@callbackURL, @message) ->
|
2014-11-13 04:07:29 +08:00
|
|
|
@nextInterval = 0
|
|
|
|
@timestap = 0
|
2014-11-11 05:12:37 +08:00
|
|
|
|
|
|
|
start: ->
|
2014-11-14 21:05:20 +08:00
|
|
|
@timestamp = new Date().getTime()
|
2014-11-13 04:07:29 +08:00
|
|
|
@nextInterval = 0
|
2014-11-11 05:12:37 +08:00
|
|
|
@_scheduleNext 0
|
|
|
|
|
|
|
|
_scheduleNext: (timeout) ->
|
|
|
|
setTimeout( =>
|
|
|
|
@_emitMessage (error, result) =>
|
2014-11-13 04:07:29 +08:00
|
|
|
if not error? and result
|
2014-11-11 05:12:37 +08:00
|
|
|
@emit "success"
|
|
|
|
else
|
2014-11-13 04:07:29 +08:00
|
|
|
@emit "failure", error
|
|
|
|
|
|
|
|
# get the next interval we have to wait and schedule a new try
|
|
|
|
interval = config.hooks.retryIntervals[@nextInterval]
|
|
|
|
if interval?
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger.warn "xx> Trying the callback again in #{interval/1000.0} secs"
|
2014-11-13 04:07:29 +08:00
|
|
|
@nextInterval++
|
|
|
|
@_scheduleNext(interval)
|
|
|
|
|
|
|
|
# no intervals anymore, time to give up
|
|
|
|
else
|
|
|
|
@nextInterval = 0
|
|
|
|
@emit "stopped"
|
|
|
|
|
2014-11-11 05:12:37 +08:00
|
|
|
, timeout)
|
|
|
|
|
|
|
|
_emitMessage: (callback) ->
|
2014-11-14 21:05:20 +08:00
|
|
|
# data to be sent
|
|
|
|
# note: keep keys in alphabetical order
|
2014-11-12 03:36:01 +08:00
|
|
|
data =
|
2014-11-14 21:05:20 +08:00
|
|
|
event: JSON.stringify(@message)
|
2014-11-13 04:07:29 +08:00
|
|
|
timestamp: @timestamp
|
2014-11-12 03:36:01 +08:00
|
|
|
|
2014-11-14 21:05:20 +08:00
|
|
|
# calculate the checksum
|
2014-11-12 03:36:01 +08:00
|
|
|
checksum = Utils.checksum("#{@callbackURL}#{JSON.stringify(data)}#{config.bbb.sharedSecret}")
|
2014-11-14 21:05:20 +08:00
|
|
|
|
|
|
|
# get the final callback URL, including the checksum
|
|
|
|
urlObj = url.parse(@callbackURL, true)
|
|
|
|
callbackURL = @callbackURL
|
|
|
|
callbackURL += if _.isEmpty(urlObj.search) then "?" else "&"
|
|
|
|
callbackURL += "checksum=#{checksum}"
|
2014-11-11 05:12:37 +08:00
|
|
|
|
|
|
|
requestOptions =
|
2014-11-15 00:26:29 +08:00
|
|
|
followRedirect: true
|
|
|
|
maxRedirects: 10
|
2014-11-14 21:05:20 +08:00
|
|
|
uri: callbackURL
|
2014-11-11 05:12:37 +08:00
|
|
|
method: "POST"
|
2014-11-14 21:05:20 +08:00
|
|
|
form: data
|
2014-11-11 05:12:37 +08:00
|
|
|
|
|
|
|
request requestOptions, (error, response, body) ->
|
2014-11-24 23:01:13 +08:00
|
|
|
if error? or not (response?.statusCode >= 200 and response?.statusCode < 300)
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger.warn "xx> Error in the callback call to: [#{requestOptions.uri}] for #{simplifiedEvent(data.event)}"
|
|
|
|
Logger.warn "xx> Error:", error
|
2014-11-15 03:55:44 +08:00
|
|
|
Logger.warn "xx> Status:", response?.statusCode
|
2014-11-11 05:12:37 +08:00
|
|
|
callback error, false
|
|
|
|
else
|
2014-11-15 00:12:37 +08:00
|
|
|
Logger.info "==> Successful callback call to: [#{requestOptions.uri}] for #{simplifiedEvent(data.event)}"
|
2014-11-11 05:12:37 +08:00
|
|
|
callback null, true
|
2014-11-12 20:53:49 +08:00
|
|
|
|
|
|
|
# A simple string that identifies the event
|
|
|
|
simplifiedEvent = (event) ->
|
2014-11-14 21:05:20 +08:00
|
|
|
try
|
|
|
|
eventJs = JSON.parse(event)
|
|
|
|
"event: { name: #{eventJs.header?.name}, timestamp: #{eventJs.header?.timestamp} }"
|
|
|
|
catch e
|
|
|
|
"event: #{event}"
|