# @anpingli/distributed

> Henson library for distributed service

Latest version **1.0.2** (published 2019-12-25) · UNLICENSED license · 0 weekly downloads

## Install

```sh
npm install @anpingli/distributed
pnpm add @anpingli/distributed
yarn add @anpingli/distributed
bun add @anpingli/distributed
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2019-12-25 |
| First published | 2019-12-25 |
| Weekly downloads | 0 |
| License | UNLICENSED |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=8.4.0 |
| Dependencies | 3 |
| Unpacked size | 57.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | anpingli |

## Links

- npm: https://www.npmjs.com/package/@anpingli/distributed
- Repository: https://gitlab.com/henson/common-lib/henson-distributed
- Homepage: https://gitlab.com/henson/common-lib/henson-distributed#readme
- Issues: https://gitlab.com/henson/common-lib/henson-distributed/issues
- npm.io page: https://npm.io/package/@anpingli/distributed

## Dependencies (3)

- [lodash](https://npm.io/package/lodash.md) 4.x
- [request](https://npm.io/package/request.md) 2.x
- [@anpingli/common](https://npm.io/package/@anpingli/common.md) 1.1.6

## Recent versions

- 1.0.2 (latest) — 2019-12-25
- 1.0.1 — 2019-12-25
- 1.0.0 — 2019-12-25

## README

# Henson Library for Distributed Service

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents**

- [Install](#install)
- [Design and Usage](#design-and-usage)
  - [Service Discovery](#service-discovery)
  - [Task Coordinator](#task-coordinator)
  - [Consul KV API](#consul-kv-api)
  - [Master Election API](#master-election-api)
  - [Get bucket's ip](#get-buckets-ip)
  - [Get nodes(running task coordinator for specified service) that owns at least 1 bucket](#get-nodesrunning-task-coordinator-for-specified-service-that-owns-at-least-1-bucket)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Install

    $ npm install @anpingli/distributed

## Design and Usage

### Service Discovery

```
import {discovery} from '@anpingli/distributed';
```

### Task Coordinator

```
import {TaskCoordinator} from '@anpingli/distributed';
```

### Consul KV API

```
import {kv} from '@anpingli/distributed';
```

### Master Election API

Option: `MasterElectionOption`

* `interval: number` - Election interval, in seconds

Example:

```
import {MasterElection} from '@anpingli/distributed';

// @param service: string - unique identity of the service
// @param option: { interval: number }
let test = new MasterElection('datacleanup', { interval: 10 });
test.start();

// check if it is the master
if (test.isMaster()) {
    //do something
}

// stop when quit
test.stop();
```

### Get bucket's ip

```
import {ServiceInfo, getServiceInfo} from '@anpingli/distributed';

let serviceInfo: ServiceInfo = await getServiceInfo('servicename', 1);

```

### Get nodes(running task coordinator for specified service) that owns at least 1 bucket
method:

getBucketOwners(servicename:string):ServiceInfo[]

It return array of ServiceInfo which have structure like this:

ServiceInfo {
  Tags: string[];
  Address: string;
  Port: number;
}

```
import {getBucketOwners} from '@anpingli/distributed';
const nodes = await getBucketOwners('connectivitymanager');
if(serviceInfos.length > 0){
    const ip = nodes[0].Address;
    const port = nodes[0].Port;
    ...
}
```

---
_Source: https://npm.io/package/@anpingli/distributed · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
