Parcourir la source

reload and stop commands, and log file

master
mortie il y a 7 ans
Parent
révision
c107c5bb5f
4 fichiers modifiés avec 106 ajouts et 9 suppressions
  1. 8
    3
      README.md
  2. 88
    6
      dedaemon.js
  3. 4
    0
      example.hcnf
  4. 6
    0
      js/parse-conf.js

+ 8
- 3
README.md Voir le fichier



## Usage ## Usage


`dedaemon <config file>`
```
dedaemon <config file> -- Start a new instance of dedaemon
dedaemon stop -- Stop all running istances of dedaemon
dedaemon reload -- Reload config file
```


e.g: e.g:


`dedaemon ~/.config/dedaemon.hcnf` `dedaemon ~/.config/dedaemon.hcnf`


You probably want to run that on startup. If you're running i3wm, that means You probably want to run that on startup. If you're running i3wm, that means
adding `exec --no-startup-id dedaemon ~/.config/dedaemon.hcnf` to
`~/.i3/config`.
adding `exec --no-startup-id dedaemon stop; dedaemon~/.config/dedaemon.hcnf` to
`~/.i3/config`. This first stop any running instance of dedaemon, then runs a
new one.


## Configuration ## Configuration



+ 88
- 6
dedaemon.js Voir le fichier

var async = require("./js/async"); var async = require("./js/async");
var udev = require("./udev"); var udev = require("./udev");


var execSync = require("child_process").execSync
var fs = require("fs");

var modules = { var modules = {
display: require("./modules/display"), display: require("./modules/display"),
input: require("./modules/input"), input: require("./modules/input"),


if (!process.argv[2]) { if (!process.argv[2]) {
console.error("Usage:", process.argv[1], "<config file>"); console.error("Usage:", process.argv[1], "<config file>");
console.error(" ", process.argv[1], "list");
console.error(" ", process.argv[1], "list -- List display and input devices");
console.error(" ", process.argv[1], "reload -- Reload the running daemon");
process.exit(1); process.exit(1);
} }


var config; var config;


var fatalError = false;
function createLogger(name) { function createLogger(name) {
function log(pre, msg) { function log(pre, msg) {
console.error(pre+msg.join(" "));
var str = pre+msg.join(" ");

console.error(str);
try {
fs.appendFileSync(config.general.log, str+"\n");
} catch (err) {
if (!fatalError) {
fatalError = true;
console.error("FATAL: Failed to write to log file!!");
console.error(err.toString());
stopAll(() => {
process.exit(1);
});
}
};
} }


return { return {
} }
} }


var logger = createLogger("dedaemon");

function startAll() { function startAll() {
try {
fs.renameSync(config.general.log, config.general.log+".old");
} catch (err) {}
fs.writeFileSync(config.general.log, "");

Object.keys(modules).forEach(i => { Object.keys(modules).forEach(i => {
var mod = modules[i]; var mod = modules[i];
var conf = config[i]; var conf = config[i];
} }


function onTerm() { function onTerm() {
console.error("Exiting...");
stopAll(() => process.exit(1));
udev.exit();
logger.info("Exiting...");
stopAll(() => {
udev.exit();
process.exit(1)
});
}

function reload() {
try {
config = parseConf(process.argv[2]);
} catch (err) {
logger.error(
"Tried to reload, but parsing the config file failed:",
err.toString());
return;
}
logger.info("Reloading.");

stopAll(() => {
startAll();
});
}

function loglol(sig) {
return function() {
logger.info("Received signal", sig);
}
}

function killRunningDaemon(signal, once) {
var cmd =
"pgrep -a node | "+
"grep dedaemon | "+
"grep -ve stop -e reload | "+
"cut -d' ' -f1";

var out = execSync(cmd)
.toString();

var lines = out.split("\n").filter(l => l !== "");
if (lines.length > 1 && once) {
console.error("There are multiple dedaemon processes running.");
process.exit(1);
}
if (lines.length === 0) {
console.error("No dedaemon process is running.");
process.exit(1);
}

lines.forEach(line => {
execSync("kill -s "+signal+" "+line);
console.error("Sent", "SIG"+signal, "to process", line);
});
process.exit(0);
} }


if (process.argv[2] === "list") { if (process.argv[2] === "list") {
process.exit(0); process.exit(0);
}); });
}); });
} else if (process.argv[2] === "reload") {
killRunningDaemon("USR1", true);
} else if (process.argv[2] === "stop") {
killRunningDaemon("TERM");
} else { } else {
var config = parseConf(process.argv[2]); var config = parseConf(process.argv[2]);


syscheck(ok => { syscheck(ok => {
if (ok) if (ok)
startAll();
setTimeout(startAll, 1000);
else else
console.error("Missing binaries, exiting."); console.error("Missing binaries, exiting.");
}); });


process.on("SIGTERM", onTerm); process.on("SIGTERM", onTerm);
process.on("SIGINT", onTerm); process.on("SIGINT", onTerm);
process.on("SIGUSR1", reload);
} }

+ 4
- 0
example.hcnf Voir le fichier

general {
log "$(HOME)/.dedaemon.log"
}

display * { display * {
mode max mode max
rate max rate max

+ 6
- 0
js/parse-conf.js Voir le fichier

module.exports = parse; module.exports = parse;


var configStructure = { var configStructure = {
general: {
count: "once",
props: {
log: "string",
},
},
display: { display: {
count: "many", count: "many",
props: { props: {

Chargement…
Annuler
Enregistrer