WIP on prosody spawning.
This commit is contained in:
@ -10,7 +10,10 @@ interface ProsodyRunning {
|
||||
messages: string[]
|
||||
}
|
||||
|
||||
async function prosodyCtl (options: RegisterServerOptions, command: string): Promise<string> {
|
||||
async function prosodyCtl (options: RegisterServerOptions, command: string, failOnError: boolean): Promise<string> {
|
||||
const logger = options.peertubeHelpers.logger
|
||||
logger.debug('Calling prosodyCtl with command ' + command)
|
||||
|
||||
const filePaths = await getProsodyFilePaths(options)
|
||||
if (!/^\w+$/.test(command)) {
|
||||
throw new Error(`Invalid prosodyctl command '${command}'`)
|
||||
@ -37,9 +40,10 @@ async function prosodyCtl (options: RegisterServerOptions, command: string): Pro
|
||||
e += data as string
|
||||
})
|
||||
spawned.on('close', (code) => {
|
||||
if (code !== 0) {
|
||||
if (code !== 0 && failOnError) {
|
||||
reject(e)
|
||||
} else {
|
||||
if (e !== '') { d += e }
|
||||
resolve(d)
|
||||
}
|
||||
})
|
||||
@ -47,12 +51,14 @@ async function prosodyCtl (options: RegisterServerOptions, command: string): Pro
|
||||
}
|
||||
|
||||
async function getProsodyAbout (options: RegisterServerOptions): Promise<string> {
|
||||
return prosodyCtl(options, 'about')
|
||||
return prosodyCtl(options, 'about', true)
|
||||
}
|
||||
|
||||
async function testProsodyRunning (options: RegisterServerOptions): Promise<ProsodyRunning> {
|
||||
const { peertubeHelpers } = options
|
||||
peertubeHelpers.logger.info('Checking if Prosody is running')
|
||||
const logger = peertubeHelpers.logger
|
||||
logger.info('Checking if Prosody is running')
|
||||
|
||||
const result: ProsodyRunning = {
|
||||
ok: false,
|
||||
messages: []
|
||||
@ -60,9 +66,11 @@ async function testProsodyRunning (options: RegisterServerOptions): Promise<Pros
|
||||
|
||||
const filePaths = await getProsodyFilePaths(options)
|
||||
try {
|
||||
logger.debug('Trying to access the pid file')
|
||||
await fs.promises.access(filePaths.pid, fs.constants.R_OK)
|
||||
result.messages.push(`Pid file ${filePaths.pid} found`)
|
||||
} catch (error) {
|
||||
logger.debug(`Failed to access pid file: ${error as string}`)
|
||||
result.messages.push(`Pid file ${filePaths.pid} not found`)
|
||||
return result
|
||||
}
|
||||
@ -86,7 +94,9 @@ async function testProsodyCorrectlyRunning (options: RegisterServerOptions): Pro
|
||||
async function ensureProsodyRunning (options: RegisterServerOptions): Promise<void> {
|
||||
const { peertubeHelpers, settingsManager } = options
|
||||
const logger = peertubeHelpers.logger
|
||||
logger.debug('Calling ensureProsodyRunning')
|
||||
|
||||
logger.debug('Checking if prosody should be active')
|
||||
const setting = await settingsManager.getSetting('chat-use-prosody')
|
||||
if (!setting) {
|
||||
logger.info('Prosody is not activated, we wont launch it')
|
||||
@ -97,21 +107,24 @@ async function ensureProsodyRunning (options: RegisterServerOptions): Promise<vo
|
||||
if (r.ok) {
|
||||
r.messages.forEach(m => logger.debug(m))
|
||||
logger.info('Prosody is already running correctly')
|
||||
// Stop here. Nothing to change.
|
||||
return
|
||||
}
|
||||
logger.info('Prosody is not running correctly: ')
|
||||
r.messages.forEach(m => logger.info(m))
|
||||
|
||||
// Shutting down...
|
||||
logger.debug('Shutting down prosody')
|
||||
await ensureProsodyNotRunning(options)
|
||||
|
||||
// writing the configuration file
|
||||
logger.debug('Writing the configuration file')
|
||||
await writeProsodyConfig(options)
|
||||
|
||||
const filePaths = await getProsodyFilePaths(options)
|
||||
|
||||
// launch prosody
|
||||
logger.info('Going to launch prosody...')
|
||||
logger.info('Going to launch prosody')
|
||||
await exec('prosody', {
|
||||
cwd: filePaths.dir,
|
||||
env: {
|
||||
@ -119,16 +132,27 @@ async function ensureProsodyRunning (options: RegisterServerOptions): Promise<vo
|
||||
PROSODY_CONFIG: filePaths.config
|
||||
}
|
||||
})
|
||||
|
||||
// TODO: listen for kill signal and kill prosody?
|
||||
logger.info('Verifying prosody is launched')
|
||||
const status = await prosodyCtl(options, 'status', true)
|
||||
logger.info(`Prosody status: ${status}`)
|
||||
}
|
||||
|
||||
async function ensureProsodyNotRunning (options: RegisterServerOptions): Promise<void> {
|
||||
const { peertubeHelpers } = options
|
||||
peertubeHelpers.logger.info('Checking if Prosody is running, and shutting it down if so')
|
||||
const logger = peertubeHelpers.logger
|
||||
logger.info('Checking if Prosody is running, and shutting it down if so')
|
||||
|
||||
// TODO: implement this.
|
||||
peertubeHelpers.logger.error('ensureProsodyNotRunning not implemented yet.')
|
||||
// NB: this function is called on plugin unregister, even if prosody is not used
|
||||
// so we must avoid creating the working dir now
|
||||
const filePaths = await getProsodyFilePaths(options)
|
||||
if (!fs.existsSync(filePaths.dir)) {
|
||||
logger.info(`The working dir ${filePaths.dir} does not exist, assuming there is no prosody on this server`)
|
||||
return
|
||||
}
|
||||
|
||||
logger.debug('Calling prosodyctl to stop the process')
|
||||
const m = await prosodyCtl(options, 'stop', false)
|
||||
logger.info(`ProsodyCtl command returned: ${m}`)
|
||||
}
|
||||
|
||||
export {
|
||||
|
Reference in New Issue
Block a user