mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-03-21 03:25:46 +00:00
Switch to using a separate module for network checking.
This commit is contained in:
parent
f0682b162a
commit
ab44c79f62
@ -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",
|
||||
|
@ -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()
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user