5.0.0 • Published 2 years ago

@fresh8/copacetic v5.0.0

Weekly downloads
357
License
MIT
Repository
github
Last release
2 years ago

Copacetic

JavaScript Style Guide Coverage Status CircleCI

A package to help your service check the health of its dependencies.

Requirements

Node v6.4.0 and above

Installation

npm install @fresh8/copacetic --save

Quick Start - Javascript

const Copacetic = require('@fresh8/copacetic')
const level = require('@fresh8/copacetic').dependencyLevel

const copacetic = Copacetic()

// Register a dependency
copacetic.registerDependency({
  name: 'My-Dependency',
  url: 'https://my-Dependency.io',
  // Defaults to SOFT
  level: level.HARD
})


// Check its health
copacetic
  .check({
    name: 'My-Dependency',
    retries: 2
  })
  .on('healthy', (Dependency) => {
    /**
     * { name: 'My-Dependency',
     *   healthy: true/false,
     *   lastChecked: Date,
     *   level: 'SOFT'
     * }
     */
  })
  .on('unhealthy', (Dependency) => {
    // Handle degraded state...
  })

// in promise mode
copacetic.eventEmitterMode = false

copacetic
  .check({ name: 'my-web-service' })
  .then((res) => {
    console.log(`${res.name} is healthy!`)
  })
  .catch((err) => {
    console.log(err)
  })

Quick Start - Typescript

import * as Copacetic from '@fresh8/copacetic'

const copacetic = Copacetic('my-service')

const myDependencyOverHttp : Copacetic.DependencyOptions = {
  name: 'my-web-service',
  url: 'http://example.com'
}

copacetic.registerDependency(myDependencyOverHttp)

instance
  .check({ name: 'my-web-service' })
  .on('healthy', (res: Copacetic.Health) => {
    // do something with your healthy dependency :)
  })
  .on('unhealthy', (res: Copacetic.Health) => {
    // handle degraded state
  })

// in promise mode
copacetic.eventEmitterMode = false

async function waitForWebService () {
  try {
    const res = await instance
      .check<Copacetic.Health>({ name: 'my-web-service' })

    console.log(`${res.name} is healthy!`)
  } catch (err) {
    console.log(err)
  }
}

Quick Start - cluster mode

const Copacetic = require('@fresh8/copacetic')
const level = require('@fresh8/copacetic').dependencyLevel

const copacetic = Copacetic("A name", false)

Copacetic.cluster.attach(copacetic)

if (process.worker) {
    //register your usual dependencies like databases


    //use the line below to have the worker ask the master process a full health report
    copacetic.checkCluster() //`checkCluster` is only defined if the process is a worker and if you called `attach()`
        .then(console.log)
} else {
    copacetic.checkAll()
        .then(() => {
            console.log(copacetic.healthReport) //Contains health information as reported by the workers
        })
}

Classes

Typedefs

Copacetic ⇐ EventEmitter

Kind: global class
Extends: EventEmitter

new Copacetic(name)

ParamTypeDefaultDescription
nameString''The name of your service

copacetic.isHealthy ⇒ Boolean

Kind: instance property of Copacetic
Returns: Boolean - Copacetic is healthy when all hard dependencies are healthy

copacetic.healthInfo ⇒ Array.<DependencyHealth>

Kind: instance property of Copacetic
Returns: Array.<DependencyHealth> - Health information on all dependencies

copacetic.healthReport ⇒ HealthReport

Kind: instance property of Copacetic
Returns: HealthReport - A full report of health information and dependencies

copacetic.getDependency(dependency) ⇒ Dependency

Kind: instance method of Copacetic

ParamType
dependencyDependency | String

copacetic.isDependencyRegistered(dependency) ⇒ Boolean

Kind: instance method of Copacetic
Returns: Boolean - Whether the dependency has been registered

ParamType
dependencyDependency | String

copacetic.registerDependency(opts) ⇒ Copacetic

Adds a dependency to a Copacetic instance

Kind: instance method of Copacetic

ParamTypeDescription
optsObjectThe configuration for a dependency

copacetic.deregisterDependency(name) ⇒ Copacetic

Removes a dependency from a Copacetic instance

Kind: instance method of Copacetic

ParamTypeDescription
nameStringThe name used to identify a dependency

copacetic.pollAll(interval, parallel, schedule) ⇒ Copacetic

Polls the health of all registered dependencies

Kind: instance method of Copacetic

ParamTypeDefault
intervalString'5 seconds'
parallelBooleantrue
scheduleString'start'

copacetic.poll(name, dependencies, interval, parallel, schedule) ⇒ Copacetic

Polls the health of a set of dependencies

Kind: instance method of Copacetic
Emits: Copacetic#event:health

ParamTypeDefaultDescription
nameStringThe identifier of a single dependency to be checked
dependenciesArray.<Object>An explicit set of dependencies to be polled
intervalString'5 seconds'
parallelBooleantrueKick of health checks in parallel or series
scheduleString'start'Schedule the next check to start (interval - ms)ms

Example

copacetic.poll({
  dependencies: [
    { name: 'my-dep' },
    { name: 'my-other-dep', retries: 2, maxDelay: '2 seconds' }
  ],
  schedule: 'end',
  interval: '1 minute 30 seconds'
})
.on('health', (serviceHealth, stop) => {
  // Do something with the result
  // [{ name: String, health: Boolean, level: HARD/SOFT, lastCheck: Date }]
  // stop polling
  stop()
})

Example

copacetic.poll({ name: 'my-dependency' })
.on('health', () => { ... Do something })

copacetic.stop()

stops polling registered dependencies

Kind: instance method of Copacetic

copacetic.checkAll(parallel) ⇒ Copacetic

Checks the health of all registered dependencies

Kind: instance method of Copacetic

ParamTypeDefaultDescription
parallelBooleantrueKick of health checks in parallel or series

copacetic.check(name, dependencies, retries, parallel) ⇒ Copacetic

Checks the health of a set, or single dependency

Kind: instance method of Copacetic
Emits: Copacetic#event:health, Copacetic#event:healthy, Copacetic#event:unhealthy

ParamTypeDefaultDescription
nameStringThe identifier of a single dependency to be checked
dependenciesArray.<Object>An explicit set of dependencies to be checked
retriesInteger1How many times should a dependency be checked, until it is deemed unhealthy
parallelBooleantrueKick of health checks in parallel or series

Example

copacetic.check({ name: 'my-dependency' })

Example

copacetic.check({ name: 'my-dependency', retries: 5 })
.on('healthy', serviceHealth => { ... Do stuff })
.on('unhealthy', serviceHealth => { ... Handle degraded state })

Example

copacetic.check({ dependencies: [
  { name: 'my-dep' },
  { name: 'my-other-dep', retries: 2, maxDelay: '1 second' }
] })
.on('health', (servicesHealth) => {
  // Do something with the result
  // [{ name: String, health: Boolean, level: HARD/SOFT, lastCheck: Date }]
})

Example

copacetic.check({ name: 'my-dependency' })
.then((health) => { ... Do Stuff })
.catch((err) => { ... Handle degraded state })

copacetic.waitFor(opts)

Convenience method that waits for a single, or set of dependencies to become healthy. Calling this means copacetic will keep re-checking indefinitely until the dependency(s) become healthy. If you want more control, use .check().

Kind: instance method of Copacetic

ParamTypeDescription
optsObjectoptions accepted by check()

Example

// wait indefinitely
copacetic.waitFor({ name: 'my-dependency'})
.on('healthy', serviceHealth => { ... Do stuff })

Example

// give up after 5 tries
copacetic.waitFor({ name: 'my-dependency', retries: 5})
.on('healthy', serviceHealth => { ... Do stuff })

copacetic._checkOne(name, maxDelay) ⇒ Promise

Kind: instance method of Copacetic

ParamTypeDescription
nameStringThe name used to identify a dependency
maxDelayIntegerThe maximum interval of time to wait when retrying

copacetic._checkMany(dependencies, parallel) ⇒ Promise

Checks an array of dependencies in parallel or sequantially

Kind: instance method of Copacetic

ParamType
dependenciesArray.<Dependency>
parallelBoolean

HealthReport : Object

The full health report including isHealthy and dependencies

Kind: global typedef
Properties

NameTypeDescription
isHealthyBooleanThe result of isHealthy
NameString
dependenciesArray.<DependencyHealth>The result of healthInfo
5.0.0

2 years ago

4.0.2

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.6.1

6 years ago

3.6.0

6 years ago

3.5.2

7 years ago

3.5.1

7 years ago

3.5.0

7 years ago

3.4.0

7 years ago

3.3.0

7 years ago

3.2.0

7 years ago

3.1.1

7 years ago

3.1.0

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago