Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add health check support #967 #1002

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion root/app.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/command/with-contenv sh

ARGS="-d /scan "
ARGS="--health-check -d /scan "

if [ ! -z "$IP" ]; then
ARGS="${ARGS} -ip ${IP}"
Expand Down
17 changes: 17 additions & 0 deletions src/healthcheck.ts
Original file line number Diff line number Diff line change
@@ -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`);
});
}
49 changes: 49 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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<number>("healthCheckPort") || 3000;
}
return {
isHealthCheckEnabled: healthCheckEnabled,
healthCheckPort: healthCheckPort,
};
}

function getScanConfiguration(option: OptionValues) {
const directoryConfig: DirectoryConfig = {
directory: option.directory || getConfig("directory"),
Expand Down Expand Up @@ -380,6 +401,15 @@ async function main() {
"-l, --label <label>",
"The label to display on the device (the default is the hostname)",
)
.addOption(
new Option("--health-check", "Start an http health check endpoint"),
)
.addOption(
new Option(
"--health-check-port <health-check-port>",
"Start an http health check endpoint",
),
)
.action(async (options, cmd) => {
const parentOption = cmd.parent.opts();

Expand All @@ -395,6 +425,11 @@ async function main() {

const deviceUpPollingInterval = getDeviceUpPollingInterval(parentOption);

const healthCheckSetting = getHealthCheckSetting(options);
if (healthCheckSetting.isHealthCheckEnabled) {
startHealthCheckServer(healthCheckSetting.healthCheckPort);
}

const scanConfig = getScanConfiguration(options);

await listenCmd(registrationConfig, scanConfig, deviceUpPollingInterval);
Expand Down Expand Up @@ -427,6 +462,15 @@ async function main() {
"Once document are detected to be in the adf, this specify the wait delay in millisecond before triggering the scan",
),
)
.addOption(
new Option("--health-check", "Start an http health check endpoint"),
)
.addOption(
new Option(
"--health-check-port <port>",
"Start an http health check endpoint",
),
)
.description(
"Automatically trigger a new scan job to this target once paper is detected in the automatic document feeder (adf)",
)
Expand All @@ -441,6 +485,11 @@ async function main() {

const deviceUpPollingInterval = getDeviceUpPollingInterval(parentOption);

const healthCheckSetting = getHealthCheckSetting(options);
if (healthCheckSetting.isHealthCheckEnabled) {
startHealthCheckServer(healthCheckSetting.healthCheckPort);
}

const scanConfig = getScanConfiguration(options);

const adfScanConfig: AdfAutoScanConfig = {
Expand Down
Loading
Loading