imageMagick fails at socket instance - node.js

I'm using the imageMagick subclass of graphicsmagick to alter ~7k png images. The function below gets about 2/3 of the way through and fails (see log).
Seems to be an issue where a socket is failing. How do I debug this?
A thought I have is to use the -limit method, but no dice...
Thanks in advance :)
Code:
// changing an image's resolution is the same thing as changing its DPI
export default async function changeScreenshotResolution(dimension) {
new Promise(() => {
try {
const screenshots = fs.readdirSync(path.join(__dirname, `../output/screenshots/${dimension}`), function(error) {if (error) console.log(error)});
screenshots.map(screenshot => {
try {
let readStream = fs.createReadStream(path.join(__dirname, `../output/screenshots/${dimension}/${screenshot}`));
let writeStream = fs.createWriteStream(path.join(__dirname, `../output/screenshots/${dimension}1/${screenshot}`));
im(readStream, screenshot)
.limit('memory', '32MB') // i tried variations of this but it made no difference
.units('PixelsPerInch')
.density(300,300)
.stream()
.pipe(writeStream)
console.log(screenshot);
} catch (error) {
console.log(`Error changing screenshot resolution: ${error}`);
}
})
} catch (error) {
console.log(`changeScreenshotResolution error: ${error}`);
}
})
};
Error: spawn convert EAGAIN
at Process.ChildProcess._handle.onexit (internal/child_process.js:268:19)
at onErrorNT (internal/child_process.js:468:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: -35,
code: 'EAGAIN',
syscall: 'spawn convert',
path: 'convert',
spawnargs: [
'-density',
'300x300',
'/Users/daddy/Dev/scout/project-sunroof-scraping/output/screenshots/rect/6550 ARABIAN CIR.png',
'-units',
'PixelsPerInch',
'/Users/daddy/Dev/scout/project-sunroof-scraping/output/screenshots/rect1/6550 ARABIAN CIR.png'
]
}
events.js:292
throw er; // Unhandled 'error' event
^
Error: read ENOTCONN
at tryReadStart (net.js:571:20)
at Socket._read (net.js:582:5)
at Socket.Readable.read (_stream_readable.js:474:10)
at Socket.read (net.js:622:39)
at new Socket (net.js:374:12)
at Object.Socket (net.js:265:41)
at createSocket (internal/child_process.js:313:14)
at ChildProcess.spawn (internal/child_process.js:436:23)
at Object.spawn (child_process.js:548:9)
at spawn (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/cross-spawn/index.js:17:18)
at gm._spawn (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/gm/lib/command.js:224:14)
at /Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/gm/lib/command.js:101:12
at series (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/array-series/index.js:11:36)
at gm._preprocess (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/gm/lib/command.js:177:5)
at gm.write (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/gm/lib/command.js:99:10)
at /Users/daddy/Dev/scout/project-sunroof-scraping/modules/changeScreenshotDPI.js:18:26
Emitted 'error' event on Socket instance at:
at emitErrorNT (internal/streams/destroy.js:100:8)
at emitErrorCloseNT (internal/streams/destroy.js:68:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: -57,
code: 'ENOTCONN',
syscall: 'read'
}

Related

Node.js websocket unhandeled error events.js websocket.js

I am using express webserver with node.js and the standard websocket require('ws');
Now it happens sometimes when internet connection is lost, i think that is the main reason when this happen, i am getting then a error message and the script stops to run:
events.js:287
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TLSWrap.onStreamRead (internal/stream_base_commons.js:205:27) Emitted 'error' event on WebSocket instance at:
at WebSocket.finalize (C:\Users\ABC\Desktop\Websocket\node_modules\ws\lib\WebSocket.js:182:41)
at TLSSocket.emit (events.js:310:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) { errno: 'ECONNRESET',
code: 'ECONNRESET', syscall: 'read' }
In the websocket client code i have also write a console.log or error events, see my code below:
ws.on('message', function incoming(data)
{
const obj = JSON.parse(data);
console.log("Stream: "+obj["k"].s+" "+obj["k"].i+" closeprice "+obj["k"].c+"\n");
});
ws.on('error', function(e){
console.log("stream error "+e);
});
the question is if i can do something to prevent this behavior? I normaly know that this problems happen when you dont console.log error events but for me it looks like i am doing that already. Maybe do i need to make changes in the ws module files?

Error not being handled in chain of promises

I am fairly new to Node and am currently trying to tweak an existing project. Part of this uses the follow-redirects package to make HTTP(S) requests to fetch an image.
Occasionally my service crashes with the following trace:
events.js:174
throw er; // Unhandled 'error' event
^
Error: socket hang up
at createHangUpError (_http_client.js:323:15)
at Socket.socketOnEnd (_http_client.js:426:23)
at Socket.emit (events.js:203:15)
at Socket.EventEmitter.emit (domain.js:448:20)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
at ClientRequest.eventHandlers.(anonymous function) (/home/.../node_modules/follow-redirects/index.js:13:24)
at ClientRequest.emit (events.js:198:13)
at ClientRequest.EventEmitter.emit (domain.js:448:20)
at Socket.socketOnEnd (_http_client.js:426:9)
at Socket.emit (events.js:203:15)
[... lines matching original stack trace ...]
at process._tickCallback (internal/process/next_tick.js:63:19)
I assume this means that I am dropping the error which comes back from the HTTP request.
Here is my function where I make the request:
function fetchRemote (imageUrl) {
let options = ...
return new Promise((resolve, reject) => {
https.get(options, resolve);
});
}
And it's called using a chain of promises.
fetchRemote(imageUrl)
.then(getResizeStream)
.then(storeImage)
.then(finished)
.catch(error);
function error (err) {
reject(new Error(`Failed: ${err.message}`));
};
Is there a logic pathway I'm missing, or should this properly catch the error? Or is the issue actually in the follow-redirects package?
Sockets are "event" emitter based and not promises. You need to handle errors on sockets by doing something along the lines of
socket.on('error',function(err){ /* handle error */});
// for you:
https
.get(options, resolve)
.on('error', err => {
reject(err);
});

Cant find origin of Unhandled 'error' event ETIMEDOUT

I am building an application that is communicating with a local server through http requests, I decided to do a "real" test and it keeps throwing errors, but I cant track where the error is coming from, the stacktrace doesnt tell me where it originates from (in my app).
When I start my app it works for 20-30 seconds, it manages to get/send data to that local webserver, but after 20-30 seconds I get the following error:
events.js:287
throw er; // Unhandled 'error' event
^
Error: connect ETIMEDOUT 192.168.55.213:80
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
Emitted 'error' event on ClientRequest instance at:
at Socket.socketErrorListener (_http_client.js:426:9)
at Socket.emit (events.js:310:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
address: '192.168.55.213',
port: 80
}
I have tried all my http requests in a try/catch block and log the errors to try and pinpoint where it originates from but nothing, the try/catches dont work, because the error is still being thrown, so it is not from within my http requests and I cant figure out the origin of the error. My requests look something like this:
async RequestFunction(requesturl) {
return new Promise((resolve, reject) => {
try {
http.get(requesturl, (resp) => {
let data = "";
resp.on('data', (chunk) => data += chunk)
resolve(data)
});
} catch (error) {
console.log(`${requesturl}+${error.code}`);
}
}
}
But the catch never gets triggered. I am guessing that mostly what I am asking for is how to track where the error is originating from?
I have also tried wrapping my Request function in every place where I am calling and awaiting it with a try/catch block, same thing.

How to run exe file from node.js script?

I have an exe and I want to run it from node.js, and pass an argument, except that it doesn't work..
var exec = require('child_process').execFile;
const fs = require('fs');
try {
if (fs.existsSync("./program/bin/Release/program.exe")) {
console.log("Exists")
} else {
console.log("Not Exists")
}
} catch(err) {
console.log(err)
}
setTimeout(function() {
exec('./program/bin/Release/program.exe manual', function(err, data) {
console.log(err)
console.log(data.toString());
});
}, 0);
It definitely exists as it prints exists, and I can run it from a cmd prompt giving manual as an argument. But through node.js it is not working. It comes back with
Error: spawn ./program/bin/Release/program.exe manual ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:264:19)
at onErrorNT (internal/child_process.js:456:16)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'spawn ./program/bin/Release/program.exe manual',
path: './program/bin/Release/program.exe manual',
spawnargs: [],
cmd: './program/bin/Release/program.exe manual'
}
Does anyone know?
Thanks
Arguments should be passed as an array of strings as the second argument, like so:
exec('./program/bin/Release/program.exe', ['manual'], function(err, data) {
console.log(err)
console.log(data.toString());
});
https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback

Sequelize Error with query sending Error: read ECONNRESET

PostgreSQL 9.6.2 I send query from node.js / express.js application.
The sequelize provide the doc how to make the query, this example findAll.
I try to make same example.
console.log('user id: ', req.decoded);
db.orders.findAll({where: {userId: req.decoded.id}})
.then(function (orders) {
console.log('orders from db: ', orders);
})
.catch(function (err) {
console.log('orders request error from db: ', err);
});
console.log('end of function');
Console log:
user id: { id: 2 }
end of function
orders request error from db: { SequelizeConnectionError: read ECONNRESET
at D:\NodeJSProjects\ParkingHouse\node_modules\sequelize\lib\dialects\postgres\connection-manager.js:110:20
at Connection.<anonymous> (D:\NodeJSProjects\ParkingHouse\node_modules\pg\lib\client.js:186:5)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at Socket.<anonymous> (D:\NodeJSProjects\ParkingHouse\node_modules\pg\lib\connection.js:86:10)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1278:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
name: 'SequelizeConnectionError',
message: 'read ECONNRESET',
parent:
{ Error: read ECONNRESET
at exports._errnoException (util.js:1022:11)
at TCP.onread (net.js:569:26) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' },
original:
{ Error: read ECONNRESET
at exports._errnoException (util.js:1022:11)
at TCP.onread (net.js:569:26) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' } }
After some time the request is repeated and then I get the data from db.
I found the similar topic Node js ECONNRESET but I don't found answer.
Why db close the connection? And how to repair it.
The problem was in antivirus F-Secure, change it and all works.
Some problem:
https://community.f-secure.com/t5/Business/DeepGuard-seems-to-cause/td-p/88447
You are loosing you connection. Probably you are not calling next() in your framework. Or do not close transaction.
This structure does not make sense:
.then(function (foundUser) {
return foundUser;
}).catch(function (err) {
//get here the exception
return err;
});
Just remove it. Then rewrite your controller to something like
const user = await authorize(req);
if (!user) {
res.status(401).send('Authorization required');
return next();
}
res.status(200).send('All ok');
next();
On error your framework should serve 500 automatically for you. If you want to handle it manually use try { await Promise } catch (err) { }
If you use something that does not support await/async yet, than take a look at http://koajs.com/
I had similar Error today
{
error: {
Error: write ECONNRESET at WriteWrap.afterWrite (net.js:788:14) errno: 'ECONNRESET',
code: 'ECONNRESET', syscall: 'write'
}
}
You need to set the database maximum pool or avoid sending multiple at the same time.
Here there is a github thread that may help you.

Resources