Verified Commit 1ec14f72 authored by Melroy van den Berg's avatar Melroy van den Berg
Browse files

Introducing health API call. And add the health check to Dockerfile

parent 2f386317
Pipeline #3529 passed with stage
in 11 seconds
......@@ -11,4 +11,8 @@ RUN npm install
COPY . .
EXPOSE 3010
HEALTHCHECK --interval=30s --timeout=12s --start-period=25s \
CMD node healthcheck.js
CMD ["npm", "start"]
const http = require('http')
const options = {
host: 'localhost',
port: '3010',
path: '/health',
timeout: 2000
}
const request = http.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`)
if (res.statusCode === 200) {
process.exit(0)
} else {
process.exit(1)
}
})
request.on('error', (err) => {
console.log('ERROR: ' + err.message)
process.exit(1)
})
request.end()
......@@ -9,6 +9,7 @@ class Communicate {
constructor (bot, botChatID) {
this.bot = bot
this.botChatID = botChatID
this.fatalError = false
this.sendMessageOptions = { parse_mode: 'markdown', disable_web_page_preview: true }
// Notify channel about Bot booting-up (commented-out for now)
this.sendTelegramMessage('(Re)starting-up Bot... 🤓')
......@@ -83,9 +84,18 @@ class Communicate {
this.bot.sendMessage(this.botChatID, message, this.sendMessageOptions).catch(error => {
console.log('ERROR: Could not send Telegram message: "' + message + '", due to error: ' + error.message)
this.fatalError = true
})
}
/**
* Retrieve fatal error state
* @returns true if an fatal error ever occurred. Otherwise false.
*/
isError () {
return this.fatalError
}
/**
* Read content from file
* @param {String} fileName file name
......
......@@ -36,8 +36,15 @@ const bot = new TelegramBot(cfg.telegram_settings.bot_token)
// Inform the Telegram servers of the new webhook url
bot.setWebHook(`${cfg.telegram_settings.public_url}/bot${TELEGRAM_SECRET_HASH}`)
const app = express()
// Create API Fetcher, data processor and communication instances
const fetcher = new Fetcher(cfg.exchange_settings)
const dataProcessor = new DataProcessor(
cfg.tickers.warmup_period,
cfg.tickers.data_period,
cfg.tickers.indicators)
const comm = new Communicate(bot, cfg.telegram_settings.chat_id)
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
......@@ -55,9 +62,11 @@ app.get(`/test_api/${TEST_API_SECRET_HASH}/crypto`, (req, res) => {
res.send('OK')
})
// Start Express Server
app.listen(port, host, () => {
console.log(`INFO: Crypto Exchange Bot v${version} is now running on ${host} on port ${port}.`)
app.get('/health', (req, res) => {
// Check if there are any issues with the communication layer
const isError = comm.isError()
const statusCode = (isError) ? 500 : 200
res.status(statusCode).json({ health_ok: !isError })
})
// Simple ping command
......@@ -75,13 +84,10 @@ bot.sendMessage = (a, b, c) => {
})
} */
// Create API Fetcher, data processor and communication instances
const fetcher = new Fetcher(cfg.exchange_settings)
const dataProcessor = new DataProcessor(
cfg.tickers.warmup_period,
cfg.tickers.data_period,
cfg.tickers.indicators)
const comm = new Communicate(bot, cfg.telegram_settings.chat_id)
// Start Express Server
app.listen(port, host, () => {
console.log(`INFO: Crypto Exchange Bot v${version} is now running on ${host} on port ${port}.`)
})
/**
* Due to the 8 API/min rate limit, we need to call the API with time-outs
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment