Comments (14)
@dmm-mkcvi did you fix?
from pkg.
I have a workaround, load the module as a file:
var file = fs.readFileSync(filepath,{ encoding: 'utf8', flag: 'r' });
and using:
new Worker(file ,{eval:true});
it will recognize the module, after, every require in the module not in node-modules should need to be passed as a script
It is working in production using this method but encrypting the module file first and decrypting it in run time only to not let the module code be in plain text
from pkg.
@dmm-mkcvi Yep,
I'm getting this error now "Error: Runtime exited with error: exit status 1
Runtime.ExitError"
I'm doing like this
var file = fs.readFileSync("./worker.ts",{ encoding: 'utf8', flag: 'r' });
console.log(file)
const worker = new Worker(file, {
workerData: { taskId },
eval: true
});
from pkg.
can you give stacktrace?
from pkg.
@dmm-mkcvi how to get stack trace in nodejs vercel? i only get error on production (Vercel) This Serverless Function has crashed.
error like
RequestId: 6304ca8a-256d-431a-91cf-8bc80718b875 Error: Runtime exited with error: exit status 1
Runtime.ExitError
from pkg.
i tried a simple test and it works fine
index.ts:
`const fs = require('fs');
var file = fs.readFileSync("./worker.ts",{ encoding: 'utf8', flag: 'r' });
const { Worker} = require('node:worker_threads');
function runService( worker) {
return new Promise((resolve, reject) => {
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0)
reject(new Error(Worker stopped with exit code ${code}
));
})
worker.postMessage({toworker:"run"});
})
}
async function run() {
console.log(file)
const worker = new Worker(file, {
eval: true
});
const result = await runService(worker)
console.log(result);
}
run().catch(err => console.error(err))`
worker.ts:
const { parentPort } = require('node:worker_threads'); parentPort.on('message', (task) => { parentPort.postMessage( {success:'1'}); });
from pkg.
@dmm-mkcvi Thanks can you please check my question on StackOverflow i have some code there https://stackoverflow.com/questions/77167599/how-can-i-manage-lengthy-task-functions-in-node-js-express-js
I'm just trying execute function in the background
from pkg.
@dmm-mkcvi Hi can you please check my code?
import fs from 'fs'
import { v4 as uuidv4 } from 'uuid';
import { Worker } from 'node:worker_threads';
api.ts
router.post('/run-long-task', async (req, res) => {
const taskId = uuidv4();
const worker = new Worker(file, {
workerData: { taskId },
eval: true
});
tasksById.set(taskId, {
status: 'running',
started: Date.now(),
});
worker.on('message', async () => {
try {
tasksById.set(taskId, { status: 'completed' });
} catch (error) {
tasksById.set(taskId, { status: 'error', error: error.message });
}
});
tasksById.set(taskId, {
status: "running",
started: Date.now(),
worker,
});
worker.on("message", (status) => {
tasksById.set(taskId, { status });
});
worker.on("error", (error) => {
tasksById.set(taskId, { status: "error", error: error.message });
});
worker.on("exit", (code) => {
if (code !== 0) {
if (tasksById.get(taskId)?.status === "running") {
tasksById.set(taskId, {
status: "error",
error: `Worker stopped with exit code ${code}`,
});
} else {
}
}
});
res.json({ taskId });
})
worker.ts:
import { parentPort } from "node:worker_threads";
async function performTask() {
// Long Task Function
}
if (parentPort) {
parentPort.on('message', (task) => {
performTask()
parentPort?.postMessage({ success: '1' });
});
}
from pkg.
Try with only on worker.on("message") and with worker.postMessage({toworker:"run"});
`router.post('/run-long-task', async (req, res) => {
const taskId = uuidv4();
const worker = new Worker(file, {
workerData: { taskId },
eval: true
});
tasksById.set(taskId, {
status: 'running',
started: Date.now(),
});
tasksById.set(taskId, {
status: "running",
started: Date.now(),
worker,
});
worker.on("message", (status) => {
tasksById.set(taskId, { status });
});
worker.on("error", (error) => {
tasksById.set(taskId, { status: "error", error: error.message });
});
worker.on("exit", (code) => {
if (code !== 0) {
if (tasksById.get(taskId)?.status === "running") {
tasksById.set(taskId, {
status: "error",
error: "Worker stopped with exit code ${code}",
});
} else {
}
}
});
worker.postMessage({toworker:"run"});
res.json({ taskId });
}`
from pkg.
@dmm-mkcvi Do you mean like this?
router.post('/run-long-task', async (req, res) => {
const taskId = uuidv4();
const worker = new Worker(file, {
workerData: { taskId },
eval: true
});
tasksById.set(taskId, {
status: 'running',
started: Date.now(),
});
tasksById.set(taskId, {
status: "running",
started: Date.now(),
worker,
});
worker.postMessage({toworker:"run"});
worker.on("message", (status) => {
tasksById.set(taskId, { status });
});
worker.on("error", (error) => {
tasksById.set(taskId, { status: "error", error: error.message });
});
worker.on("exit", (code) => {
if (code !== 0) {
if (tasksById.get(taskId)?.status === "running") {
tasksById.set(taskId, {
status: "error",
error: "Worker stopped with exit code ${code}",
});
} else {
}
}
});
worker.postMessage({ toworker: "run" });
res.json({ taskId });
})
from pkg.
@dmm-mkcvi Any idea about this?
from pkg.
@leo Can you please help?
from pkg.
This issue is stale because it has been open 90 days with no activity. Remove the stale label or comment or this will be closed in 5 days. To ignore this issue entirely you can add the no-stale label
from pkg.
This issue is now closed due to inactivity, you can of course reopen or reference this issue if you see fit.
from pkg.
Related Issues (20)
- Cannot build base binary for FreeBSD
- Latest arm64 macOS binary does not work when built on Ubuntu HOT 2
- there is something evil bug when we start a exe which is created by pkg
- Failed to compile Gluon applications correctly
- Issue #1962 should not be closed
- Not working with selenium-webdriver HOT 2
- Error! No available node version satisfies 'node20' HOT 4
- Deprecated subdependencies (babel, npmcli, ts-evaluator, and w3c-hr-time)
- fs.promises.access doesn't work in /snapshot HOT 2
- Error! Not more than one entry file/directory is expected HOT 1
- pkg can't use execFile correctly HOT 1
- notice pkg has a serious bug when we use child_process exec exec,execFile spawn HOT 1
- cpSync with recurse:true results in ENOENT
- at Pkg.usageError (/Users/usernameee/volta/tools/image/npm/10.2.4/lib/base-command.js:113:26)
- Failed to launch chrome browser from selenium webdriver in exe file HOT 1
- does it supprot termux
- SyntaxError: Invalid or unexpected token HOT 1
- Error: The specified module could not be found (External DLLs)
- [BUG] can't run child_process.fork with string code HOT 1
- Not able to create binaries for axios node package
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pkg.