Cluster API

Contents

This API provides cluster related functions.

Usage

Add the following to your build.gradle file:

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

Add the import statement to your controller:

import clusterLib from '/lib/xp/cluster';

You are now ready to use the API.

Functions

isMaster

Tests whether the current node is the master node in a cluster. A 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
import {isMaster} from '/lib/xp/cluster';
if (isMaster()) {
    initializeRepo();
}
Run task only on master
import {isMaster} from '/lib/xp/cluster';
import {submit} from '/lib/xp/task';

if (isMaster() {
    const taskId = submit({
        description: 'Master only task',
        task: () => {
          longRunningTask();
        }
    });
})
Catch event and execute something only on master
import {isMaster} from '/lib/xp/cluster';
import {listener} from '/lib/xp/event';

listener({
    type: 'node.pushed',
    localOnly: false,
    callback: (event) => {
        if(isMaster()) {
            log.info('event: %s', JSON.stringify(event));
        }
    }
});

Contents

Contents

AI-powered search

Juke AI