Cluster Watchdog
Cluster Watchdog is a lightweight package for managing active-standby clusters
Installation
To install Cluster Watchdog, run one of the following commands:
using npm:
npm i cluster-watchdog
using yarn:
yarn add cluster-watchdog
using pnpm:
pnpm add cluster-watchdog
Usage
import { initWatchdog, runWhenActive } from 'cluster-watchdog';
import { Client, createClient } from 'node-zookeeper-client';
// Create a ZooKeeper client and connect to it
const createZookeeperClient = (connectionString: string): Promise<Client> =>
new Promise<Client>((resolve) => {
const client: Client = createClient(connectionString);
client.once('connected', () => resolve(client));
});
// A function that will execute only if the service is the primary/active
const poller = runWhenActive(async () => {
try {
/*
do polling/db writing
*/
setTimeout(poller, 1000);
} catch (e) {
setTimeout(poller, 3000);
}
});
(async () => {
const client = await createZookeeperClient('localhost:2181');
// Initiate Watchdog Cluster
await initWatchdog({
client,
sequencePath: '/path',
threshold: 5000,
log: console.log,
});
// Call the function
poller();
})();
License
Cluster Watchdog is licensed under the MIT License.