Cluster Library

Contents

This library contains cluster related functions.

Usage

Add the following to your build.gradle file:

dependencies {
  include "com.enonic.xp:lib-cluster:${xpVersion}"
}

In your JavaScript controller, add a require statement:

const clusterLib = require('/lib/xp/cluster');

You are now ready to use the library functionality.

Functions

isMaster

Tests whether the current node is the master node in the cluster. Cluster with multiple master nodes will only return true on the elected node.

Parameters

None

Returns

boolean : true if the current node is master; false otherwise.

Examples

Initialize data only on the master node
if (clusterLib.isMaster()) {
    initializeRepo();
}
Run task only on master
let tasklib = require('/lib/xp/task');

if (clusterLib.isMaster() {
    let taskId = taskLib.submit({
        description: 'Master only task',
        task: function () {
          longRunningTask();
        }
    });
})
Catch event and execute something only on master
let eventLib = require('/lib/xp/event');

eventLib.listener({
    type: 'node.pushed',
    localOnly: false,
    callback: function (event) {
        if(clusterLib.isMaster()) {
            log.info(JSON.stringify(event));
        }
    }
});

Contents

Contents