This repository has been archived by the owner on Mar 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
[TESTING PURPOSE] test bad internal script #235
Draft
IAmMorrow
wants to merge
3
commits into
master
Choose a base branch
from
donotmerge-test-bad-internal-script
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?)