npm package: @mimik/cluster
Description: cluster management for mimik microservices
Author: mimik technology inc
Latest version: 5.1.3

<a name="module_cluster"></a>

## cluster
The following environment variables are set:

| Env variable name | Description | Default | Comments |
| ----------------- | ----------- | ------- | -------- |
| REGISTRATION_STATUS | Status of the registration with mIT |  |  |
| CLUSTER_STATUS_SERVERS | Number of servers in the cluster |  |  |
| CLUSTER_STATUS_UPDATED_AT | Timestamp of the last cluster status update |  |  |
| CLUSTER_STATUS_LEASE_TIME | Lease time interval |  |  |

**Example**  
```js
import cluster from '@mimik/cluster';
```

* [cluster](#module_cluster)
    * [~rpCluster(options)](#module_cluster..rpCluster) ⇒ <code>Promise</code>
    * [~createItResource(con, req, res)](#module_cluster..createItResource)
    * [~deleteServer(con, req, res)](#module_cluster..deleteServer)
    * [~register(correlationId, retry)](#module_cluster..register) ⇒ <code>Promise</code>
    * [~unRegister(correlationId)](#module_cluster..unRegister) ⇒ <code>Promise</code>

<a name="module_cluster..rpCluster"></a>

### cluster~rpCluster(options) ⇒ <code>Promise</code>
The response is an object with the following structure:
```javascript
{
  "data": [ 'array of all successful response payloads' ],
  "results": [
    {
      "result": (response from [rp-retry](http://lib.mimik360.com:4873/request-retry)),
      "server": (cluster node that produced the result)
    }
  ],
  "failures": [
    {
      "error": (error thrown for a request to a cluster node),
      "server": (cluster node that produced the error)
    }
  ]
}
```

**Kind**: inner method of [<code>cluster</code>](#module_cluster)  
**Category**: async  
**Requires**: <code>module:@mimik/sumologic-winston-logger</code>, <code>module:@mimik/oauth-helper</code>, <code>module:@mimik/response-helper</code>  
**Fulfil**: <code>object</code> - Aggregated response of the request.  

| Param | Type | Description |
| --- | --- | --- |
| options | <code>object</code> | Options for the request (similar to [axios](https://www.npmjs.com/package/axios) options). The only difference is the URL: it must NOT include the base URL, because the cluster provides it. |

<a name="module_cluster..createItResource"></a>

### cluster~createItResource(con, req, res)
Create IT resource endpoint.

**Kind**: inner method of [<code>cluster</code>](#module_cluster)  
**Category**: async  
**Requires**: <code>module:@mimik/swagger-helper</code>, <code>module:@mimik/response-helper</code>, <code>module:@mimik/sumologic-winston-logger</code>  

| Param | Type | Description |
| --- | --- | --- |
| con | <code>object</code> | Request context (from OpenAPI). |
| req | <code>object</code> | The HTTP request. |
| res | <code>object</code> | The HTTP response. |

<a name="module_cluster..deleteServer"></a>

### cluster~deleteServer(con, req, res)
Delete server endpoint.

**Kind**: inner method of [<code>cluster</code>](#module_cluster)  
**Category**: async  
**Requires**: <code>module:@mimik/swagger-helper</code>, <code>module:@mimik/response-helper</code>  

| Param | Type | Description |
| --- | --- | --- |
| con | <code>object</code> | Request context (from OpenAPI). |
| req | <code>object</code> | The HTTP request. |
| res | <code>object</code> | The HTTP response. |

<a name="module_cluster..register"></a>

### cluster~register(correlationId, retry) ⇒ <code>Promise</code>
Register a server with mIT.

**Kind**: inner method of [<code>cluster</code>](#module_cluster)  
**Category**: async  
**Requires**: <code>module:@mimik/sumologic-winston-logger</code>, <code>module:@mimik/oauth-helper</code>, <code>module:@mimik/address-helper</code>  
**Fulfil**: `null` or the response from `rpCluster`

Registration with mIT is retried at the interval defined by `REGISTRATION_RETRY`.
When registration completes, a lease call is made to mIT at a regular interval.
The lease interval is defined by mIT; if unspecified, it defaults to 2 hours.
When cluster management is enabled, a concurrent registration is executed after
approximately 60 seconds to guarantee cluster integrity.  

| Param | Type | Description |
| --- | --- | --- |
| correlationId | <code>string</code> | Correlation ID to associate with the request. |
| retry | <code>boolean</code> | Flag used when concurrent registration occurs. |

<a name="module_cluster..unRegister"></a>

### cluster~unRegister(correlationId) ⇒ <code>Promise</code>
The returned object is:
``` javascript
{
  nbServers: "number of servers in the cluster",
  serverId: "server ID",
  errors: "optional array of errors that occurred while unregistering"
}
```

**Kind**: inner method of [<code>cluster</code>](#module_cluster)  
**Category**: async  
**Requires**: <code>module:@mimik/sumologic-winston-logger</code>, <code>module:@mimik/oauth-helper</code>  
**Fulfil**: An object containing the number of servers, the serverId, and any errors (if present).  

| Param | Type | Description |
| --- | --- | --- |
| correlationId | <code>string</code> | Correlation ID to associate with the request. |