Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render complicated xml file #49

Open
mcgradycchen opened this issue Apr 12, 2016 · 5 comments
Open

render complicated xml file #49

mcgradycchen opened this issue Apr 12, 2016 · 5 comments

Comments

@mcgradycchen
Copy link

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

@albanm
Copy link
Owner

albanm commented Apr 12, 2016

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.

@mcgradycchen
Copy link
Author

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~

@albanm
Copy link
Owner

albanm commented Apr 13, 2016

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 ?

@gagern
Copy link
Collaborator

gagern commented Apr 13, 2016

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.

@rainabba
Copy link

@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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants