cancel
Showing results for 
Search instead for 
Did you mean: 

Optimizing JS Timers in Alfresco to Minimize CPU Overhead

Meffapefecy
Champ in-the-making
Champ in-the-making

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 1

afaust
Legendary Innovator
Legendary Innovator

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.

Getting started

Explore our Alfresco products with the links below. Use labels to filter content by product module.