mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
273 lines
9.3 KiB
273 lines
9.3 KiB
2 years ago
|
import _ from 'lodash-es'
|
||
|
import util from 'node:util'
|
||
|
import getosSync from 'getos'
|
||
|
import os from 'node:os'
|
||
|
import filesize from 'filesize'
|
||
|
import path from 'node:path'
|
||
|
import fs from 'fs-extra'
|
||
|
import { DateTime } from 'luxon'
|
||
|
import { generateError, generateSuccess } from '../../helpers/graph.mjs'
|
||
7 years ago
|
|
||
2 years ago
|
const getos = util.promisify(getosSync)
|
||
|
|
||
|
export default {
|
||
7 years ago
|
Query: {
|
||
3 years ago
|
systemFlags () {
|
||
2 years ago
|
return WIKI.config.flags
|
||
6 years ago
|
},
|
||
3 years ago
|
async systemInfo () { return {} },
|
||
|
async systemExtensions () {
|
||
|
const exts = Object.values(WIKI.extensions.ext).map(ext => _.pick(ext, ['key', 'title', 'description', 'isInstalled', 'isInstallable']))
|
||
|
for (const ext of exts) {
|
||
5 years ago
|
ext.isCompatible = await WIKI.extensions.ext[ext.key].isCompatible()
|
||
|
}
|
||
|
return exts
|
||
3 years ago
|
},
|
||
2 years ago
|
async systemInstances () {
|
||
|
const instRaw = await WIKI.db.knex('pg_stat_activity')
|
||
|
.select([
|
||
|
'usename',
|
||
|
'client_addr',
|
||
|
'application_name',
|
||
|
'backend_start',
|
||
|
'state_change'
|
||
|
])
|
||
|
.where('datname', WIKI.db.knex.client.connectionSettings.database)
|
||
|
.andWhereLike('application_name', 'Wiki.js%')
|
||
|
const insts = {}
|
||
|
for (const inst of instRaw) {
|
||
|
const instId = inst.application_name.substring(10, 20)
|
||
|
const conType = inst.application_name.includes(':MAIN') ? 'main' : 'sub'
|
||
|
const curInst = insts[instId] ?? {
|
||
|
activeConnections: 0,
|
||
|
activeListeners: 0,
|
||
|
dbFirstSeen: inst.backend_start,
|
||
|
dbLastSeen: inst.state_change
|
||
|
}
|
||
|
insts[instId] = {
|
||
|
id: instId,
|
||
|
activeConnections: conType === 'main' ? curInst.activeConnections + 1 : curInst.activeConnections,
|
||
|
activeListeners: conType === 'sub' ? curInst.activeListeners + 1 : curInst.activeListeners,
|
||
|
dbUser: inst.usename,
|
||
|
dbFirstSeen: curInst.dbFirstSeen > inst.backend_start ? inst.backend_start : curInst.dbFirstSeen,
|
||
|
dbLastSeen: curInst.dbLastSeen < inst.state_change ? inst.state_change : curInst.dbLastSeen,
|
||
|
ip: inst.client_addr
|
||
|
}
|
||
|
}
|
||
|
return _.values(insts)
|
||
|
},
|
||
3 years ago
|
systemSecurity () {
|
||
|
return WIKI.config.security
|
||
2 years ago
|
},
|
||
|
async systemJobs (obj, args) {
|
||
2 years ago
|
const results = args.states?.length > 0 ?
|
||
2 years ago
|
await WIKI.db.knex('jobHistory').whereIn('state', args.states.map(s => s.toLowerCase())).orderBy('startedAt', 'desc') :
|
||
|
await WIKI.db.knex('jobHistory').orderBy('startedAt', 'desc')
|
||
2 years ago
|
return results.map(r => ({
|
||
|
...r,
|
||
|
state: r.state.toUpperCase()
|
||
|
}))
|
||
2 years ago
|
},
|
||
2 years ago
|
async systemJobsScheduled (obj, args) {
|
||
|
return WIKI.db.knex('jobSchedule').orderBy('task')
|
||
|
},
|
||
|
async systemJobsUpcoming (obj, args) {
|
||
|
return WIKI.db.knex('jobs').orderBy([
|
||
|
{ column: 'waitUntil', order: 'asc', nulls: 'first' },
|
||
|
{ column: 'createdAt', order: 'asc' }
|
||
|
])
|
||
5 years ago
|
}
|
||
6 years ago
|
},
|
||
3 years ago
|
Mutation: {
|
||
2 years ago
|
async cancelJob (obj, args, context) {
|
||
|
WIKI.logger.info(`Admin requested cancelling job ${args.id}...`)
|
||
|
try {
|
||
|
const result = await WIKI.db.knex('jobs')
|
||
|
.where('id', args.id)
|
||
|
.del()
|
||
|
if (result === 1) {
|
||
|
WIKI.logger.info(`Cancelled job ${args.id} [ OK ]`)
|
||
|
} else {
|
||
|
throw new Error('Job has already entered active state or does not exist.')
|
||
|
}
|
||
|
return {
|
||
2 years ago
|
operation: generateSuccess('Cancelled job successfully.')
|
||
2 years ago
|
}
|
||
|
} catch (err) {
|
||
|
WIKI.logger.warn(err)
|
||
2 years ago
|
return generateError(err)
|
||
2 years ago
|
}
|
||
|
},
|
||
2 years ago
|
async disconnectWS (obj, args, context) {
|
||
|
WIKI.servers.ws.disconnectSockets(true)
|
||
|
WIKI.logger.info('All active websocket connections have been terminated.')
|
||
|
return {
|
||
2 years ago
|
operation: generateSuccess('All websocket connections closed successfully.')
|
||
2 years ago
|
}
|
||
|
},
|
||
|
async installExtension (obj, args, context) {
|
||
|
try {
|
||
|
await WIKI.extensions.ext[args.key].install()
|
||
|
// TODO: broadcast ext install
|
||
|
return {
|
||
2 years ago
|
operation: generateSuccess('Extension installed successfully')
|
||
2 years ago
|
}
|
||
|
} catch (err) {
|
||
2 years ago
|
return generateError(err)
|
||
2 years ago
|
}
|
||
|
},
|
||
2 years ago
|
async retryJob (obj, args, context) {
|
||
|
WIKI.logger.info(`Admin requested rescheduling of job ${args.id}...`)
|
||
|
try {
|
||
|
const job = await WIKI.db.knex('jobHistory')
|
||
|
.where('id', args.id)
|
||
|
.first()
|
||
|
if (!job) {
|
||
|
throw new Error('No such job found.')
|
||
|
} else if (job.state === 'interrupted') {
|
||
|
throw new Error('Cannot reschedule a task that has been interrupted. It will automatically be retried shortly.')
|
||
|
} else if (job.state === 'failed' && job.attempt < job.maxRetries) {
|
||
|
throw new Error('Cannot reschedule a task that has not reached its maximum retry attempts.')
|
||
|
}
|
||
|
await WIKI.db.knex('jobs')
|
||
|
.insert({
|
||
|
id: job.id,
|
||
|
task: job.task,
|
||
|
useWorker: job.useWorker,
|
||
|
payload: job.payload,
|
||
|
retries: job.attempt,
|
||
|
maxRetries: job.maxRetries,
|
||
|
isScheduled: job.wasScheduled,
|
||
|
createdBy: WIKI.INSTANCE_ID
|
||
|
})
|
||
|
WIKI.logger.info(`Job ${args.id} has been rescheduled [ OK ]`)
|
||
|
return {
|
||
2 years ago
|
operation: generateSuccess('Job rescheduled successfully.')
|
||
2 years ago
|
}
|
||
|
} catch (err) {
|
||
|
WIKI.logger.warn(err)
|
||
2 years ago
|
return generateError(err)
|
||
2 years ago
|
}
|
||
|
},
|
||
3 years ago
|
async updateSystemFlags (obj, args, context) {
|
||
2 years ago
|
WIKI.config.flags = {
|
||
|
...WIKI.config.flags,
|
||
|
...args.flags
|
||
|
}
|
||
6 years ago
|
await WIKI.configSvc.applyFlags()
|
||
|
await WIKI.configSvc.saveToDb(['flags'])
|
||
|
return {
|
||
2 years ago
|
operation: generateSuccess('System Flags applied successfully')
|
||
5 years ago
|
}
|
||
5 years ago
|
},
|
||
3 years ago
|
async updateSystemSecurity (obj, args, context) {
|
||
|
WIKI.config.security = _.defaultsDeep(_.omit(args, ['__typename']), WIKI.config.security)
|
||
|
// TODO: broadcast config update
|
||
|
await WIKI.configSvc.saveToDb(['security'])
|
||
5 years ago
|
return {
|
||
2 years ago
|
operation: generateSuccess('System Security configuration applied successfully')
|
||
5 years ago
|
}
|
||
6 years ago
|
}
|
||
|
},
|
||
6 years ago
|
SystemInfo: {
|
||
5 years ago
|
configFile () {
|
||
6 years ago
|
return path.join(process.cwd(), 'config.yml')
|
||
|
},
|
||
5 years ago
|
cpuCores () {
|
||
5 years ago
|
return os.cpus().length
|
||
|
},
|
||
5 years ago
|
currentVersion () {
|
||
6 years ago
|
return WIKI.version
|
||
|
},
|
||
5 years ago
|
dbHost () {
|
||
3 years ago
|
return WIKI.config.db.host
|
||
6 years ago
|
},
|
||
2 years ago
|
dbVersion () {
|
||
|
return _.get(WIKI.db, 'knex.client.version', 'Unknown Version')
|
||
|
},
|
||
5 years ago
|
hostname () {
|
||
5 years ago
|
return os.hostname()
|
||
|
},
|
||
5 years ago
|
httpPort () {
|
||
|
return WIKI.servers.servers.http ? _.get(WIKI.servers.servers.http.address(), 'port', 0) : 0
|
||
|
},
|
||
|
httpRedirection () {
|
||
|
return _.get(WIKI.config, 'server.sslRedir', false)
|
||
|
},
|
||
|
httpsPort () {
|
||
|
return WIKI.servers.servers.https ? _.get(WIKI.servers.servers.https.address(), 'port', 0) : 0
|
||
|
},
|
||
2 years ago
|
isMailConfigured () {
|
||
|
return WIKI.config?.mail?.host?.length > 2
|
||
|
},
|
||
|
async isSchedulerHealthy () {
|
||
|
const results = await WIKI.db.knex('jobHistory').count('* as total').whereIn('state', ['failed', 'interrupted']).andWhere('startedAt', '>=', DateTime.utc().minus({ days: 1 }).toISO()).first()
|
||
|
return _.toSafeInteger(results?.total) === 0
|
||
|
},
|
||
5 years ago
|
latestVersion () {
|
||
6 years ago
|
return WIKI.system.updates.version
|
||
6 years ago
|
},
|
||
5 years ago
|
latestVersionReleaseDate () {
|
||
3 years ago
|
return DateTime.fromISO(WIKI.system.updates.releaseDate).toJSDate()
|
||
6 years ago
|
},
|
||
5 years ago
|
nodeVersion () {
|
||
5 years ago
|
return process.version.substr(1)
|
||
|
},
|
||
5 years ago
|
async operatingSystem () {
|
||
7 years ago
|
let osLabel = `${os.type()} (${os.platform()}) ${os.release()} ${os.arch()}`
|
||
|
if (os.platform() === 'linux') {
|
||
|
const osInfo = await getos()
|
||
|
osLabel = `${os.type()} - ${osInfo.dist} (${osInfo.codename || os.platform()}) ${osInfo.release || os.release()} ${os.arch()}`
|
||
|
}
|
||
6 years ago
|
return osLabel
|
||
|
},
|
||
|
async platform () {
|
||
6 years ago
|
const isDockerized = await fs.pathExists('/.dockerenv')
|
||
|
if (isDockerized) {
|
||
6 years ago
|
return 'docker'
|
||
6 years ago
|
}
|
||
6 years ago
|
return os.platform()
|
||
6 years ago
|
},
|
||
5 years ago
|
ramTotal () {
|
||
6 years ago
|
return filesize(os.totalmem())
|
||
|
},
|
||
5 years ago
|
sslDomain () {
|
||
3 years ago
|
return WIKI.config.ssl.enabled && WIKI.config.ssl.provider === 'letsencrypt' ? WIKI.config.ssl.domain : null
|
||
5 years ago
|
},
|
||
|
sslExpirationDate () {
|
||
3 years ago
|
return WIKI.config.ssl.enabled && WIKI.config.ssl.provider === 'letsencrypt' ? _.get(WIKI.config.letsencrypt, 'payload.expires', null) : null
|
||
5 years ago
|
},
|
||
|
sslProvider () {
|
||
|
return WIKI.config.ssl.enabled ? WIKI.config.ssl.provider : null
|
||
|
},
|
||
|
sslStatus () {
|
||
|
return 'OK'
|
||
|
},
|
||
|
sslSubscriberEmail () {
|
||
3 years ago
|
return WIKI.config.ssl.enabled && WIKI.config.ssl.provider === 'letsencrypt' ? WIKI.config.ssl.subscriberEmail : null
|
||
5 years ago
|
},
|
||
5 years ago
|
async upgradeCapable () {
|
||
5 years ago
|
return !_.isNil(process.env.UPGRADE_COMPANION)
|
||
5 years ago
|
},
|
||
|
workingDirectory () {
|
||
6 years ago
|
return process.cwd()
|
||
|
},
|
||
5 years ago
|
async groupsTotal () {
|
||
2 years ago
|
const total = await WIKI.db.groups.query().count('* as total').first()
|
||
5 years ago
|
return _.toSafeInteger(total.total)
|
||
6 years ago
|
},
|
||
5 years ago
|
async pagesTotal () {
|
||
2 years ago
|
const total = await WIKI.db.pages.query().count('* as total').first()
|
||
5 years ago
|
return _.toSafeInteger(total.total)
|
||
6 years ago
|
},
|
||
5 years ago
|
async usersTotal () {
|
||
2 years ago
|
const total = await WIKI.db.users.query().count('* as total').first()
|
||
5 years ago
|
return _.toSafeInteger(total.total)
|
||
5 years ago
|
},
|
||
|
async tagsTotal () {
|
||
2 years ago
|
const total = await WIKI.db.tags.query().count('* as total').first()
|
||
5 years ago
|
return _.toSafeInteger(total.total)
|
||
7 years ago
|
}
|
||
6 years ago
|
}
|
||
7 years ago
|
}
|