peertube-plugin-livechat/server/lib/prosody/ctl.ts

98 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-04-14 13:26:00 +00:00
import { getProsodyFilePaths, writeProsodyConfig } from './config'
import * as fs from 'fs'
import * as util from 'util'
2021-04-13 15:13:41 +00:00
2021-04-14 13:26:00 +00:00
const exec = util.promisify(require('child_process').exec)
interface ProsodyRunning {
2021-04-13 16:00:45 +00:00
ok: boolean
messages: string[]
}
2021-04-14 13:26:00 +00:00
async function testProsodyRunning (options: RegisterServerOptions): Promise<ProsodyRunning> {
2021-04-13 15:13:41 +00:00
const { peertubeHelpers } = options
2021-04-14 13:26:00 +00:00
peertubeHelpers.logger.info('Checking if Prosody is running')
const result: ProsodyRunning = {
2021-04-13 16:00:45 +00:00
ok: false,
messages: []
}
2021-04-14 13:26:00 +00:00
const filePaths = await getProsodyFilePaths(options)
try {
await fs.promises.access(filePaths.pid, fs.constants.R_OK)
} catch (error) {
result.messages.push(`Pid file ${filePaths.pid} not found`)
return result
}
result.ok = true
return result
}
async function testProsodyCorrectlyRunning (options: RegisterServerOptions): Promise<ProsodyRunning> {
const { peertubeHelpers } = options
peertubeHelpers.logger.info('Checking if Prosody is correctly running')
const result = await testProsodyRunning(options)
if (!result.ok) { return result }
result.ok = false // more tests to come
2021-04-13 15:13:41 +00:00
// TODO
peertubeHelpers.logger.error('testProsodyCorrectlyRunning not implemented yet.')
2021-04-13 16:00:45 +00:00
return result
2021-04-13 15:13:41 +00:00
}
async function ensureProsodyRunning (options: RegisterServerOptions): Promise<void> {
const { peertubeHelpers, settingsManager } = options
const logger = peertubeHelpers.logger
const setting = await settingsManager.getSetting('chat-use-prosody')
if (!setting) {
logger.info('Prosody is not activated, we wont launch it')
return
}
const r = await testProsodyCorrectlyRunning(options)
2021-04-13 16:00:45 +00:00
if (r.ok) {
2021-04-14 13:26:00 +00:00
r.messages.forEach(m => logger.debug(m))
2021-04-13 15:13:41 +00:00
logger.info('Prosody is already running correctly')
return
}
2021-04-14 13:26:00 +00:00
logger.info('Prosody is not running correctly: ')
r.messages.forEach(m => logger.info(m))
2021-04-13 15:13:41 +00:00
// Shutting down...
await ensureProsodyNotRunning(options)
// writing the configuration file
await writeProsodyConfig(options)
2021-04-14 13:26:00 +00:00
const filePaths = await getProsodyFilePaths(options)
// launch prosody
logger.info('Going to launch prosody...')
await exec('prosody', {
cwd: filePaths.dir,
env: {
...process.env,
PROSODY_CONFIG: filePaths.config
}
})
2021-04-13 15:13:41 +00:00
// TODO: listen for kill signal and kill prosody?
}
async function ensureProsodyNotRunning (options: RegisterServerOptions): Promise<void> {
const { peertubeHelpers } = options
peertubeHelpers.logger.info('Checking if Prosody is running, and shutting it down if so')
// TODO: implement this.
peertubeHelpers.logger.error('ensureProsodyNotRunning not implemented yet.')
}
export {
2021-04-14 13:26:00 +00:00
testProsodyRunning,
2021-04-13 15:13:41 +00:00
testProsodyCorrectlyRunning,
ensureProsodyRunning,
ensureProsodyNotRunning
}