diff --git a/Dockerfile b/Dockerfile index 8f13a0f..e7abc9a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -51,3 +51,7 @@ VOLUME ["/scan"] ENTRYPOINT ["/init"] CMD ["/app.sh"] +# Health check +HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ + CMD curl -f http://localhost:3000/health || exit 1 + diff --git a/root/app.sh b/root/app.sh index 551ed6d..efbf658 100755 --- a/root/app.sh +++ b/root/app.sh @@ -1,6 +1,6 @@ #!/command/with-contenv sh -ARGS="-d /scan " +ARGS="--health-check -d /scan " if [ ! -z "$IP" ]; then ARGS="${ARGS} -ip ${IP}" diff --git a/src/healthcheck.ts b/src/healthcheck.ts new file mode 100644 index 0000000..644cedf --- /dev/null +++ b/src/healthcheck.ts @@ -0,0 +1,17 @@ +import http from "http"; + +export function startHealthCheckServer(PORT: number) { + const server = http.createServer((req, res) => { + if (req.method === "GET" && req.url === "/health") { + res.writeHead(200, { "Content-Type": "application/json" }); + res.end(JSON.stringify({ status: "healthy" })); + } else { + res.writeHead(404, { "Content-Type": "text/plain" }); + res.end("Not Found"); + } + }); + + server.listen(PORT, () => { + console.log(`Health endpoint exposed on port ${PORT} on path: /health`); + }); +} diff --git a/src/index.ts b/src/index.ts index 11276ab..3d32f13 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,6 +26,8 @@ import { } from "./scanProcessing"; import * as commitInfo from "./commitInfo.json"; import { PaperlessConfig } from "./paperless/PaperlessConfig"; +import { startHealthCheckServer } from "./healthcheck"; + let iteration = 0; @@ -299,6 +301,10 @@ function getPaperlessConfig( getConfig("paperless_post_document_url"); const configPaperlessToken: string = parentOption.paperlessToken || getConfig("paperless_token"); + const configPaperlessKeepFiles = + parentOption.paperlessKeepFiles || + getConfig("paperless_keep_files") || + false; if (paperlessPostDocumentUrl && configPaperlessToken) { const configPaperlessKeepFiles: boolean = @@ -329,6 +335,21 @@ function getPaperlessConfig( } } +function getHealthCheckSetting(option: OptionValues) { + const healthCheckEnabled: boolean = + option.healthCheck || getConfig("enableHealthCheck") === true; + let healthCheckPort: number; + if (option.healthCheckPort) { + healthCheckPort = parseInt(option.healthCheckPort, 10); + } else { + healthCheckPort = getConfig("healthCheckPort") || 3000; + } + return { + isHealthCheckEnabled: healthCheckEnabled, + healthCheckPort: healthCheckPort, + }; +} + function getScanConfiguration(option: OptionValues) { const directoryConfig: DirectoryConfig = { directory: option.directory || getConfig("directory"), @@ -380,6 +401,15 @@ async function main() { "-l, --label