Options
All
  • Public
  • Public/Protected
  • All
Menu

resource-handler

resource-handler

A thin wrapper around asynchronous resources

npm version Actions Status

Motivation

There are some scenarios when you need to monitor an async resource like a database connection that does not have auto reconnection functionality in order to recover it from a failure. This package provdes a lightwight wrapper around such resources that allows you to easily restore their state without any hussle.

Installation

npm i resource-handler

API

You can find API here.

Quick start

import * as amqp from 'amqplib';
import { create } from 'resource-handler';

const rh = create(async () => {
return amqp.connect(opts);
});

await rh.open();

const connection = await rh.resource();

await rh.close();

With automatic opening:

import * as amqp from 'amqplib';
import { open } from 'resource-handler';

const rh = await open(async () => {
return amqp.connect(opts);
});

const connection = await rh.resource();

await rh.close();

Retry options

By default, resource-handler uses default values for restoring a given resouce. You can tune it to meet your needs:

import * as amqp from 'amqplib';
import { create } from 'resource-handler';

const rh = create(
async () => {
return amqp.connect(opts);
},
{
retry: {
retries: 5,
minTimeout: 2000,
maxTimeout: 10000,
factor: 1.5,
randomize: true,
},
},
);

await rh.open();

const connection = await rh.resource();

await rh.close();

Custom closer

In case your resource has other than .close method for closing its operation, you can provide a custom closer function:

import { create } from 'resource-handler';

const rh = create(
async () => {
return connect();
},
{
closer: (resource) => resource.destroy(),
},
);

await rh.open();

const connection = await rh.resource();

await rh.close();

Events proxying

import { create } from 'resource-handler';

const rh = create(
async () => {
return connect();
},
{
events: ['foo'],
},
);

await rh.open();

rh.on('foo', () => console.log('bar'));

const connection = await rh.resource();

connection.emit('foo');

await rh.close();

Abrupt retries

import { create } from 'resource-handler';

const rh = create(
async () => {
return connect();
},
{
retry: {
onFailedAttempt(err) {
if (err.message === 'some error') {
throw new Error('Stop it!');
}
},
},
},
);

try {
await rh.open();
const res = await rh.resource();

console.log("Successfuly open a resource");
} catch (e) {
console.log("Failed to open a resource", e);
}

Generated using TypeDoc