Commit fa8c3c27 authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

software/js-drone: handle SIGTERM

parent 16d3c1ec
...@@ -26,7 +26,7 @@ md5sum = b1910416ffa6f2b6a84fd13cca6109ce ...@@ -26,7 +26,7 @@ md5sum = b1910416ffa6f2b6a84fd13cca6109ce
[main] [main]
filename = main.js filename = main.js
md5sum = 81e692d3edcfe666bb4d7505b3375d8d md5sum = 8bc77ebd49e8e6fe060de507bccc44d5
[pubsub] [pubsub]
filename = pubsub.js filename = pubsub.js
...@@ -34,4 +34,4 @@ md5sum = 1555496ad591a31a845f33488d5c335d ...@@ -34,4 +34,4 @@ md5sum = 1555496ad591a31a845f33488d5c335d
[worker] [worker]
filename = worker.js filename = worker.js
md5sum = 33f09ad15e70230054f00778e6f53f38 md5sum = ccbdd75b2a58e8d92d7cb990f2565b37
...@@ -8,8 +8,8 @@ import { ...@@ -8,8 +8,8 @@ import {
stopPubsub, stopPubsub,
takeOffAndWait takeOffAndWait
} from {{ json_module.dumps(qjs_wrapper) }}; } from {{ json_module.dumps(qjs_wrapper) }};
import { setTimeout, Worker } from "os"; import { Worker, SIGTERM, setTimeout, signal } from "os";
import { open, exit } from "std"; import { exit, open } from "std";
(function (arm, console, exit, open, scriptArgs, setTimeout, start, stop, (function (arm, console, exit, open, scriptArgs, setTimeout, start, stop,
stopPubsub, takeOffAndWait, Worker) { stopPubsub, takeOffAndWait, Worker) {
...@@ -37,6 +37,7 @@ import { open, exit } from "std"; ...@@ -37,6 +37,7 @@ import { open, exit } from "std";
worker = new Worker("{{ worker_script }}"); worker = new Worker("{{ worker_script }}");
function quit(is_a_drone, exit_code) { function quit(is_a_drone, exit_code) {
worker.onmessage = null;
stopPubsub(); stopPubsub();
if (is_a_drone) { if (is_a_drone) {
stop(); stop();
...@@ -44,10 +45,19 @@ import { open, exit } from "std"; ...@@ -44,10 +45,19 @@ import { open, exit } from "std";
exit(exit_code); exit(exit_code);
} }
function exitWorker(exit_code) {
worker.postMessage({
type: "exit",
code: exit_code
});
}
signal(SIGTERM, exitWorker.bind(null, 0));
function exitOnFail(ret, msg) { function exitOnFail(ret, msg) {
if (ret) { if (ret) {
console.log(msg); console.log(msg);
quit(1); exitWorker(1);
} }
} }
...@@ -87,7 +97,7 @@ import { open, exit } from "std"; ...@@ -87,7 +97,7 @@ import { open, exit } from "std";
// First argument must provide the user script path // First argument must provide the user script path
if (user_script === undefined) { if (user_script === undefined) {
console.log('Please provide the user_script path.'); console.log('Please provide the user_script path.');
quit(1); exitWorker(1);
} }
worker.postMessage({ worker.postMessage({
...@@ -144,7 +154,7 @@ import { open, exit } from "std"; ...@@ -144,7 +154,7 @@ import { open, exit } from "std";
quit(configuration.isADrone, e.data.exit); quit(configuration.isADrone, e.data.exit);
} else { } else {
console.log('Unsupported message type', type); console.log('Unsupported message type', type);
quit(configuration.isADrone, !configuration.isASimulation, 1); exitWorker(1);
} }
}; };
}(arm, console, exit, open, scriptArgs, setTimeout, start, stop, stopPubsub, }(arm, console, exit, open, scriptArgs, setTimeout, start, stop, stopPubsub,
......
...@@ -30,10 +30,10 @@ import { ...@@ -30,10 +30,10 @@ import {
setReadHandler, setReadHandler,
waitpid waitpid
} from "os"; } from "os";
import { evalScript, exit, fdopen, loadFile, open } from "std"; import { evalScript, fdopen, loadFile, open } from "std";
(function (Drone, SIGTERM, WNOHANG, Worker, close, console, evalScript, exec, (function (Drone, SIGTERM, WNOHANG, Worker, close, console, evalScript, exec,
exit, fdopen, getAltitude, getAltitudeRel, getInitialAltitude, fdopen, getAltitude, getAltitudeRel, getInitialAltitude,
getLatitude, getLongitude, getYaw, initPubsub, kill, loadFile, getLatitude, getLongitude, getYaw, initPubsub, kill, loadFile,
loiter, open, pipe, setAirspeed, setMessage, setReadHandler, loiter, open, pipe, setAirspeed, setMessage, setReadHandler,
setTargetCoordinates, triggerParachute, waitpid) { setTargetCoordinates, triggerParachute, waitpid) {
...@@ -55,13 +55,7 @@ import { evalScript, exit, fdopen, loadFile, open } from "std"; ...@@ -55,13 +55,7 @@ import { evalScript, exit, fdopen, loadFile, open } from "std";
//required to fly //required to fly
triggerParachute: triggerParachute, triggerParachute: triggerParachute,
drone_dict: {}, drone_dict: {},
exit: function (exit_code) { exit: exitWorker,
parent.postMessage({type: "exited", exit: exit_code});
parent.onmessage = null;
if (user_me.hasOwnProperty("onWebSocketMessage")) {
stopGwsocket();
}
},
getAltitudeAbs: getAltitude, getAltitudeAbs: getAltitude,
getCurrentPosition: function () { getCurrentPosition: function () {
return { return {
...@@ -89,7 +83,15 @@ import { evalScript, exit, fdopen, loadFile, open } from "std"; ...@@ -89,7 +83,15 @@ import { evalScript, exit, fdopen, loadFile, open } from "std";
}; };
conf_file.close(); conf_file.close();
function readMessage(rd) { function exitWorker(exit_code) {
if (user_me.hasOwnProperty("onWebSocketMessage")) {
stopGwsocket();
}
parent.postMessage({type: "exited", exit: exit_code});
parent.onmessage = null;
}
function readMessage(rd) {
function read4() { function read4() {
var b1, b2, b3, b4; var b1, b2, b3, b4;
b1 = rd.getByte(); b1 = rd.getByte();
...@@ -175,7 +177,7 @@ import { evalScript, exit, fdopen, loadFile, open } from "std"; ...@@ -175,7 +177,7 @@ import { evalScript, exit, fdopen, loadFile, open } from "std";
var script_content = loadFile(path); var script_content = loadFile(path);
if (script_content === null) { if (script_content === null) {
console.log("Failed to load user script " + path); console.log("Failed to load user script " + path);
exit(1); exitWorker(1);
} }
try { try {
evalScript( evalScript(
...@@ -183,7 +185,7 @@ import { evalScript, exit, fdopen, loadFile, open } from "std"; ...@@ -183,7 +185,7 @@ import { evalScript, exit, fdopen, loadFile, open } from "std";
); );
} catch (e) { } catch (e) {
console.log("Failed to evaluate user script", e); console.log("Failed to evaluate user script", e);
exit(1); exitWorker(1);
} }
execUserScript(null, user_me); execUserScript(null, user_me);
...@@ -200,7 +202,9 @@ import { evalScript, exit, fdopen, loadFile, open } from "std"; ...@@ -200,7 +202,9 @@ import { evalScript, exit, fdopen, loadFile, open } from "std";
function handleMainMessage(evt) { function handleMainMessage(evt) {
var type = evt.data.type, message, peer_id; var type = evt.data.type, message, peer_id;
if (type === "initPubsub") { switch (type) {
case "initPubsub":
initPubsub(configuration.numberOfDrone, configuration.numberOfSubscriber); initPubsub(configuration.numberOfDrone, configuration.numberOfSubscriber);
for (peer_id = 0; peer_id < configuration.numberOfDrone + configuration.numberOfSubscriber; peer_id++) { for (peer_id = 0; peer_id < configuration.numberOfDrone + configuration.numberOfSubscriber; peer_id++) {
peer_dict[peer_id] = new Drone(peer_id); peer_dict[peer_id] = new Drone(peer_id);
...@@ -210,10 +214,14 @@ import { evalScript, exit, fdopen, loadFile, open } from "std"; ...@@ -210,10 +214,14 @@ import { evalScript, exit, fdopen, loadFile, open } from "std";
} }
} }
parent.postMessage({type: "initialized"}); parent.postMessage({type: "initialized"});
} else if (type === "load") { break;
case "load":
loadUserScript(evt.data.path); loadUserScript(evt.data.path);
parent.postMessage({type: "loaded"}); parent.postMessage({type: "loaded"});
} else if (type === "update") { break;
case "update":
Object.entries(peer_dict).forEach(function ([id, peer]) { Object.entries(peer_dict).forEach(function ([id, peer]) {
message = peer.message; message = peer.message;
if (user_me.id !== Number(id) && message.length > 0) { if (user_me.id !== Number(id) && message.length > 0) {
...@@ -231,9 +239,15 @@ import { evalScript, exit, fdopen, loadFile, open } from "std"; ...@@ -231,9 +239,15 @@ import { evalScript, exit, fdopen, loadFile, open } from "std";
user_me.onUpdate(evt.data.timestamp); user_me.onUpdate(evt.data.timestamp);
} }
parent.postMessage({type: "updated"}); parent.postMessage({type: "updated"});
} else { break;
case "exit":
exitWorker(evt.data.code);
break;
default:
throw new Error("Unsupported message type", type); throw new Error("Unsupported message type", type);
} };
} }
parent.onmessage = function (evt) { parent.onmessage = function (evt) {
...@@ -243,11 +257,11 @@ import { evalScript, exit, fdopen, loadFile, open } from "std"; ...@@ -243,11 +257,11 @@ import { evalScript, exit, fdopen, loadFile, open } from "std";
// Catch all potential bug to exit the main process // Catch all potential bug to exit the main process
// if it occurs // if it occurs
console.log(error); console.log(error);
exit(1); exitWorker(1);
} }
}; };
}(Drone, SIGTERM, WNOHANG, Worker, close, console, evalScript, exec, }(Drone, SIGTERM, WNOHANG, Worker, close, console, evalScript, exec,
exit, fdopen, getAltitude, getAltitudeRel, getInitialAltitude, fdopen, getAltitude, getAltitudeRel, getInitialAltitude,
getLatitude, getLongitude, getYaw, initPubsub, kill, loadFile, getLatitude, getLongitude, getYaw, initPubsub, kill, loadFile,
loiter, open, pipe, setAirspeed, setMessage, setReadHandler, loiter, open, pipe, setAirspeed, setMessage, setReadHandler,
setTargetCoordinates, triggerParachute, waitpid)); setTargetCoordinates, triggerParachute, waitpid));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment