Skip to content

Commit

Permalink
feat: add watchdog healthcheck to docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
Jblew committed Jan 31, 2019
1 parent e8e3092 commit 1f4e017
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ ADD . /app

RUN bash -c 'set -o pipefail && \
( \
if [[ "$(node --version)" = "$(cat .nvmrc)"* ]]; then \
echo "Node version correct"; else echo "Node version in .nvmrc is different. Please update Dockerfile" && exit 1; fi \
if [[ "$(node --version)" = "$(cat .nvmrc)"* ]]; then \
echo "Node version correct"; else echo "Node version in .nvmrc is different. Please update Dockerfile" && exit 1; fi \
) \
&& npm install \
&& npm run build \
&& npm install -g'

CMD ["wise daemon"]

HEALTHCHECK --interval=500s --timeout=15s --start-period=500s --retries=2 CMD [ "node", "container-healthcheck.js" ]

##§ '\n' + data.config.docker.generateDockerfileFrontMatter(data) + '\n' §##
LABEL maintainer="The Wise Team (https://wise-team.io/) <contact@wiseteam.io>"
Expand Down
9 changes: 9 additions & 0 deletions container-healthcheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const watchdog = require("node-docker-watchdog");

const env = process.env.WISE_ENVIRONMENT_TYPE || process.env.ENVIRONMENT_TYPE || process.env.NODE_ENV || "unknown";

console.log("Healthcheck");
watchdog.CliWatchdogHealthcheck({
project: "steem-wise-cli daemon",
environment: env
});
5 changes: 5 additions & 0 deletions src/actions/daemon/DaemonAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import { ConfigLoader, ConfigLoadedFromFile } from "../../config/Config";
import { Wise, DirectBlockchainApi, SteemOperationNumber, SingleDaemon } from "steem-wise-core";
import { StaticConfig } from "../../config/StaticConfig";
import { Context } from "../../Context";
import { Watchdog } from "./Watchdog";

export class DaemonAction {
private context: Context;
private watchdog: Watchdog;
public constructor(context: Context) {
this.context = context;
this.watchdog = new Watchdog();
}

public doAction(config: ConfigLoadedFromFile, sinceBlockNum: undefined | number): Promise<string> {
Expand All @@ -23,6 +26,7 @@ export class DaemonAction {
let delegatorWise: Wise;

return Promise.resolve()
.then(() => this.watchdog.start())
.then(() => ConfigLoader.askForCredentialsIfEmpty(config))
.then(() => {
api = new DirectBlockchainApi(Wise.constructDefaultProtocol(), config.postingWif, {
Expand Down Expand Up @@ -57,6 +61,7 @@ export class DaemonAction {
}

if (event.type === SingleDaemon.EventType.EndBlockProcessing) {
this.watchdog.heartbeatBeat();
try {
fs.writeFileSync(lastBlockFile, "" + event.blockNum, { flag: "w+" });
this.context.log("Processed block " + event.blockNum);
Expand Down
22 changes: 22 additions & 0 deletions src/actions/daemon/Watchdog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { DockerWatchdogServer, TimeWatchdogStrategy } from "node-docker-watchdog";

export class Watchdog {
private heartbeat: TimeWatchdogStrategy;
private watchdogServer: DockerWatchdogServer;

public constructor() {
this.heartbeat = new TimeWatchdogStrategy().setIdentitier("heartbeat watchdog");

this.watchdogServer = new DockerWatchdogServer([this.heartbeat]);
}

public async start() {
await this.watchdogServer.listen();
this.heartbeatBeat();
}

public heartbeatBeat() {
const ttlSeconds = 30 * 60; // 30 minutes
this.heartbeat.beat(ttlSeconds * 1000);
}
}

0 comments on commit 1f4e017

Please sign in to comment.