2015-01-14 17:01:27 +00:00
|
|
|
process.on 'uncaughtException', (e) ->
|
|
|
|
console.error('Got unhandled exception', e, e?.stack)
|
|
|
|
|
2013-12-14 05:18:20 +00:00
|
|
|
Promise = require 'bluebird'
|
2013-12-17 00:33:42 +00:00
|
|
|
knex = require './db'
|
2013-12-14 05:18:20 +00:00
|
|
|
utils = require './utils'
|
|
|
|
bootstrap = require './bootstrap'
|
2014-10-14 09:08:26 +00:00
|
|
|
config = require './config'
|
2013-12-14 05:18:20 +00:00
|
|
|
|
2015-01-14 03:05:23 +00:00
|
|
|
knex.init.then ->
|
|
|
|
utils.mixpanelTrack('Supervisor start')
|
2014-08-14 21:32:03 +00:00
|
|
|
|
2015-01-14 03:05:23 +00:00
|
|
|
console.log('Starting connectivity check..')
|
|
|
|
utils.connectivityCheck()
|
2014-08-14 21:32:03 +00:00
|
|
|
|
2015-01-14 03:05:23 +00:00
|
|
|
knex('config').select('value').where(key: 'uuid').then ([ uuid ]) ->
|
|
|
|
if not uuid?.value
|
|
|
|
console.log('New device detected. Bootstrapping..')
|
|
|
|
retryingBootstrap = ->
|
|
|
|
utils.mixpanelTrack('Device bootstrap')
|
|
|
|
bootstrap().catch (err) ->
|
|
|
|
utils.mixpanelTrack('Device bootstrap failed, retrying', {error: err, delay: config.bootstrapRetryDelay})
|
|
|
|
Promise.delay(config.bootstrapRetryDelay)
|
|
|
|
.then(retryingBootstrap)
|
|
|
|
retryingBootstrap()
|
|
|
|
else
|
|
|
|
uuid.value
|
|
|
|
.then (uuid) ->
|
|
|
|
# Persist the uuid in subsequent metrics
|
|
|
|
utils.mixpanelProperties.uuid = uuid
|
2014-06-18 16:54:36 +00:00
|
|
|
|
2015-01-14 03:05:23 +00:00
|
|
|
api = require './api'
|
|
|
|
application = require './application'
|
2015-07-20 20:02:37 +00:00
|
|
|
device = require './device'
|
2015-01-28 15:13:26 +00:00
|
|
|
randomstring = require 'randomstring'
|
2013-12-14 05:18:20 +00:00
|
|
|
|
2015-01-14 03:05:23 +00:00
|
|
|
console.log('Starting API server..')
|
2015-07-24 20:59:58 +00:00
|
|
|
|
2015-07-28 21:10:17 +00:00
|
|
|
secret = config.forceApiSecret ? randomstring.generate()
|
2015-09-02 12:12:11 +00:00
|
|
|
api(secret).listen(config.listenPort)
|
2015-02-04 13:14:30 +00:00
|
|
|
|
|
|
|
# Let API know what version we are, and our api connection info.
|
|
|
|
console.log('Updating supervisor version and api info')
|
2015-07-20 20:02:37 +00:00
|
|
|
device.updateState(
|
2015-01-15 20:03:00 +00:00
|
|
|
api_port: config.listenPort
|
2015-01-28 15:13:26 +00:00
|
|
|
api_secret: secret
|
2015-02-04 13:14:30 +00:00
|
|
|
supervisor_version: utils.supervisorVersion
|
2015-02-07 14:51:26 +00:00
|
|
|
provisioning_progress: null
|
|
|
|
provisioning_state: ''
|
2015-04-07 16:18:28 +00:00
|
|
|
download_progress: null
|
2015-01-15 20:03:00 +00:00
|
|
|
)
|
2015-08-26 13:40:22 +00:00
|
|
|
|
2015-01-14 03:05:23 +00:00
|
|
|
console.log('Starting Apps..')
|
|
|
|
knex('app').select()
|
|
|
|
.then (apps) ->
|
2015-08-12 20:24:18 +00:00
|
|
|
Promise.all(apps.map(application.startAndUnlock))
|
2015-01-14 03:05:23 +00:00
|
|
|
.catch (error) ->
|
|
|
|
console.error('Error starting apps:', error)
|
|
|
|
.then ->
|
|
|
|
utils.mixpanelTrack('Start application update poll', {interval: config.appUpdatePollInterval})
|
|
|
|
setInterval(->
|
|
|
|
application.update()
|
|
|
|
, config.appUpdatePollInterval)
|
2013-12-29 18:07:58 +00:00
|
|
|
application.update()
|
2014-05-10 16:27:25 +00:00
|
|
|
|
2015-01-14 03:05:23 +00:00
|
|
|
updateIpAddr = ->
|
|
|
|
utils.findIpAddrs().then (ipAddrs) ->
|
2015-07-20 20:02:37 +00:00
|
|
|
device.updateState(
|
2015-01-14 03:05:23 +00:00
|
|
|
ip_address: ipAddrs.join(' ')
|
|
|
|
)
|
|
|
|
console.log('Starting periodic check for IP addresses..')
|
|
|
|
setInterval(updateIpAddr, 30 * 1000) # Every 30s
|
|
|
|
updateIpAddr()
|