Hi,
Yet another question about GenieACS’s callback mechanism. I have a lot of questions about how GenieACS actually executes its extensions, for right now it seems like if it takes too long for the callback to get called, whether or not it’s running asynchronously, the extension halts with the time out error. I am working with an extension script that I am not wholly familiar with so I decided to try messing with the example script given in the documentation.
The example script as given does work, but I decided to try adding additional time out to the script in order to simulate my own extension, which takes a matter of seconds before it has a result that it can call back.
const http = require("http");
let cache = null;
let cacheExpire = 0;
function latlong(args,callback) {
if (Date.now() < cacheExpire) {return callback(null, cache);}
http
.get("http://api.open-notify.org/iss-now.json", res => {
if(res.statusCode !== 200)
return callback(
new Error('Request failed (status code: ${res.statusCode})')
);
let rawData = "";
res.on("data", (chunk) => (rawData += chunk));
res.on("end", () => {
let pos=JSON.parse(rawData)["iss_position"];
cache = [+pos["latitude"], +pos["longitude"]];
cacheExpire = Date.now() + 10000;
callback(null,cache);
});
})
.on("error", (err) => {
callback(err);
});
}
function test(args, callback)
{
//Should be asynchronous, so extension is finished after adding this to the event queue.
setTimeout(latlong(args,callback),5000);
}
exports.latlong = test;
AFAIK the setTimeout function should run asynchronously, so the script itself should finish executing, and then when the callback is called, GenieACS updates the value accordingly. Of course, this is not what happens, I get an extension timed out.
It seems like even with the callback, if an extension would take too long to return a value, GenieACS gives a time out anyways. What happens if there is a significant delay between when a script finishes executing and when the callback is finally called? Does the ACS interface give a temporary blank value to a parameter and then replace it with the actual parameter when the callback runs?
Hopefully that all makes sense. I know there have been several other questions about extensions and callbacks, but none of them seem to give me the clarification I’m seeking. I hope I’m not just really confused about how the callback works.
Thanks,