# All files / src/api heartbeat.coffee

40% Statements 6/15
0% Branches 0/3
0% Functions 0/1
40% Lines 6/15

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    281x
    1x
    1x
    1x
    1x

    1x

     utils = require '../utils'
    server = require '../server'
    Mediator = require('../model/mediators').Mediator
    moment = require 'moment'
    Q = require 'q'

    exports.getHeartbeat = ->
      try
        uptime = Q.denodeify( server.getUptime )
        result = yield uptime

        mediators = yield Mediator.find().exec()
        for mediator in mediators
          if not result.mediators then result.mediators = {}

          if mediator._lastHeartbeat? and mediator._uptime? and
          # have we received a heartbeat within the last minute?
          moment().diff(mediator._lastHeartbeat, 'seconds') <= 60
            result.mediators[mediator.urn] = mediator._uptime

          else
            result.mediators[mediator.urn] = null

        result.now = Date.now()
        this.body = result
      catch e
        utils.logAndSetResponse this, 500, "Error: #{e}", 'error'