sc-manager/api/core/ioroutes.mjs

128 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

import defaults from '../defaults.mjs'
2020-09-08 07:53:42 +00:00
import { formatLog } from './loghelper.mjs'
import { getApp, stopSpam } from '../util.mjs'
2020-09-08 07:53:42 +00:00
/*
* 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')
}
2020-09-08 07:53:42 +00:00
}
/*
* 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 = getApp(ctx, data.name, 'getlastlogs')
if (!app) return
cb(app.ctx.log.ringbuffer.records.map(formatLog))
2020-09-08 07:53:42 +00:00
}
/*
* Event: 'core.listenlogs'
*
* Start listening to new log lines
*/
export async function listenlogs(ctx, data) {
ctx.socket.join('logger.' + (data.name || 'service-core'))
2020-09-08 07:53:42 +00:00
}
/*
* Event: 'core.unlistenlogs'
*
* Stop listening to new log lines
*/
export async function unlistenlogs(ctx, data) {
ctx.socket.leave('logger.' + (data.name || 'service-core'))
2020-09-08 07:53:42 +00:00
}
/*
* Event: 'core.update'
*
* Update specific software
*/
export async function update(ctx, data, cb) {
let app = getApp(ctx, data.name, 'update')
if (!app) return
if (stopSpam(ctx, 'update', data.name)) return
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.error(err, 'Error checking for updates on app ' + data.name)
})
2020-09-08 07:53:42 +00:00
}
/*
* Event: 'core.start'
*
* Start specific software
*/
export async function start(ctx, data, cb) {
let app = getApp(ctx, data.name, 'start')
if (!app) return
if (stopSpam(ctx, 'start', data.name)) return
2020-09-08 07:53:42 +00:00
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.error(err, 'Error starting app ' + data.name)
})
2020-09-08 07:53:42 +00:00
}
/*
* Event: 'core.listentoapp'
*
* Start listening to changes in core application name
2020-09-08 07:53:42 +00:00
*/
export async function listentoapp(ctx, data) {
if (!data.name) {
ctx.log.warn(`listento called with missing name`)
return
}
2020-09-08 07:53:42 +00:00
let app = getApp(ctx, data.name, 'listentoapp')
if (!app) return
ctx.socket.join('app.' + data.name)
let version = ctx.db.data.core[app.name].versions[0]
ctx.socket.emit('app.updatelog', {
name: data.name,
log: version?.log || ctx.db.data.core[app.name].updater
})
}
2020-09-08 07:53:42 +00:00
/*
* 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 = getApp(ctx, data.name, 'unlistentoapp')
if (!app) return
ctx.socket.leave('app.' + data.name)
}