Skip to content

Commit adb3695

Browse files
committed
Detect running in a container
1 parent 19c4837 commit adb3695

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

features/statuspage.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import axios from "axios";
22
import { Client } from "discord.js";
3+
import { isDocker } from "../util";
34

45
export default (client: Client): void => {
6+
7+
// Check if the bot is running in a docker container by checking if the env variable UPTIME_KUMA_CONTAINERIZED is true
8+
if (isDocker()) return;
59
const updateStatus = async () => {
610
// This function is called every 1 minutes and pings the network status page for uptime monitoring
711
await axios.get(
812
`https://${process.env.UPTIME_KUMA_MONITOR_DOMAIN}/api/push/${process.env.UPTIME_KUMA_MONITOR_ID}?msg=OK&ping=${client.ws.ping}`
913
);
14+
console.log("ping anyways")
1015
setTimeout(updateStatus, 1000 * 60);
1116
};
1217
updateStatus().catch((err) => console.log(err));

index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import WOKCommands from "wokcommands";
44
import path from "path";
55
import chalk from "chalk";
66
import dotenv from "dotenv";
7+
import { isDocker } from "./util";
78

89
// import custom modules
910
import { checkForRoles, checkIfCollectionsExist } from "./rolesOps";
@@ -27,6 +28,7 @@ const client = new DiscordJs.Client({
2728
client.on("ready", async () => {
2829
if (client.user) {
2930
console.log(chalk.green(`Logged in as ${client.user.tag}!`));
31+
if (isDocker()) console.log(chalk.blueBright(`Running in a Docker container!`));
3032
console.log(
3133
chalk.yellow.bold(`I am running version: ${process.env.VERSION}`)
3234
);

package-lock.json

Lines changed: 0 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"axios": "^0.27.2",
2525
"chalk": "4.1.2",
2626
"discord.js": "^13.9.2",
27-
"is-docker": "^3.0.0",
2827
"mongoose": "^6.5.4",
2928
"path": "^0.12.7",
3029
"prettier": "^2.7.1",

util.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
1+
import fs from 'node:fs';
12
export function sleep(ms: number) {
23
// Create new promise that resolves itself after a delay of <ms>
34
return new Promise((resolve: (args: void) => void) =>
45
setTimeout(resolve, ms)
56
);
67
}
8+
9+
10+
//! This code was taken from the package `is-docker` and modified to work here with esm
11+
//! Original repository: https://github.com/sindresorhus/is-docker
12+
let isDockerCached: boolean;
13+
14+
function hasDockerEnv() {
15+
try {
16+
fs.statSync('/.dockerenv');
17+
return true;
18+
} catch {
19+
return false;
20+
}
21+
}
22+
23+
function hasDockerCGroup() {
24+
try {
25+
return fs.readFileSync('/proc/self/cgroup', 'utf8').includes('docker');
26+
} catch {
27+
return false;
28+
}
29+
}
30+
31+
export function isDocker() {
32+
// TODO: Use `??=` when targeting Node.js 16.
33+
if (isDockerCached === undefined) {
34+
isDockerCached = hasDockerEnv() || hasDockerCGroup();
35+
}
36+
37+
return isDockerCached;
38+
}

0 commit comments

Comments
 (0)