-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
118 additions
and
20 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { dirname, resolve } from "path"; | ||
import { runAsWorker } from "synckit"; | ||
import { fileURLToPath } from "url"; | ||
import { Worker } from "worker_threads"; | ||
|
||
// When some error occurs in @astrojs/compiler, the worker may be terminated. | ||
// So calling @astrojs/compiler directly on this worker may hang calls via `synckit`. | ||
// This worker creates a child worker to process @astrojs/compiler, and if an error occurs in the child worker, | ||
// it terminates the child worker and recreates it to prevent hangs. | ||
// Related to https://github.com/ota-meshi/eslint-plugin-astro/issues/99 | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
const workerPath = resolve(__dirname, "./astrojs-compiler-worker.mjs"); | ||
let worker; | ||
let seq = 0; | ||
let processMap = new Map(); | ||
|
||
function onMessage({ id, result, error, properties }) { | ||
const proc = processMap.get(id); | ||
processMap.delete(id); | ||
|
||
if (error) { | ||
proc?.reject(Object.assign(error, properties)); | ||
terminateWorker(); | ||
} else { | ||
proc?.resolve(result); | ||
} | ||
} | ||
|
||
function terminateWorker() { | ||
if (worker) { | ||
worker.terminate(); | ||
worker = null; | ||
} | ||
} | ||
|
||
runAsWorker((method, ...args) => { | ||
const id = seq++; | ||
if (!worker) { | ||
worker = new Worker(workerPath, {}); | ||
worker.on("message", onMessage); | ||
worker.on("exit", terminateWorker); | ||
worker.on("error", terminateWorker); | ||
worker.unref(); | ||
} | ||
return new Promise((resolve, reject) => { | ||
processMap.set(id, { resolve, reject }); | ||
setTimeout(() => { | ||
if (processMap.has(id)) { | ||
processMap.delete(id); | ||
terminateWorker(); | ||
reject(new Error("Timeout")); | ||
} | ||
}, 3000); | ||
worker.postMessage({ id, method, args }); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,7 +1,33 @@ | ||
import { runAsWorker } from "synckit"; | ||
import * as compiler from "@astrojs/compiler"; | ||
import { parentPort } from "worker_threads"; | ||
|
||
runAsWorker((method, ...args) => { | ||
const result = compiler[method](...args); | ||
return result; | ||
}); | ||
try { | ||
parentPort.on("message", async ({ id, method, args }) => { | ||
let msg; | ||
try { | ||
msg = { id, result: await compiler[method](...args) }; | ||
} catch (error) { | ||
msg = { id, error, properties: extractProperties(error) }; | ||
} | ||
parentPort.postMessage(msg); | ||
}); | ||
} catch (error) { | ||
parentPort.on("message", ({ id }) => { | ||
parentPort.postMessage({ | ||
id, | ||
error, | ||
properties: extractProperties(error), | ||
}); | ||
}); | ||
} | ||
|
||
function extractProperties(object) { | ||
if (object && typeof object === "object") { | ||
const properties = {}; | ||
for (const key in object) { | ||
properties[key] = object[key]; | ||
} | ||
return properties; | ||
} | ||
return object; | ||
} |
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