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