Optimizing JS Timers in Alfresco to Minimize CPU Overhead

cancel
Showing results for 
Search instead for 
Did you mean: 
Meffapefecy
Member II

Optimizing JS Timers in Alfresco to Minimize CPU Overhead

Hello everyone,

I have a question: How can a timer function be integrated into Alfresco without causing excessive CPU consumption? Presently, we have an Alfresco rule triggering the execution of a JS script. Within this script, a timer function of the following type is being used:

 

function wait(sleepDuration) {
  var startTimestamp = new Date().getTime();
  while (new Date().getTime() < startTimestamp + sleepDuration) { /* Do nothing */ }
}



To clarify, this `wait` function is called before executing the `retrieveModifiedNodes(node)` function. This delay allows SolR time to index. Without it, the retrieval isn't always complete.

However, we've observed that this method logically demands a lot of CPU resources.

We're aware that Alfresco uses the Rhino engine for JavaScript. Therefore, we're seeking the best approach to adopt. For instance, would it be more suitable within the context of Alfresco to use this method instead:

 

setTimeout(function() {
var modifiedNodes = retrieveModifiedNodes(node);
}, 60000);



Or are there other more appropriate methods available?

1 Reply
afaust
Master

Re: Optimizing JS Timers in Alfresco to Minimize CPU Overhead

The ACS internal JavaScript API is not meant to be used for asynchronous use cases / internal timers, and has no special support for it. setTimeout is a browser JS function that is not available. A script loaded / executed from the classpath (instead of being stored in the Data Dictionary) can access the Java interoperability feature to use the Java Thread.sleep() method to wait without spending CPU cycles.