Skip to content

Commit c5cf937

Browse files
committed
Daemon support
1 parent 197b53a commit c5cf937

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

cli/daemon.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// https://wiki.unix7.org/node/daemon-sample
2+
var child_process = require('child_process');
3+
var { writeFileSync } = require('fs');
4+
5+
function child(exe, args, env) {
6+
var child = child_process.spawn(exe, args, {
7+
detached: true,
8+
stdio: ['ignore', 'ignore', 'ignore'],
9+
env: env
10+
})
11+
child.unref()
12+
return child
13+
}
14+
15+
module.exports = function(nodeBin) {
16+
console.log('Daemon PID :', process.pid);
17+
console.log('Daemon PPID:', process.ppid);
18+
console.log(`\nTo kill server, Run \"openradio-pulse -k\"`);
19+
writeFileSync(process.env.TMPDIR + "/openradio-pulse-daemon.json", JSON.stringify({ pid: process.pid, ppid: process.ppid }));
20+
21+
if (process.env.__daemon) {
22+
return process.pid
23+
}
24+
process.env.__daemon = true
25+
26+
var args = [].concat(process.argv)
27+
var node = args.shift()
28+
var env = process.env
29+
child(node, args, env)
30+
return process.exit()
31+
}

cli/pulse.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
const supportedPlatform = ["Linux"];
33
const { spawn } = require("child_process");
44
const openradio = require("../");
5+
const daemon = require("./daemon");
6+
const fs = require("fs");
57
const http = require("http");
68
const server = http.createServer();
79
const argv = process.argv.slice(2);
@@ -21,7 +23,8 @@ const config = {
2123
address: "0.0.0.0"
2224
},
2325
parec_path: process.env.PREFIX + "/bin/parec",
24-
log: 0
26+
log: 0,
27+
daemon: 1
2528
}
2629

2730
if (!supportedPlatform.includes(require("os").type()) && !["--force", "-force", "-f"].includes(argv[0])) {
@@ -30,9 +33,9 @@ if (!supportedPlatform.includes(require("os").type()) && !["--force", "-force",
3033
process.exit(1);
3134
}
3235

33-
console.log("\nOpenradio Pulseaudio - v1.0");
36+
console.log("\nOpenradio Pulseaudio - v1.1");
3437

35-
argv.forEach((key, index) => {
38+
argv.forEach(async (key, index) => {
3639
let value = argv[index+1];
3740
if (["--port", "-port", "-p"].includes(key)) {
3841
if (isNaN(value)) return console.error("Usage: openradio-pulse --port [Port Number]");
@@ -45,6 +48,9 @@ argv.forEach((key, index) => {
4548
console.log(" --parec-path [Path] - Path to parec binary (Default: $PREFIX/bin/parec)");
4649
console.log(" --help - Show this");
4750
console.log(" --log - Log every HTTP Traffic");
51+
console.log("\nDaemonize Service:\n");
52+
console.log(" --daemon - Do not run as Daemonize Service");
53+
console.log(" --kill - Kill Daemonize service");
4854
console.log("\nAudio Input Options:\n");
4955
console.log(" --input-samplerate [num] - Input Samplerate (Default: 44100)");
5056
console.log(" --input-channels [num] - Input Channels (Default: 2)");
@@ -77,10 +83,27 @@ argv.forEach((key, index) => {
7783
config.server.address = value;
7884
} else if (["--log", "-log", "-l", "-verbose", "--verbose", "-v"].includes(key)) {
7985
config.log = 1;
86+
} else if (["--daemon", "-daemon", "-d"].includes(key)) {
87+
config.daemon = 0;
88+
} else if (["--kill", "-kill", "-k"].includes(key)) {
89+
try {
90+
let daemons = JSON.parse(fs.readFileSync(process.env.TMPDIR + "/openradio-pulse-daemon.json"));
91+
console.log("Killing process " + daemons.pid + "....");
92+
process.kill(daemons.pid, 'SIGINT');
93+
fs.rmSync(process.env.TMPDIR + "/openradio-pulse-daemon.json");
94+
} catch (error) {
95+
if (error.code === "ENOENT") {
96+
console.error("No daemon was running or already killed.\nYou may should kill it manually by ran \"pkill -9 node\" if it's still running.");
97+
return process.exit(1);
98+
}
99+
console.error(error);
100+
return process.exit(1);
101+
}
102+
process.exit();
80103
}
81104
});
82105

83-
server.on('error', console.error);
106+
server.on('error', err => console.error(`[${Date()}]`, err));
84107
server.on('request', (req, res) => {
85108
let id = Math.random();
86109
let address = req.socket.address();
@@ -105,22 +128,31 @@ console.log("\n- HTTP Server");
105128
console.log(" Address :", config.server.address);
106129
console.log(" Port :", config.server.port);
107130
console.log("\nparec Binary path:", config.parec_path);
131+
if (config.daemon) daemon();
108132
console.log("Log Incomming Traffic:", config.log ? "Yes" : "No");
109-
110133
process.stdout.write(`\n[${Date()}] Launching Server.... `);
111134

112135
let listener = server.listen(config.server.port, config.server.address, () => {
113136
process.stdout.write("Done");
114137
console.log(`\n[${Date()}]`, "Now listening on port", listener.address().port);
115138

116139
let radio = openradio(config.output);
117-
radio.playPCM(spawn(config.parec_path, config.input).stdout);
140+
function play() {
141+
let parec = spawn(config.parec_path, config.input);
142+
parec.on('close', play);
143+
parec.on('error', err => console.error(`[${Date()}]`, err));
144+
parec.stderr.pipe(process.stderr);
145+
radio.playPCM(parec.stdout, config.input);
146+
}
147+
118148
radio.on('data', chunk => {
119149
sink.forEach((res, id) => {
120150
res.write(chunk, err => {
121151
if (err) sink.delete(id);
122152
});
123153
});
124154
});
155+
156+
play();
125157
});
126158

0 commit comments

Comments
 (0)