From f5b438d587f9977025fa8323c846a9883b8685b0 Mon Sep 17 00:00:00 2001 From: John Livingston Date: Wed, 14 Apr 2021 15:26:00 +0200 Subject: [PATCH] WIP. Spawning prosody. --- server/lib/prosody/config.ts | 4 ++- server/lib/prosody/ctl.ts | 56 +++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/server/lib/prosody/config.ts b/server/lib/prosody/config.ts index b12b816a..fd18d1f7 100644 --- a/server/lib/prosody/config.ts +++ b/server/lib/prosody/config.ts @@ -40,6 +40,7 @@ async function getWorkingDir ({ peertubeHelpers, storageManager }: RegisterServe } interface ProsodyFilePaths { + dir: string pid: string error: string log: string @@ -48,6 +49,7 @@ interface ProsodyFilePaths { async function getProsodyFilePaths (options: RegisterServerOptions): Promise { const dir = await getWorkingDir(options) return { + dir: dir, pid: path.resolve(dir, 'prosody.pid'), error: path.resolve(dir, 'prosody.err'), log: path.resolve(dir, 'prosody.log'), @@ -82,7 +84,7 @@ modules_disabled = { allow_registration = false -daemonize = true; +daemonize = false; pidfile = "${paths.pid}"; diff --git a/server/lib/prosody/ctl.ts b/server/lib/prosody/ctl.ts index a11c66a0..8caa9b45 100644 --- a/server/lib/prosody/ctl.ts +++ b/server/lib/prosody/ctl.ts @@ -1,23 +1,40 @@ -import { writeProsodyConfig } from './config' +import { getProsodyFilePaths, writeProsodyConfig } from './config' +import * as fs from 'fs' +import * as util from 'util' -interface ProsodyCorrectlyRunning { +const exec = util.promisify(require('child_process').exec) + +interface ProsodyRunning { ok: boolean messages: string[] } -/** - * @param options - * @returns true if prosody is running with up to date parameters. A string array of messages otherwise. - */ -async function testProsodyCorrectlyRunning (options: RegisterServerOptions): Promise { +async function testProsodyRunning (options: RegisterServerOptions): Promise { const { peertubeHelpers } = options - peertubeHelpers.logger.info('Checking if Prosody is correctly running') - const result: ProsodyCorrectlyRunning = { + peertubeHelpers.logger.info('Checking if Prosody is running') + const result: ProsodyRunning = { ok: false, messages: [] } - result.messages.push('Pid file not found') + 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 { + 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 // TODO peertubeHelpers.logger.error('testProsodyCorrectlyRunning not implemented yet.') @@ -36,18 +53,30 @@ async function ensureProsodyRunning (options: RegisterServerOptions): Promise logger.debug(m)) logger.info('Prosody is already running correctly') return } - logger.info('Prosody is not running correctly: ' + r.messages.join(', ')) + logger.info('Prosody is not running correctly: ') + r.messages.forEach(m => logger.info(m)) + // Shutting down... await ensureProsodyNotRunning(options) // writing the configuration file await writeProsodyConfig(options) - // TODO: launch prosody - logger.error('ensureProsodyRunning not implemented yet.') + 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 + } + }) // TODO: listen for kill signal and kill prosody? } @@ -61,6 +90,7 @@ async function ensureProsodyNotRunning (options: RegisterServerOptions): Promise } export { + testProsodyRunning, testProsodyCorrectlyRunning, ensureProsodyRunning, ensureProsodyNotRunning