mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-01-29 15:44:13 +00:00
Application now emits events on status changes
This commit is contained in:
parent
a2e4bd6634
commit
6df6feb5ef
51
app.coffee
51
app.coffee
@ -4,6 +4,7 @@ async = require('async')
|
||||
bootstrap = require('./bootstrap')
|
||||
state = require('./state')
|
||||
settings = require('./settings')
|
||||
request = require('request')
|
||||
Application = require('./application')
|
||||
|
||||
console.log('Supervisor started..')
|
||||
@ -29,6 +30,56 @@ tasks = [
|
||||
callback()
|
||||
(callback) ->
|
||||
hakiApp = new Application(state.get('gitUrl'), '/home/haki/hakiapp', 'haki')
|
||||
|
||||
hakiApp.on 'pre-init', ->
|
||||
request(
|
||||
uri: "#{settings.API_ENDPOINT}/ewa/device?$filter=uuid eq '#{state.get('uuid')}'"
|
||||
method: 'PATCH'
|
||||
json:
|
||||
status: 'Initialising'
|
||||
)
|
||||
|
||||
hakiApp.on 'post-init', ->
|
||||
request(
|
||||
uri: "#{settings.API_ENDPOINT}/ewa/device?$filter=uuid eq '#{state.get('uuid')}'"
|
||||
method: 'PATCH'
|
||||
json:
|
||||
status: 'Idle'
|
||||
)
|
||||
|
||||
hakiApp.on 'pre-update', ->
|
||||
request(
|
||||
uri: "#{settings.API_ENDPOINT}/ewa/device?$filter=uuid eq '#{state.get('uuid')}'"
|
||||
method: 'PATCH'
|
||||
json:
|
||||
status: 'Updating'
|
||||
)
|
||||
|
||||
hakiApp.on 'post-update', (hash) ->
|
||||
request(
|
||||
uri: "#{settings.API_ENDPOINT}/ewa/device?$filter=uuid eq '#{state.get('uuid')}'"
|
||||
method: 'PATCH'
|
||||
json:
|
||||
status: 'Idle'
|
||||
commit: state.get('gitHash')
|
||||
)
|
||||
|
||||
hakiApp.on 'start', ->
|
||||
request(
|
||||
uri: "#{settings.API_ENDPOINT}/ewa/device?$filter=uuid eq '#{state.get('uuid')}'"
|
||||
method: 'PATCH'
|
||||
json:
|
||||
status: 'Running'
|
||||
)
|
||||
|
||||
hakiApp.on 'stop', ->
|
||||
request(
|
||||
uri: "#{settings.API_ENDPOINT}/ewa/device?$filter=uuid eq '#{state.get('uuid')}'"
|
||||
method: 'PATCH'
|
||||
json:
|
||||
status: 'Idle'
|
||||
)
|
||||
|
||||
if not state.get('appInitialised')
|
||||
console.log('Initialising app..')
|
||||
hakiApp.init((error) ->
|
||||
|
@ -1,9 +1,13 @@
|
||||
{spawn} = require('child_process')
|
||||
{getpwnam} = require('posix')
|
||||
async = require('async')
|
||||
{EventEmitter} = require('events')
|
||||
_ = require('lodash')
|
||||
state = require('./state')
|
||||
|
||||
class Application
|
||||
class Application extends EventEmitter
|
||||
constructor: (@repo, @path, @user) ->
|
||||
EventEmitter.call(this)
|
||||
@process = null
|
||||
@inprogress = false
|
||||
@queue = []
|
||||
@ -33,18 +37,24 @@ class Application
|
||||
(callback) =>
|
||||
spawn('git', ['remote', 'add', 'origin', @repo], @options).on('exit', callback).on('error', callback)
|
||||
]
|
||||
async.series(tasks, callback)
|
||||
@emit('pre-init')
|
||||
async.series(tasks, =>
|
||||
@emit('post-init')
|
||||
callback?(arguments...)
|
||||
)
|
||||
|
||||
_start: (callback) ->
|
||||
if not @process
|
||||
@process = spawn('foreman', ['start'], @options)
|
||||
@emit('start')
|
||||
callback?()
|
||||
|
||||
_stop: (callback) ->
|
||||
# Kill will return false if process has already died
|
||||
handler = =>
|
||||
@process = null
|
||||
callback?()
|
||||
@emit('stop')
|
||||
callback?(arguments...)
|
||||
|
||||
spawn('pkill', ['-TERM', '-P', @process.pid], @options).on('exit', handler).on('error', handler)
|
||||
|
||||
@ -62,6 +72,18 @@ class Application
|
||||
(callback) =>
|
||||
spawn('git', ['pull', 'origin', 'master'], @options).on('exit', callback).on('error', callback)
|
||||
|
||||
# Save the new commit hash
|
||||
(callback) =>
|
||||
options = _.clone(@options)
|
||||
delete options.stdio
|
||||
ps = spawn('git', ['rev-parse', 'HEAD'], options).on('close', callback).on('error', callback)
|
||||
|
||||
# The hash will always be on the first chunk as I/O buffers are always larger than 40 bytes
|
||||
ps.stdout.on('data', (hash) ->
|
||||
hash = '' + hash
|
||||
state.set('gitHash', hash.trim())
|
||||
)
|
||||
|
||||
# Install npm dependencies
|
||||
(callback) =>
|
||||
spawn('npm', ['install'], @options).on('exit', callback).on('error', callback)
|
||||
@ -73,7 +95,11 @@ class Application
|
||||
else
|
||||
callback()
|
||||
]
|
||||
async.series(tasks, callback)
|
||||
@emit('pre-update')
|
||||
async.series(tasks, =>
|
||||
@emit('post-update')
|
||||
callback?(arguments...)
|
||||
)
|
||||
|
||||
# These methods shouldn't be called in parallel, queue them if they conflict
|
||||
['start', 'stop', 'init', 'update'].forEach((method) ->
|
||||
|
@ -6,7 +6,8 @@
|
||||
"async": "~0.2.9",
|
||||
"request": "~2.22.0",
|
||||
"posix": "~1.0.2",
|
||||
"express": "~3.2.6"
|
||||
"express": "~3.2.6",
|
||||
"lodash": "~1.3.1"
|
||||
},
|
||||
"engines": [
|
||||
"node >= 0.10.x"
|
||||
|
Loading…
x
Reference in New Issue
Block a user