8000 console log hook and send message to slack by jo-bisonai · Pull Request #184 · Bisonai/orakl · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

console log hook and send message to slack #184

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

Merged
merged 1 commit into from
Jan 26, 2023
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
1 change: 1 addition & 0 deletions core/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ NODE_ENV=
CHAIN=
HOST_SETTINGS_DB_DIR=
HEALTH_CHECK_PORT=
SLACK_WEBHOOK_URL=
3 changes: 3 additions & 0 deletions core/docker-compose.aggregator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ services:
- ORAKL_DIR=/app/db
- CHAIN
- HEALTH_CHECK_PORT
- SLACK_WEBHOOK_URL
healthcheck:
&common-healthcheck
test: "curl --fail http://localhost:${HEALTH_CHECK_PORT} || exit 1"
Expand All @@ -52,6 +53,7 @@ services:
- ORAKL_DIR=/app/db
- CHAIN
- HEALTH_CHECK_PORT
- SLACK_WEBHOOK_URL
healthcheck:
<<: *common-healthcheck
depends_on:
Expand All @@ -72,6 +74,7 @@ services:
- ORAKL_DIR=/app/db
- CHAIN
- HEALTH_CHECK_PORT
- SLACK_WEBHOOK_URL
healthcheck:
<<: *common-healthcheck
depends_on:
Expand Down
18 changes: 18 additions & 0 deletions core/docker-compose.vrf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ services:
- "host.docker.internal:host-gateway"
environment:
- NODE_ENV=production
- ORAKL_DIR=/app/db
- CHAIN
- HEALTH_CHECK_PORT
- SLACK_WEBHOOK_URL
healthcheck:
&common-healthcheck
test: "curl --fail http://localhost:${HEALTH_CHECK_PORT} || exit 1"
Expand All @@ -36,6 +40,8 @@ services:
redis:
condition: service_healthy
restart: unless-stopped
volumes:
- ${HOST_SETTINGS_DB_DIR}:/app/db

worker-vrf:
image: core-icn
Expand All @@ -44,11 +50,17 @@ services:
entrypoint: ["yarn", "start:worker:vrf"]
environment:
- NODE_ENV=production
- ORAKL_DIR=/app/db
- CHAIN
- HEALTH_CHECK_PORT
- SLACK_WEBHOOK_URL
healthcheck:
<<: *common-healthcheck
depends_on:
<<: *common-depends_on
restart: unless-stopped
volumes:
- ${HOST_SETTINGS_DB_DIR}:/app/db

reporter-vrf:
image: core-icn
Expand All @@ -59,8 +71,14 @@ services:
entrypoint: ["yarn", "start:reporter:vrf"]
environment:
- NODE_ENV=production
- ORAKL_DIR=/app/db
- CHAIN
- HEALTH_CHECK_PORT
- SLACK_WEBHOOK_URL
healthcheck:
<<: *common-healthcheck
depends_on:
<<: *common-depends_on
restart: unless-stopped
volumes:
- ${HOST_SETTINGS_DB_DIR}:/app/db
2 changes: 2 additions & 0 deletions core/package.json
Original file line number Diff line number Diff 8000 line change
Expand Up @@ -39,10 +39,12 @@
},
"dependencies": {
"@bisonai-cic/icn-contracts": "^0.3.0",
"@slack/webhook": "^6.1.0",
"axios": "^1.2.2",
"bn.js": "^5.2.1",
"bullmq": "^3.2.2",
"cmd-ts": "^0.11.0",
"console-hook": "^0.0.2",
"dotenv": "^16.0.3",
"elliptic": "^6.5.4",
"ethers": "^5.7.2",
Expand Down
2 changes: 2 additions & 0 deletions core/src/listener/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { IcnError, IcnErrorCode } from '../errors'
import { WORKER_ANY_API_QUEUE_NAME, WORKER_VRF_QUEUE_NAME, DB, CHAIN } from '../settings'
import { getListeners } from '../settings'
import { healthCheck } from '../health-checker'
import { hookConsoleError } from '../utils'

const LISTENERS = {
Aggregator: {
Expand All @@ -24,6 +25,7 @@ const LISTENERS = {
}

async function main() {
hookConsoleError()
const listener = loadArgs()
const listenersConfig = await getListeners(DB, CHAIN)

Expand Down
2 changes: 2 additions & 0 deletions core/src/reporter/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { aggregatorReporter } from './aggregator'
import { vrfReporter } from './vrf'
import { anyApiReporter } from './any-api'
import { healthCheck } from '../health-checker'
import { hookConsoleError } from '../utils'

const REPORTERS = {
AGGREGATOR: aggregatorReporter,
Expand All @@ -11,6 +12,7 @@ const REPORTERS = {
}

async function main() {
hookConsoleError()
const reporter = loadArgs()
REPORTERS[reporter]()
healthCheck()
Expand Down
27 changes: 25 additions & 2 deletions core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as Fs from 'node:fs/promises'
import * as fs from 'node:fs'
import { tmpdir } from 'node:os'
import os from 'node:os'
import path from 'node:path'
import { IcnError, IcnErrorCode } from './errors'
import { IncomingWebhook } from '@slack/webhook'
import Hook from 'console-hook'

export async function loadJson(filepath) {
const json = await Fs.readFile(filepath, 'utf8')
Expand Down Expand Up @@ -79,7 +81,28 @@ export function printObject(object) {

export function mkTmpFile({ fileName }: { fileName: string }): string {
const appPrefix = 'orakl'
const tmpDir = fs.mkdtempSync(path.join(tmpdir(), appPrefix))
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), appPrefix))
const tmpFilePath = path.join(tmpDir, fileName)
return tmpFilePath
}

function sendToSlack(error) {
const url = process.env.SLACK_WEBHOOK_URL
if (url) {
const webhook = new IncomingWebhook(url)
const text = ` :fire: _An error has occurred at_ \`${os.hostname()}\`\n \`\`\`${JSON.stringify(
error
)} \`\`\`\n>*System information*\n>*memory*: ${os.freemem()}/${os.totalmem()}\n>*machine*: ${os.machine()}\n>*platform*: ${os.platform()}\n>*upTime*: ${os.uptime()}\n>*version*: ${os.version()}
`
webhook.send({ text })
}
}

export function hookConsoleError() {
const consoleHook = Hook().attach((method, args) => {
if (method == 'error') {
sendToSlack(args)
}
})
consoleHook.detach
}
2 changes: 2 additions & 0 deletions core/src/worker/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { vrfWorker } from './vrf'
import { anyApiWorker } from './any-api'
import { predefinedFeedWorker } from './predefined-feed'
import { healthCheck } from '../health-checker'
import { hookConsoleError } from '../utils'

const WORKERS = {
AGGREGATOR: aggregatorWorker,
Expand All @@ -13,6 +14,7 @@ const WORKERS = {
}

async function main() {
hookConsoleError()
const worker = loadArgs()
WORKERS[worker]()
healthCheck()
Expand Down
0