node-libxslt icon indicating copy to clipboard operation
node-libxslt copied to clipboard

render complicated xml file

Open mcgradycchen opened this issue 9 years ago • 5 comments

Hi Albanm,

We got a issue. Sometimes our xml file is very complicated, the apply sync function will run more than 30 minutes and makes 100% cpu.

Is there a way to abort the apply function with a timeout parameter or how can I implement this request?

Thanks

mcgradycchen avatar Apr 12 '16 10:04 mcgradycchen

Hello,

I don't think it is possible to abort a running function. Did you try the async function ? It will not shorten the time but it will free the main event loop and allow you to set your own timeout, something like this (not tested):

function(callback) {
  aborted = false;
  var timeout = setTimeout(function() {
    aborted = true;
    callback(new Error('libxslt timeout'));
  }, 1000);
  stylesheet.apply(..., function(err, result) {
    if (!aborted) {
      clearTimeout(timeout);
      callback(err, result);
    }
  });
}

This is probably ok only if you don't care much about load as the task will still be running in the background and occupying a thread.

Another, probably saner solution is to analyze summarily the xml input (string length ? number of nodes ?) before running libxslt.

albanm avatar Apr 12 '16 11:04 albanm

Hi Albanm,

Thanks for your reply.

The timeout solution looks good, but the task still be running is unacceptable.

I'm not familiar with Libxmljs. Would you like to tell me how can I summary the xml nodes or the depth ?

Thanks~

mcgradycchen avatar Apr 13 '16 01:04 mcgradycchen

Actually I am not that familiar with it myself. I guess something like this would work to count the nodes:

doc.find('//*').length

But is it going to be efficient on a very large document ? I don't know. Maybe the document string length is a good enough estimate ?

albanm avatar Apr 13 '16 06:04 albanm

Does your conversion have to happen in a process that lives on, or could you create a process just for the conversion, and kill said process if the conversion takes too long? That would be the most robust way of doing this, I think.

gagern avatar Apr 13 '16 15:04 gagern

@mcgradycchen The issue you're having isn't specific to this library though. You'll want to learn how to setup new tasks/processes and control them to deal with that timeout scenario. Check out "Killing/Stopping the command" at http://krasimirtsonev.com/blog/article/Nodejs-managing-child-processes-starting-stopping-exec-spawn

Basically, you'll want to make a module that has your task (which needs to be run, but needs to be cancelable also) and launch a new process which runs that task (and can be killed).

rainabba avatar May 18 '17 19:05 rainabba