Switch to using a separate module for network checking.

This commit is contained in:
Pagan Gazzard 2015-01-06 14:32:57 +00:00 committed by Pablo Carranza Vélez
parent f0682b162a
commit ab44c79f62
3 changed files with 2 additions and 37 deletions

View File

@ -18,6 +18,7 @@
"knex": "~0.5.1",
"lodash": "~2.4.1",
"mixpanel": "0.0.20",
"network-checker": "git+ssh://git@bitbucket.org:rulemotion/network-checker.git#v0.0.1",
"ngrok": "~0.1.97",
"pubnub": "~3.6.4",
"request": "^2.51.0",

View File

@ -1,36 +0,0 @@
Promise = require 'bluebird'
request = Promise.promisifyAll require 'request'
# options: The url to check, or an object of request options.
# timeout: 10s
# gzip: true
exports.checkURL = checkURL = (options) ->
if typeof options is 'string'
options =
url: options
options.timeout ?= 10000
options.gzip ?= true
request
.getAsync(options)
.spread (response) ->
return response.statusCode in [ 200, 304 ]
.catch (e) ->
return false
# options: The url to monitor, or an object of:
# interval: The time between each check.
# checkURL options
# fn(bool connected): The function that will be called each time the state changes.
exports.monitorURL = (options, fn) ->
interval = options?.interval or 0
connectivityState = null # Used to prevent multiple messages when disconnected
_check = ->
checkURL(options)
.then (connected) ->
return if connected == connectivityState
connectivityState = connected
fn(connected)
return # Do not wait on fn if it returns a promise
.finally ->
setTimeout(_check, interval)
_check()

View File

@ -4,7 +4,7 @@ fs = Promise.promisifyAll require 'fs'
config = require './config'
mixpanel = require 'mixpanel'
request = require './request'
networkCheck = require './lib/network-check'
networkCheck = require 'network-checker'
blink = require('blinker')(config.ledFile)
utils = exports