balena-supervisor/src/app.coffee

55 lines
1.7 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'
request = require 'request'
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()
Promise.join bootstrap.startBootstrapping(), utils.getOrGenerateSecret('api'), utils.getOrGenerateSecret('logsChannel'), (uuid, secret, logsChannel) ->
2015-01-14 03:05:23 +00:00
# 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'
2015-10-28 23:02:00 +00:00
application = require('./application')(logsChannel)
device = require './device'
2015-09-01 17:11:46 +00:00
bootstrap.done
.then ->
console.log('Starting API server..')
api(application).listen(config.listenPort)
2015-09-01 17:13:01 +00:00
# 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-10-28 23:02:00 +00:00
logs_channel: logsChannel
2015-09-01 17:13:01 +00:00
)
2015-01-14 03:05:23 +00:00
console.log('Starting Apps..')
application.initialize()
2015-01-14 03:05:23 +00:00
updateIpAddr = ->
callback = (error, response, body ) ->
if !error && response.statusCode == 200
device.updateState(
ip_address: body.Data.IPAddresses.join(' ')
)
2015-09-16 15:35:13 +00:00
request.get({ url: "#{config.gosuperAddress}/v1/ipaddr", json: true }, callback )
2015-01-14 03:05:23 +00:00
console.log('Starting periodic check for IP addresses..')
setInterval(updateIpAddr, 30 * 1000) # Every 30s
updateIpAddr()