idle event
There seems to be a use case with serverless functions, AWS Lambdas specifically:
You need to explicitly close the connection so the lambda can end properly, otherwise it will run until configured timeout is reached. And if you are doing many get/set calls you can't always know in a practical way which and when is the last one processed
let client;
function downloadXML(url) {
return new Promise( //code to download, resolve/reject );
}
function saveToReds(key, data) {
client.set(key, data);
}
exports.handler = async (event, context, callback) => {
client = redis.createClient(elastiCacheRedis);
someXML = await downloadXML(url);
objectsArrayXmlElements = libxmljs.parseXmlString(someXML);
objectsArrayXmlElements.forEach(
/*do stuff, then call saveToRedis(key, data); */
);
client.unref();
}
The code above will fail to write the first time to redis because client.unref() seems to be called before the file is downloaded and parsed. The weird thing is the second time the lambda function is run it will write correctly. Using process.nextTick() didn't seem to make any difference, exactly the same behaviour The only way it did work as expected was calling client.unref() inside the idle event.
someone?
I'm not sure that's actually what's happening here. I'd personally avoid using client.unref in this scenario and lean more toward an explicit client.quit when you know you are done. It's best to use .unref when you know there's another handle keeping the event loop turning and thus the client alive. Typically with Node, "unref" simply means "don't include this handle during accounting" where the accounting is "if the number of open handles is zero, the application can terminate." With Lambda, the contract of "this is a server that is still accepting incoming connections" is different enough being explicit is probably a good idea.
You may want to consider doing something like adding a counter that does a client.quit() in the saveToRedis client.set callback when the number of completed client.set commands equals the objectsArrayXmlElements. E.g. something like:
someXML = await downloadXML(url);
objectsArrayXmlElements = libxmljs.parseXmlString(someXML);
let completed = 0;
objectsArrayXmlElements.forEach(
/*do stuff */
redis.set(key, data, (err) => {
if (++completed === objectsArrayXmlElements.length) {
client.quit()
}
});
);
There's some error-handling you may want to incorporate, but does this provide you with at least something you can try for now?
If for await of existed yet this might be a good place for it.
I know this is a few months old, but I've stumbled upon this and figured I'd throw my two cents in.
While @brycebaril 's example would work fine, I personally find the use a counter in that way to be a clunky way to handle the problem. And considering in @gurucleff 's example he's already using Promise, you could keep that going and simply wrap the redis call in a Promise. Something like the following.
let client;
function downloadXML(url) {
return new Promise( /*code to download, resolve/reject*/ );
}
function saveToReds(key, data) {
return new Promise( (res, rej) => {
client.set(key, data), (err) => {
if (err) {
rej(err);
} else {
res();
}
};
});
}
exports.handler = async (event, context, callback) => {
client = redis.createClient(elastiCacheRedis);
downloadXML(url).then( (someXML) => {
objectsArrayXmlElements = libxmljs.parseXmlString(someXML);
return Promise.all(
return objectsArrayXmlElements.map( (xmlElement) => {
// do your work
return saveToReds(key, data);
})
);
}).then(client.quit).catch( (e) => {
// log some stuff
client.quit();
});
}