@webframe/healthcheck 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +31 -2
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/package.json +28 -4
- package/src/index.ts +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 tw050x
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @webframe/healthcheck
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A healthcheck for services with no HTTP server, such as queue workers. It writes the service's health to a file every so often, and the container's healthcheck reads it.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @webframe/healthcheck
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { defineHealthcheck } from '@webframe/healthcheck'
|
|
11
|
+
|
|
12
|
+
const worker = defineWorker({
|
|
13
|
+
source,
|
|
14
|
+
jobs,
|
|
15
|
+
health: defineHealthcheck({ filePath: '/tmp/health', checkInterval: 10_000 }),
|
|
16
|
+
})
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
A [worker](https://webframe.tw050x.net/guide/workers) given it marks it unhealthy when it hits an error it can't recover from, so the container is restarted. Anything else can call `health.error()` itself.
|
|
20
|
+
|
|
21
|
+
```yaml
|
|
22
|
+
# compose.yaml
|
|
23
|
+
healthcheck:
|
|
24
|
+
test: ["CMD", "grep", "-q", "^healthy$", "/tmp/health"]
|
|
25
|
+
interval: 30s
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Services with an HTTP server don't need it: [every server answers `GET /healthcheck`](https://webframe.tw050x.net/guide/server#healthchecks).
|
|
29
|
+
|
|
30
|
+
The full documentation is at [webframe.tw050x.net/packages/healthcheck](https://webframe.tw050x.net/packages/healthcheck).
|
|
31
|
+
|
|
32
|
+
Part of [webframe](https://webframe.tw050x.net), MIT licensed.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type HealthStatus = 'healthy' | 'unhealthy';
|
|
2
|
+
export type HealthcheckOptions = {
|
|
3
|
+
/** The file the status is written to, for the container's healthcheck to read. */
|
|
4
|
+
filePath: string;
|
|
5
|
+
/** How often to write it, in milliseconds. */
|
|
6
|
+
checkInterval: number;
|
|
7
|
+
};
|
|
8
|
+
export type Healthcheck = {
|
|
9
|
+
/** The status in the file. Throws, and marks the service unhealthy, if the file holds anything else. */
|
|
10
|
+
read(): HealthStatus;
|
|
11
|
+
/** Marks the service unhealthy for good: an error it can't recover from. */
|
|
12
|
+
error(): void;
|
|
13
|
+
/** Stops writing the status. */
|
|
14
|
+
stop(): void;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Writes the service's health to a file every so often, `healthy` until `error()` is called,
|
|
18
|
+
* for a container healthcheck such as `test: ["CMD", "grep", "-q", "^healthy$", "/tmp/health"]`.
|
|
19
|
+
* Workers without an HTTP server use it in place of `GET /healthcheck`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function defineHealthcheck(options: HealthcheckOptions): Healthcheck;
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,YAAY,GAAG,SAAS,GAAG,WAAW,CAAC;AAEnD,MAAM,MAAM,kBAAkB,GAAG;IAC/B,kFAAkF;IAClF,QAAQ,EAAE,MAAM,CAAC;IACjB,8CAA8C;IAC9C,aAAa,EAAE,MAAM,CAAC;CACvB,CAAA;AAED,MAAM,MAAM,WAAW,GAAG;IACxB,wGAAwG;IACxG,IAAI,IAAI,YAAY,CAAC;IACrB,4EAA4E;IAC5E,KAAK,IAAI,IAAI,CAAC;IACd,gCAAgC;IAChC,IAAI,IAAI,IAAI,CAAC;CACd,CAAA;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,GAAG,WAAW,CA2B1E"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
/**
|
|
3
|
+
* Writes the service's health to a file every so often, `healthy` until `error()` is called,
|
|
4
|
+
* for a container healthcheck such as `test: ["CMD", "grep", "-q", "^healthy$", "/tmp/health"]`.
|
|
5
|
+
* Workers without an HTTP server use it in place of `GET /healthcheck`.
|
|
6
|
+
*/
|
|
7
|
+
export function defineHealthcheck(options) {
|
|
8
|
+
let unrecoverable = false;
|
|
9
|
+
const write = () => {
|
|
10
|
+
writeFileSync(options.filePath, unrecoverable ? 'unhealthy' : 'healthy', 'utf-8');
|
|
11
|
+
};
|
|
12
|
+
const error = () => {
|
|
13
|
+
unrecoverable = true;
|
|
14
|
+
clearInterval(interval);
|
|
15
|
+
write();
|
|
16
|
+
};
|
|
17
|
+
const read = () => {
|
|
18
|
+
const content = readFileSync(options.filePath, 'utf-8');
|
|
19
|
+
if (content === 'healthy' || content === 'unhealthy') {
|
|
20
|
+
return content;
|
|
21
|
+
}
|
|
22
|
+
error();
|
|
23
|
+
throw new Error(`Invalid healthcheck file content: ${content}`);
|
|
24
|
+
};
|
|
25
|
+
const interval = setInterval(write, options.checkInterval);
|
|
26
|
+
interval.unref();
|
|
27
|
+
write();
|
|
28
|
+
return Object.freeze({ read, error, stop: () => clearInterval(interval) });
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAoBtD;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAA2B;IAC3D,IAAI,aAAa,GAAG,KAAK,CAAC;IAE1B,MAAM,KAAK,GAAG,GAAG,EAAE;QACjB,aAAa,CAAC,OAAO,CAAC,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IACpF,CAAC,CAAC;IAEF,MAAM,KAAK,GAAG,GAAG,EAAE;QACjB,aAAa,GAAG,IAAI,CAAC;QACrB,aAAa,CAAC,QAAQ,CAAC,CAAC;QACxB,KAAK,EAAE,CAAC;IACV,CAAC,CAAC;IAEF,MAAM,IAAI,GAAG,GAAiB,EAAE;QAC9B,MAAM,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;QACxD,IAAI,OAAO,KAAK,SAAS,IAAI,OAAO,KAAK,WAAW,EAAE,CAAC;YACrD,OAAO,OAAO,CAAC;QACjB,CAAC;QACD,KAAK,EAAE,CAAC;QACR,MAAM,IAAI,KAAK,CAAC,qCAAqC,OAAO,EAAE,CAAC,CAAC;IAClE,CAAC,CAAC;IAEF,MAAM,QAAQ,GAAG,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;IAC3D,QAAQ,CAAC,KAAK,EAAE,CAAC;IACjB,KAAK,EAAE,CAAC;IAER,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AAC7E,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webframe/healthcheck",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A file-based healthcheck for services run in containers.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"src"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {},
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=22"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/tw050x/webframe.git",
|
|
24
|
+
"directory": "packages/node/healthcheck"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://webframe.tw050x.net/packages/healthcheck",
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
|
|
3
|
+
export type HealthStatus = 'healthy' | 'unhealthy';
|
|
4
|
+
|
|
5
|
+
export type HealthcheckOptions = {
|
|
6
|
+
/** The file the status is written to, for the container's healthcheck to read. */
|
|
7
|
+
filePath: string;
|
|
8
|
+
/** How often to write it, in milliseconds. */
|
|
9
|
+
checkInterval: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type Healthcheck = {
|
|
13
|
+
/** The status in the file. Throws, and marks the service unhealthy, if the file holds anything else. */
|
|
14
|
+
read(): HealthStatus;
|
|
15
|
+
/** Marks the service unhealthy for good: an error it can't recover from. */
|
|
16
|
+
error(): void;
|
|
17
|
+
/** Stops writing the status. */
|
|
18
|
+
stop(): void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Writes the service's health to a file every so often, `healthy` until `error()` is called,
|
|
23
|
+
* for a container healthcheck such as `test: ["CMD", "grep", "-q", "^healthy$", "/tmp/health"]`.
|
|
24
|
+
* Workers without an HTTP server use it in place of `GET /healthcheck`.
|
|
25
|
+
*/
|
|
26
|
+
export function defineHealthcheck(options: HealthcheckOptions): Healthcheck {
|
|
27
|
+
let unrecoverable = false;
|
|
28
|
+
|
|
29
|
+
const write = () => {
|
|
30
|
+
writeFileSync(options.filePath, unrecoverable ? 'unhealthy' : 'healthy', 'utf-8');
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const error = () => {
|
|
34
|
+
unrecoverable = true;
|
|
35
|
+
clearInterval(interval);
|
|
36
|
+
write();
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const read = (): HealthStatus => {
|
|
40
|
+
const content = readFileSync(options.filePath, 'utf-8');
|
|
41
|
+
if (content === 'healthy' || content === 'unhealthy') {
|
|
42
|
+
return content;
|
|
43
|
+
}
|
|
44
|
+
error();
|
|
45
|
+
throw new Error(`Invalid healthcheck file content: ${content}`);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const interval = setInterval(write, options.checkInterval);
|
|
49
|
+
interval.unref();
|
|
50
|
+
write();
|
|
51
|
+
|
|
52
|
+
return Object.freeze({ read, error, stop: () => clearInterval(interval) });
|
|
53
|
+
}
|