balena-supervisor/src/app.coffee

74 lines
2.2 KiB
CoffeeScript
Raw Normal View History

process.on 'uncaughtException', (e) ->
console.error('Got unhandled exception', e, e?.stack)
Promise = require 'bluebird'
2013-12-17 00:33:42 +00:00
knex = require './db'
utils = require './utils'
bootstrap = require './bootstrap'
config = require './config'
2015-01-14 03:05:23 +00:00
knex.init.then ->
utils.mixpanelTrack('Supervisor start')
2015-01-14 03:05:23 +00:00
console.log('Starting connectivity check..')
utils.connectivityCheck()
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'
device = require './device'
randomstring = require 'randomstring'
2015-01-14 03:05:23 +00:00
console.log('Starting API server..')
secret = config.forceApiSecret ? randomstring.generate()
api(secret).listen(config.listenPort)
# Let API know what version we are, and our api connection info.
console.log('Updating supervisor version and api info')
device.updateState(
api_port: config.listenPort
api_secret: secret
supervisor_version: utils.supervisorVersion
provisioning_progress: null
provisioning_state: ''
download_progress: null
)
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) ->
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)
application.update()
2015-01-14 03:05:23 +00:00
updateIpAddr = ->
utils.findIpAddrs().then (ipAddrs) ->
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()