161 lines
4.1 KiB
JavaScript
161 lines
4.1 KiB
JavaScript
import defaults from '../defaults.mjs'
|
|
import { formatLog } from './loghelper.mjs'
|
|
import { getStatus } from '../util.mjs'
|
|
|
|
const stopSpam = {}
|
|
|
|
/*
|
|
* Event: 'core.restart'
|
|
*
|
|
* Restart server
|
|
*/
|
|
export async function restart(ctx, data, cb) {
|
|
if (ctx.db.config.allowRestart) {
|
|
ctx.core.restart()
|
|
} else {
|
|
ctx.log.fatal('Invalid core restart command')
|
|
ctx.log.event.error('Invalid core restart command')
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Event: 'core.getlastlogs'
|
|
*
|
|
* Returns last few log messages from log
|
|
*/
|
|
export async function getlastlogs(ctx, data, cb) {
|
|
if (data.name === 'service-core') {
|
|
return cb(ctx.core.log.ringbuffer.records.map(formatLog))
|
|
}
|
|
let app = ctx.core.applicationMap.get(data.name)
|
|
if (!app) {
|
|
ctx.log.warn('Invalid getlastlogs command for app ' + data.name)
|
|
ctx.log.event.warn('Invalid getlastlogs command for app ' + data.name)
|
|
return
|
|
}
|
|
|
|
cb(app.ctx.log.ringbuffer.records.map(formatLog))
|
|
}
|
|
|
|
/*
|
|
* Event: 'core.listenlogs'
|
|
*
|
|
* Start listening to new log lines
|
|
*/
|
|
export async function listenlogs(ctx, data) {
|
|
ctx.socket.join('logger.' + (data.name || 'service-core'))
|
|
}
|
|
|
|
/*
|
|
* Event: 'core.unlistenlogs'
|
|
*
|
|
* Stop listening to new log lines
|
|
*/
|
|
export async function unlistenlogs(ctx, data) {
|
|
ctx.socket.leave('logger.' + (data.name || 'service-core'))
|
|
}
|
|
|
|
/*
|
|
* Event: 'core.update'
|
|
*
|
|
* Update specific software
|
|
*/
|
|
export async function update(ctx, data, cb) {
|
|
let app = ctx.core.applicationMap.get(data.name)
|
|
if (!app) {
|
|
ctx.log.warn('Invalid update command for app ' + data.name)
|
|
ctx.log.event.warn('Invalid update command for app ' + data.name)
|
|
return
|
|
}
|
|
|
|
let d = new Date()
|
|
if (stopSpam[app.name] && d - stopSpam[app.name] < 1000 * 60 * 5) {
|
|
ctx.log.warn('Update called too fast for app ' + data.name)
|
|
ctx.log.event.warn('Update called too fast for app ' + data.name)
|
|
return
|
|
}
|
|
stopSpam[app.name] = d
|
|
|
|
ctx.log.info('Checking for updates on app ' + data.name)
|
|
app.update().then(function(res) {
|
|
ctx.log.info(res, 'Update completed on app ' + data.name)
|
|
}, function(err) {
|
|
ctx.log.err(err, 'Error checking for updates on app ' + data.name)
|
|
})
|
|
}
|
|
|
|
/*
|
|
* Event: 'core.start'
|
|
*
|
|
* Start specific software
|
|
*/
|
|
export async function start(ctx, data, cb) {
|
|
let app = ctx.core.applicationMap.get(data.name)
|
|
|
|
if (!app || (!app.config.scAllowStop && app.running)) {
|
|
ctx.log.warn('Invalid start command for app ' + data.name)
|
|
ctx.log.event.warn('Invalid start command for app ' + data.name)
|
|
return
|
|
}
|
|
|
|
let d = new Date()
|
|
if (app.running && stopSpam[app.name] && d - stopSpam[app.name] < 1000 * 60 * 5) {
|
|
ctx.log.warn('Update called too fast for app ' + data.name)
|
|
ctx.log.event.warn('Update called too fast for app ' + data.name)
|
|
return
|
|
}
|
|
stopSpam[app.name] = d
|
|
|
|
ctx.log.info('Checking for updates on app ' + data.name)
|
|
ctx.core.runApplication(app).then(function(res) {
|
|
ctx.log.info('Successfully started ' + data.name + ' running ' + app.running)
|
|
}, function(err) {
|
|
ctx.log.err(err, 'Error starting app ' + data.name)
|
|
})
|
|
}
|
|
|
|
/*
|
|
* Event: 'core.listentoapp'
|
|
*
|
|
* Start listening to changes in core application name
|
|
*/
|
|
export async function listentoapp(ctx, data) {
|
|
if (!data.name) {
|
|
ctx.log.warn(`listento called with missing name`)
|
|
return
|
|
}
|
|
|
|
let app = ctx.core.applicationMap.get(data.name)
|
|
|
|
if (!app) {
|
|
ctx.log.warn(`listento called on non-existing app ${data.name}`)
|
|
return
|
|
}
|
|
ctx.socket.join('app.' + data.name)
|
|
let version = ctx.db.get(ctx.db.data.core[app.name].versions, ctx.db.data.core[app.name].latestInstalled)
|
|
ctx.socket.emit('app.updatelog', {
|
|
name: data.name,
|
|
log: version?.log || ctx.db.data.core[app.name].updater
|
|
})
|
|
}
|
|
|
|
/*
|
|
* Event: 'core.unlistentoapp'
|
|
*
|
|
* Stop listening to new log lines
|
|
*/
|
|
export async function unlistentoapp(ctx, data) {
|
|
if (!data.name) {
|
|
ctx.log.warn(`unlistento called with missing name`)
|
|
return
|
|
}
|
|
|
|
let app = ctx.core.applicationMap.get(data.name)
|
|
|
|
if (!app) {
|
|
ctx.log.warn(`unlistento called on non-existing app ${data.name}`)
|
|
return
|
|
}
|
|
|
|
ctx.socket.leave('app.' + data.name)
|
|
}
|