2022-03-29 10:25:25 +00:00
|
|
|
import defaults from './defaults.mjs'
|
|
|
|
|
2020-09-08 07:53:42 +00:00
|
|
|
export function safeWrap(log, name, fn) {
|
|
|
|
return function(data, cb) {
|
|
|
|
try {
|
|
|
|
let out = fn(data, cb)
|
|
|
|
if (out && out.then) {
|
|
|
|
out.then(function() {}, function(err) {
|
|
|
|
log.error(err, 'Unknown error in ' + name)
|
|
|
|
log.event.error('Unknown error occured in ' + name + ': ' + err.message)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
log.error(err, 'Unknown error in ' + name)
|
|
|
|
log.event.error('Unknown error occured in ' + name + ': ' + err.message)
|
|
|
|
}
|
|
|
|
}
|
2022-03-29 10:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getConfig(ctx) {
|
|
|
|
let merge = {
|
2022-04-02 20:21:46 +00:00
|
|
|
applications: [],
|
|
|
|
name: ctx.db.config.name || '',
|
|
|
|
title: ctx.db.config.title || '',
|
2022-03-29 10:25:25 +00:00
|
|
|
}
|
|
|
|
for (let app of ctx.core.applications) {
|
2022-04-02 20:13:35 +00:00
|
|
|
merge[app.name] = {
|
|
|
|
provider: app.config.provider || null,
|
|
|
|
url: app.config.url || null,
|
|
|
|
port: app.config.port || null,
|
|
|
|
scAllowStop: app.config.scAllowStop || null,
|
|
|
|
}
|
2022-03-29 10:25:25 +00:00
|
|
|
merge.applications.push(app.name)
|
|
|
|
}
|
2022-04-02 20:21:46 +00:00
|
|
|
return merge
|
2022-03-29 10:25:25 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 08:09:48 +00:00
|
|
|
export function getApp(ctx, name, action) {
|
|
|
|
let app = ctx.core.applicationMap.get(name)
|
|
|
|
|
|
|
|
if (!app) {
|
|
|
|
ctx.log.warn(`Invalid action ${action} on non-existing app ${name}`)
|
|
|
|
ctx.log.event.warn(`Invalid action ${action} on non-existing app ${name}`)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
if (action === 'start' && !app.config.scAllowStop && app.running) {
|
|
|
|
ctx.log.warn(`Invalid action ${action} on existing app ${name}`)
|
|
|
|
ctx.log.event.warn(`Invalid action ${action} on existing app ${name}`)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
const lastAction = {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export function stopSpam(ctx, action, name) {
|
|
|
|
let key = name + '.' + action
|
|
|
|
var d = new Date()
|
|
|
|
if (!lastAction[key]) {
|
|
|
|
lastAction[key] = d
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (d - lastAction[key] < 1000 * 60 * 1) {
|
|
|
|
ctx.log.warn(`${action} called too fast on ${name}`)
|
|
|
|
ctx.log.event.warn(`${action} called too fast on ${name}`)
|
|
|
|
return lastAction[key]
|
|
|
|
}
|
|
|
|
lastAction[key] = d
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-29 10:25:25 +00:00
|
|
|
export function getStatus(ctx) {
|
|
|
|
let status = {}
|
|
|
|
for (let app of ctx.core.applications) {
|
|
|
|
if (app.provider.static) {
|
|
|
|
status[app.name] = {
|
|
|
|
active: 'static',
|
|
|
|
latestInstalled: 'static',
|
|
|
|
updated: '',
|
|
|
|
running: app.running,
|
|
|
|
updating: false,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let appDb = ctx.db.data.core[app.name]
|
|
|
|
let active = ctx.db.get(appDb.versions, appDb.active)
|
|
|
|
let installed = ctx.db.get(appDb.versions, appDb.latestInstalled)
|
|
|
|
status[app.name] = {
|
|
|
|
active: active,
|
|
|
|
latestInstalled: installed,
|
|
|
|
updated: appDb.updater,
|
2024-04-06 17:36:35 +00:00
|
|
|
running: app.running || app.ctx.version,
|
2022-03-29 10:25:25 +00:00
|
|
|
updating: app.updating,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status
|
|
|
|
}
|