78 lines
1.8 KiB
JavaScript
78 lines
1.8 KiB
JavaScript
const {spawn, fork} = require('node:child_process')
|
|
const is_running = require('is-running')
|
|
const config = require('../../config')
|
|
const webrtc_server_path = config.webrtc_server_path
|
|
const app_args_static = config.app_args.static
|
|
const app_port_arg = config.app_args.runtime.port
|
|
const fs = require('fs')
|
|
|
|
const is_proc_running = (pid) => {
|
|
return is_running(pid)
|
|
}
|
|
|
|
const is_proc_running_async = async (pid, check_time) => {
|
|
var timeout_count = 0
|
|
var check_times = 10
|
|
|
|
while (true) {
|
|
++timeout_count
|
|
if (!is_proc_running(pid)) {
|
|
return false
|
|
}
|
|
if (timeout_count > check_times) {
|
|
return true
|
|
}
|
|
await new Promise(resolve => setTimeout(resolve, check_time / check_times))
|
|
}
|
|
}
|
|
|
|
const run_webrtc = async (webrtc_port, app_port) => {
|
|
if (!fs.existsSync(webrtc_server_path)) {
|
|
return null
|
|
}
|
|
|
|
var webrtc_proc
|
|
try {
|
|
webrtc_proc = fork(webrtc_server_path, [webrtc_port.toString(), app_port.toString()], {
|
|
detached: true
|
|
})
|
|
} catch (err) {
|
|
return null
|
|
}
|
|
|
|
//var proc_status = await is_proc_running_async(webrtc_proc.pid, 500)
|
|
|
|
return (webrtc_proc) ? webrtc_proc.pid : null
|
|
}
|
|
|
|
const run_app = async (app_path, app_port) => {
|
|
var app_proc
|
|
try {
|
|
app_proc = spawn(app_path, [].concat(app_args_static,
|
|
[app_port_arg + app_port.toString()]), {
|
|
detached: true
|
|
})
|
|
} catch (err) {
|
|
return null
|
|
}
|
|
|
|
return (app_proc) ? app_proc.pid : null
|
|
|
|
}
|
|
|
|
const kill_proc = (pid) => {
|
|
if (process.platform === 'win32') {
|
|
spawn('taskkill', ['/pid', pid, '/f', '/t'])
|
|
}
|
|
else {
|
|
process.kill(pid)
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
run_webrtc,
|
|
run_app,
|
|
is_proc_running,
|
|
is_proc_running_async,
|
|
kill_proc
|
|
} |