1 | # Copacetic
|
2 | [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
|
3 | [![Coverage Status](https://coveralls.io/repos/github/fresh8/copacetic/badge.svg?branch=master)](https://coveralls.io/github/fresh8/copacetic?branch=master)
|
4 | [![CircleCI](https://circleci.com/gh/fresh8/copacetic.svg?style=svg)](https://circleci.com/gh/fresh8/copacetic)
|
5 |
|
6 | A package to help your service check the health of its dependencies.
|
7 |
|
8 |
|
9 | ## Requirements
|
10 | Node v6.4.0 and above
|
11 |
|
12 | ## Installation
|
13 | ```
|
14 | npm install @fresh8/copacetic --save
|
15 | ```
|
16 |
|
17 | #### Quick Start - Javascript
|
18 | ```javascript
|
19 | const Copacetic = require('@fresh8/copacetic')
|
20 | const level = require('@fresh8/copacetic').dependencyLevel
|
21 |
|
22 | const copacetic = Copacetic()
|
23 |
|
24 | // Register a dependency
|
25 | copacetic.registerDependency({
|
26 | name: 'My-Dependency',
|
27 | url: 'https://my-Dependency.io',
|
28 | // Defaults to SOFT
|
29 | level: level.HARD
|
30 | })
|
31 |
|
32 |
|
33 | // Check its health
|
34 | copacetic
|
35 | .check({
|
36 | name: 'My-Dependency',
|
37 | retries: 2
|
38 | })
|
39 | .on('healthy', (Dependency) => {
|
40 | /**
|
41 | * { name: 'My-Dependency',
|
42 | * healthy: true/false,
|
43 | * lastChecked: Date,
|
44 | * level: 'SOFT'
|
45 | * }
|
46 | */
|
47 | })
|
48 | .on('unhealthy', (Dependency) => {
|
49 | // Handle degraded state...
|
50 | })
|
51 |
|
52 | // in promise mode
|
53 | copacetic.eventEmitterMode = false
|
54 |
|
55 | copacetic
|
56 | .check({ name: 'my-web-service' })
|
57 | .then((res) => {
|
58 | console.log(`${res.name} is healthy!`)
|
59 | })
|
60 | .catch((err) => {
|
61 | console.log(err)
|
62 | })
|
63 | ```
|
64 |
|
65 | #### Quick Start - Typescript
|
66 | ```typescript
|
67 | import * as Copacetic from '@fresh8/copacetic'
|
68 |
|
69 | const copacetic = Copacetic('my-service')
|
70 |
|
71 | const myDependencyOverHttp : Copacetic.DependencyOptions = {
|
72 | name: 'my-web-service',
|
73 | url: 'http://example.com'
|
74 | }
|
75 |
|
76 | copacetic.registerDependency(myDependencyOverHttp)
|
77 |
|
78 | instance
|
79 | .check({ name: 'my-web-service' })
|
80 | .on('healthy', (res: Copacetic.Health) => {
|
81 | // do something with your healthy dependency :)
|
82 | })
|
83 | .on('unhealthy', (res: Copacetic.Health) => {
|
84 | // handle degraded state
|
85 | })
|
86 |
|
87 | // in promise mode
|
88 | copacetic.eventEmitterMode = false
|
89 |
|
90 | async function waitForWebService () {
|
91 | try {
|
92 | const res = await instance
|
93 | .check<Copacetic.Health>({ name: 'my-web-service' })
|
94 |
|
95 | console.log(`${res.name} is healthy!`)
|
96 | } catch (err) {
|
97 | console.log(err)
|
98 | }
|
99 | }
|
100 | ```
|
101 |
|
102 | #### Quick Start - cluster mode
|
103 | ```js
|
104 |
|
105 | const Copacetic = require('@fresh8/copacetic')
|
106 | const level = require('@fresh8/copacetic').dependencyLevel
|
107 |
|
108 | const copacetic = Copacetic("A name", false)
|
109 |
|
110 | Copacetic.cluster.attach(copacetic)
|
111 |
|
112 | if (process.worker) {
|
113 | //register your usual dependencies like databases
|
114 |
|
115 |
|
116 | //use the line below to have the worker ask the master process a full health report
|
117 | copacetic.checkCluster() //`checkCluster` is only defined if the process is a worker and if you called `attach()`
|
118 | .then(console.log)
|
119 | } else {
|
120 | copacetic.checkAll()
|
121 | .then(() => {
|
122 | console.log(copacetic.healthReport) //Contains health information as reported by the workers
|
123 | })
|
124 | }
|
125 | ```
|
126 |
|
127 | {{>main}}
|
128 |
|
\ | No newline at end of file |