Skip to content
This repository has been archived by the owner on Mar 19, 2020. It is now read-only.

[TESTING PURPOSE] test bad internal script #235

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import cluster from "cluster";

if (cluster.isMaster) {
if (!process.env.IS_INTERNAL_PROCESS) {
// Main electron thread
require("./main");
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/internal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import LoggerTransport from "~/logger/logger-transport-internal";

import { executeCommand, unsubscribeCommand, unsubscribeAllCommands } from "./commandHandler";

/* eslint-disable */
// $FlowFixMe
coucou_arnaud = 1;
Copy link
Contributor

@gre gre Feb 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IAmMorrow i'm not exactly sure if it's testing a bad case a boot (like a module failing to load). i think it would be better to do coucou_arnaud()

actually i'm still not sure to understand why it even crash anything (it's because we have some sort of script mode enabled?)


process.on("exit", () => {
logger.debug("exiting process, unsubscribing all...");
unsubscribeSetup();
Expand Down
146 changes: 146 additions & 0 deletions src/main/InternalProcess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { fork } from "child_process";
import logger from "~/logger";

class InternalProcess {
constructor({ timeout }) {
this.process = null;

this.timeout = timeout;
this.active = false;

this.onStartCallback = null;
this.onMessageCallback = null;
this.onExitCallback = null;

this.messageQueue = [];
}

onMessage(callback) {
this.onMessageCallback = callback;
}

onExit(callback) {
this.onExitCallback = callback;
}

run() {
while (this.messageQueue.length && this.active && this.process) {
const message = this.messageQueue.shift();
this.process.send(message);
}
}

send(message) {
this.messageQueue.push(message);
if (this.active) {
this.run();
}
}

onStart(callback) {
this.onStartCallback = callback;
}

configure(path, args, options) {
this.path = path;
this.args = args;
this.options = options;
}

start() {
if (this.process) {
throw new Error("Internal process is already running !");
}

this.process = fork(this.path, this.args, this.options);

this.active = true;
const pid = this.process.pid;

logger.warn(`spawned internal process ${pid}`);

this.process.on("exit", (code, signal) => {
this.process = null;

if (code !== null) {
console.log(`internal process ${pid} gracefully exited with code ${code}`);
logger.warn(`Internal process ended with code ${code}`);
} else {
console.log(`internal process ${pid} got killed by signal ${signal}`);
logger.warn(`Internal process killed with signal ${signal}`);
}

if (this.onExitCallback) {
this.onExitCallback(code, signal, this.active);
}
});

this.process.on("message", message => {
if (this.onMessageCallback) {
this.onMessageCallback(message);
}
});

if (this.messageQueue.length) {
this.run();
}

this.process.stdout.on("data", data =>
String(data)
.split("\n")
.forEach(msg => {
if (!msg) return;
if (process.env.INTERNAL_LOGS) console.log(msg);
try {
const obj = JSON.parse(msg);
if (obj && obj.type === "log") {
logger.onLog(obj.log);
return;
}
} catch (e) {}
logger.debug("I: " + msg);
}),
);
this.process.stderr.on("data", data => {
const msg = String(data).trim();
if (__DEV__) console.error("I.e: " + msg);
logger.error("I.e: " + String(data).trim());
});

if (this.onStartCallback) {
this.onStartCallback();
}
}

stop() {
return new Promise((resolve, reject) => {
if (!this.process) {
reject(new Error("Process not running"));
return;
}

this.messageQueue = [];
const pid = this.process.pid;

logger.warn(`ending process ${pid} ...`);
this.active = false;
this.process.once("exit", () => {
resolve();
});
this.process.disconnect();
setTimeout(() => {
if (this.process && this.process.pid === pid) {
this.process.kill("SIGINT");
}
}, this.timeout);
});
}

restart() {
return this.stop().then(() => {
this.start();
});
}
}

export default InternalProcess;
4 changes: 0 additions & 4 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ if (!gotLock) {
});
}

app.on("window-all-closed", () => {
app.quit();
});

app.on("activate", () => {
const w = getMainWindow();
if (w) {
Expand Down
Loading