Skip to content
This repository has been archived by the owner on Dec 8, 2019. It is now read-only.

Commit

Permalink
Added healthcheck and updated dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Freek Mencke committed Mar 23, 2019
1 parent 688c470 commit e54d2e4
Show file tree
Hide file tree
Showing 11 changed files with 584 additions and 405 deletions.
9 changes: 4 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
language: node_js
language: minimal
dist: trusty
node_js: '10'

cache: npm
services:
- docker

script:
- npm run lint
- npm run build:ci

- docker build --rm -t toxsickcoder/osrs-tracker-cron:dev .
16 changes: 13 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
FROM node:10-alpine as builder
WORKDIR /usr/src/app/
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run lint
RUN npm run build:ci

FROM node:10-alpine
WORKDIR /usr/src/app
COPY ./dist .
CMD ["node", "osrs-tracker-cron.js"]
WORKDIR /usr/app/
COPY --from=builder /usr/src/app/dist/ ./
HEALTHCHECK --interval=1m --timeout=2s \
CMD wget --quiet --tries=1 --spider http://localhost:8080 || exit 1
CMD [ "node", "osrs-tracker-cron.js" ]
841 changes: 492 additions & 349 deletions package-lock.json

Large diffs are not rendered by default.

27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "osrs-tracker-cron",
"version": "0.0.7",
"version": "0.0.8",
"author": "Freek Mencke",
"homepage": "https://twitter.com/FreekMencke",
"repository": {
Expand All @@ -13,32 +13,33 @@
"start:prod": "webpack --watch --config webpack.config.js --progress --env.mode=production && node dist/osrs-tracker-cron.js",
"build:prod": "webpack --config webpack.config.js --progress --env.mode=production",
"build:ci": "webpack --config webpack.config.js --env.mode=production",
"build:docker": "docker build --rm -t toxsickcoder/osrs-tracker-cron:dev .",
"analyse:dev": "webpack --config webpack.config.js --progress --env.mode=development --env.analyse && start dist/report.html",
"analyse:prod": "webpack --config webpack.config.js --progress --env.mode=production --env.analyse && start dist/report.html",
"lint": "tslint --project ."
},
"dependencies": {
"cron": "1.6.0",
"cron": "1.7.0",
"moment": "^2.24.0",
"mysql": "2.16.0",
"node-fetch": "2.3.0",
"object-hash": "^1.3.1"
},
"devDependencies": {
"@types/cron": "1.6.0",
"@types/cron": "1.7.0",
"@types/mysql": "2.15.5",
"@types/node": "11.9.4",
"@types/node-fetch": "2.1.6",
"@types/node": "^10.0.0",
"@types/node-fetch": "2.1.7",
"@types/object-hash": "^1.2.0",
"clean-webpack-plugin": "^1.0.1",
"nodemon-webpack-plugin": "^4.0.7",
"terser-webpack-plugin": "1.1.0",
"clean-webpack-plugin": "^2.0.1",
"nodemon-webpack-plugin": "^4.0.8",
"terser-webpack-plugin": "1.2.3",
"ts-loader": "^5.3.3",
"tslint": "^5.12.1",
"tslint": "^5.14.0",
"tslint-eslint-rules": "^5.4.0",
"typescript": "^3.3.3",
"webpack": "^4.29.3",
"webpack-bundle-analyzer": "^3.0.4",
"webpack-cli": "^3.2.3"
"typescript": "^3.3.4000",
"webpack": "^4.29.6",
"webpack-bundle-analyzer": "^3.1.0",
"webpack-cli": "^3.3.0"
}
}
8 changes: 4 additions & 4 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
NAME=$(node -p "require('./package.json').name");
VERSION=$(node -p "require('./package.json').version");

# secure canister login
echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin
DOCKER_USERNAME="toxsickcoder"

docker build -t "$DOCKER_USERNAME/$NAME:$VERSION" -t "$DOCKER_USERNAME/$NAME:latest" .

docker login -u "$DOCKER_USERNAME"

docker push "$DOCKER_USERNAME/$NAME:$VERSION"
docker push "$DOCKER_USERNAME/$NAME:latest"

curl -X POST $DOCKER_WEBHOOK
26 changes: 26 additions & 0 deletions src/app/common/health-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import http from 'http';
import { API } from '../../config/api';
import { HealthRepository } from '../repositories/health.repository';
import { Tasks } from '../tasks';

export function startHealthCheckServer(): void {
http
.createServer(async (req, res) => {
const tasksRunning =
Tasks.runningTasks.length === Tasks.TASK_COUNT && Tasks.runningTasks.reduce((a, b) => a && b.running!, true);

let mysqlConnectionHealthy = false;
await API.getDbConnection(connection =>
HealthRepository.checkConnection(connection).then(res => (mysqlConnectionHealthy = res.success))
);

if (tasksRunning && mysqlConnectionHealthy) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('HEALTHY\n');
} else {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('UNHEALTHY\n');
}
})
.listen(8080);
}
7 changes: 7 additions & 0 deletions src/app/repositories/health.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { PoolConnection } from 'mysql';

export class HealthRepository {
static checkConnection(connection: PoolConnection): Promise<{ success: boolean }> {
return new Promise(resolve => connection.query('show tables', outerError => resolve({ success: !outerError })));
}
}
16 changes: 14 additions & 2 deletions src/app/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ import { XpProcessPlayers } from './tasks/xp-process-players.task';
import { XpQueuePlayers } from './tasks/xp-queue-players.task';

export class Tasks {
static init(): void {
static readonly TASK_COUNT = 4;
static runningTasks: CronJob[] = [];

static start(stopOld: boolean = true): void {
if (stopOld) {
this.runningTasks.forEach(task => task.stop());
this.runningTasks = [];
}

this.initJobs();
}

private static initJobs(): void {
this.startJob('0 0 */2 * * *' /* Every two hours */, OsrsQueueDbu.runTask); // QUEUE ALL ITEMS FOR PRICE TRACKING
this.startJob('0 0 0 * * *' /* At UTC midnight */, XpQueuePlayers.runTask); // QUEUE ALL PLAYERS FOR XP TRACKING
this.startJob('0 * * * * *' /* Every minute */, OsrsProcessDbu.runTask); // PROCESS ALL ITEMS FOR PRICE TRACKING
this.startJob('0 * * * * *' /* Every minute */, XpProcessPlayers.runTask); // PROCESS PLAYERS FOR XP DATAPOINTS
}

private static startJob(cron: string, task: () => void): void {
new CronJob(cron, task, undefined, true, 'UTC');
this.runningTasks.push(new CronJob(cron, task, undefined, true, 'UTC'));
}
}
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cluster from 'cluster';

import { startHealthCheckServer } from './app/common/health-server';
import { Logger } from './app/common/logger';
import { Tasks } from './app/tasks';

Expand All @@ -13,5 +13,6 @@ if (cluster.isMaster) {
});
} else {
Logger.log(`WORKER ${cluster.worker.id} CREATED - INITIALISING TASKS`);
Tasks.init();
Tasks.start();
startHealthCheckServer();
}
32 changes: 6 additions & 26 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
{
"rulesDirectory": [
"node_modules/tslint-eslint-rules/dist/rules"
],
"rulesDirectory": ["node_modules/tslint-eslint-rules/dist/rules"],
"rules": {
"indent": [
true,
"spaces"
],
"indent": [true, "spaces"],
"max-classes-per-file": false,
"no-consecutive-blank-lines": true,
"no-duplicate-variable": [
true,
"check-parameters"
],
"no-duplicate-variable": [true, "check-parameters"],
"prefer-const": true,
"quotemark": [
true,
"single",
"avoid-escape",
"avoid-template"
],
"semicolon": [
true,
"always"
],
"quotemark": [true, "single", "avoid-escape", "avoid-template"],
"semicolon": [true, "always"],
"ter-indent": [
true,
2,
Expand All @@ -40,10 +24,6 @@
"typeLiterals": "ignore"
}
],
"typedef": [
true,
"call-signature",
"parameter"
]
"typedef": [true, "call-signature", "parameter"]
}
}
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module.exports = env => {
],
},
plugins: [
new CleanWebpackPlugin(['./dist']),
new CleanWebpackPlugin(),
new webpack.DefinePlugin({
VERSION: JSON.stringify(packageJson.version),
DEVELOP: env.mode === 'development',
Expand Down

0 comments on commit e54d2e4

Please sign in to comment.