Nodejs killed Sharp process by timeout 5 sec - javascript

I use sharp on Hook to resize images on the fly in fastify.
fastify.addHook('onSend', async (request, reply, payload) => {
const newPayload = await sharp(payload.filename).resize(200, 100).toFormat('jpg').toBuffer();
return newPayload
})
But when I run this code
I see 5 seconds between finished resize and killed some process bu Nodejs at the end.
After it I get image into browser.
Can you explain this behaviour?

Related

How to bypass tab freezing with my React app?

I have a react app which updates data from a database on a 2 second timer:
componentDidMount() {
this.interval = setInterval(()=> this.getSelPos(), 2000);
}
componentWillUnmount() {
clearInterval(this.interval)
}
async getSelPos() {
const response = await axios.get("http://localhost:5000/selector", { crossdomain: true });
console.log(response.data);
this.setState({SelectorPos: parseInt(response.data.selector)});
this.setState({Pressure: parseInt(response.data.pressure)});
this.setState({Temperature: parseInt(response.data.temperature)});
This just sends to a node api that gets a value from my database and responds. Currently set to poll every 2 seconds and update the page.
The problem I'm running into is that when I tab out or minimise the browser for a few minutes it'll hang for a few seconds on returning, or it'll crash entirely. I suspect this is due to the browser being smart and freezing the tab when not in use. But I'm stumped as to how to fix this issue?

How to slow down a single request / API endpoint for development?

EDIT: I am asking to slow down a SINGLE api call. Not the whole network. And I can't change the actual response time server side.
I am working in React. One component makes use of an API endpoint that sometimes takes very long. Every time that happens we allow the user to save, due to a bug.
I want to know how to slow down just that one API call, in order to create that particular situation.
So, I have for example
setLoading(true)
const templates = await getTemplates(dispatch)
setLoading(false)
in one component.
I would like to know what options I have to simulate that API response taking n seconds. No matter if doing that by code, tooling, etc..
setTimeout() on the server to delay sending the response, or on the client before resolving the promise.
EDIT: The code depends on your particular implementation/tools. Please share a snippet from your client-side function which calls the API, or the handler on the server.
Nevertheless, here's an example based on your snippet and JS's Promise/await combo:
setLoading(true)
const templates = await getTemplates(dispatch)
const delay = 1000;
await new Promise(resolve => setTimeout(resolve, delay));
setLoading(false);
Other example: node.js server
app.get('/api/whatever', (request, response) => {
const data = getDataFromDatabase():
const delay = 1000;
setTimeout(function() {
response.end(data);
}, delay);
})
Try this:
const call = (dati) => new Promise((resolve, reject) => {
setTimeout(() => { resolve(dati); }, 1000);
})
or you can see here: let promise wait a couple of seconds before return

How to notify HTTP client of the completion of a long task

I have a Node.js system that uploads a large number of objects to MongoDB and creates folders in dropbox for each object. This takes around 0.5 seconds per object. In situations therefore where i have many objects this could take up to around a minute. What i currently do is notify the client that the array of objects has been accepted using a 202 response code. However how do i then notify the client of completion a minute later.
app.post('/BulkAdd', function (req, res) {
issues = []
console.log(req.body)
res.status(202).send({response:"Processing"});
api_functions.bulkAdd(req.body).then( (failed, issues, success) => {
console.log('done')
})
});
bulkAdd: async function (req, callback) {
let failed = []
let issues = []
let success = []
i = 1
await req.reduce((promise, audit) => {
// return promise.then(_ => dropbox_functions.createFolder(audit.scanner_ui)
let globalData;
return promise.then(_ => this.add(audit)
.then((data)=> {globalData = data; return dropbox_functions.createFolder(data.ui, data)}, (error)=> {failed.push({audit: audit, error: 'There was an error adding this case to the database'}); console.log(error)})
.then((data)=>{console.log(data, globalData);return dropbox_functions.checkScannerFolderExists(audit.scanner_ui)},(error)=>{issues.push({audit: globalData, error: 'There was an error creating the case folder in dropbox'})})
.then((data)=>{return dropbox_functions.moveFolder(audit.scanner_ui, globalData.ui)},(error)=>{issues.push({audit: globalData, error: 'No data folder was found so an empty one was created'}); return dropbox_functions.createDataFolder(globalData.ui)})
.then(()=>success.push({audit:globalData}), issues.push({audit: globalData, error: 'Scanner folder found but items not moved'}))
);
}, Promise.resolve()).catch(error => {console.log(error)});
return(failed, issues, success)
},
Well the problem with making client request wait, is it will timeout after certain period or sometimes will show error with no response received.
What you can do is
- Make client request to server to initiate the task, and return 200OK and keep doing your task on server.
- Now write a file on server after insertion of every object as status.
- Read the file from client every 5-10 sec to check if server has completed creating objects or not.
- Mean while your task is not completed on server, show status with completion percentage or some animation.
Or simply implement WebHook or WebSockets to maintain communication.

AWS Lambda halts halfway while executing a function

I tried to execute two functions imported from two different files on aws lambda:
const tag_test = require("./tag.js");
const login_logout = require("./login_logout.js");
exports.handler = async function(event, context) {
await tag_test.tag();
await login_logout.login();
console.log("all tests done.");
}
The first function was executed fine, but while running the second function, lambda halted at one point and waited until the whole process timed out. I suspect it stopping right before let browser because on the console log, I can see "opening up browser" but not "got browser".
module.exports.tag = async() => {
console.log("starting test 2");
const puppeteer = require('puppeteer-lambda');
console.log("opening up browser");
let browser = await puppeteer.getBrowser(
'--no-sandbox',
'--disable-gpu',
'--single-process'
);
console.log("got browser");
let page = await browser.newPage();
console.log("got page");
//my test
//...
}
Does anyone have any insight on what went wrong?
increase lambda timeout. default is 3 seconds.
Resolved.
It's actually puppeteer-lambda's problem. Apparently you cannot open a browser, close it and then open it again on AWS lambda with puppeteer-lambda. I opened and later closed a browser in my first test and that's what made my second test stuck at let browser.
To solve this problem, I let the first test return the browser and pass it to the second test for reusing.

child Process reply after 10 min to browser api

I am trying to run a node js child process in the backend which is taking at least 10 min time to complete, and I want to send the response after 10 min. In node js, i have increased the server timeout to 10 min and its fine. But in the browser, it's still a problem. Its saying timeout error after 2 min of waiting.
As I am using axios module from npm, it has the functionality of timeout but that not working.
Could someone help me how to stalled browser for more than 10 min to send the message from the server and then render the UI?
UI Code:
action created code->
export const executeFile = (fileName) => {
return ((dispatch)=>{
axios.post(`/execute`,{fileName},{timeout:1000000}).then(
res=> dispatch({
type:'EXECUTE_FILE',
payload:res
})
).catch(err=>console.log(err))
})
}
backend code->
app.post('/execute',(req,res)=>{
console.log(req.body.fileName,"aaaaaaaaa");
// handler_function.deleteFolderRecursive()
exec('sleep 200 && ls', (err, stdout, stderr) => {
if (err) {
console.error(err,"here");
return;
}
console.log(stdout,"in here sucess");
res.send(stdout)
});
});

Categories

Resources