-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Monitor ws endpoints with script execution (#191)
* xcm ws endpoints monitor script * add workflow * fix workflow * add build step * fix build misplacement * remove include test chains param
- Loading branch information
Showing
4 changed files
with
202 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,32 @@ name: XCM WS endpoints monitor | |
|
||
on: | ||
workflow_dispatch: | ||
schedule: | ||
# Runs every 30 minutes | ||
- cron: '*/30 * * * *' | ||
|
||
jobs: | ||
run_script: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: hello world | ||
run: echo "Hello World!" | ||
- name: 🤘 checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: ⚙️ Setup Node.js environment | ||
uses: actions/[email protected] | ||
with: | ||
node-version: 18.x | ||
cache: 'npm' | ||
|
||
- name: Update npm | ||
run: npm i -g npm@9 | ||
|
||
- name: ⬇️ install | ||
run: npm ci --ignore-scripts | ||
|
||
- name: 🛠️ Build | ||
run: npm run build | ||
|
||
- name: 💻 Run script | ||
run: npx bun ./scripts/check-websockets.ts --slack-wh=${{ secrets.SLACK_WEBHOOK_URL }} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
/* eslint-disable no-console */ | ||
import { chainsList } from '@moonbeam-network/xcm-config'; | ||
import { IncomingWebhook } from '@slack/webhook'; | ||
import WebSocket from 'ws'; | ||
|
||
function procesArgs() { | ||
const args = process.argv; | ||
const incudeTestChains = args.some((arg) => arg === '--include-test-chains'); | ||
const slackWebhookArg = args.find((arg) => arg.startsWith('--slack-wh=')); | ||
const webhookUrl = slackWebhookArg ? slackWebhookArg.split('=')[1] : ''; | ||
return { incudeTestChains, webhookUrl }; | ||
} | ||
|
||
async function checkWebSocketEndpoints( | ||
endpoints: ChainEndpoint[], | ||
): Promise<{ endpoint: ChainEndpoint; isAlive: boolean }[]> { | ||
async function checkIsWebSocketAlive({ | ||
chainKey, | ||
ws: endpoint, | ||
}: ChainEndpoint): Promise<boolean> { | ||
return new Promise((resolve) => { | ||
const ws = new WebSocket(endpoint); | ||
|
||
let isAlive = false; | ||
|
||
ws.on('error', (error) => { | ||
console.error( | ||
`WebSocket ${chainKey} connection to ${endpoint} failed. Error: ${error.message}`, | ||
); | ||
resolve(false); | ||
}); | ||
|
||
ws.on('open', () => { | ||
console.log( | ||
`WebSocket ${chainKey} connection to ${endpoint} successful.`, | ||
); | ||
isAlive = true; | ||
ws.terminate(); | ||
}); | ||
|
||
ws.on('close', () => { | ||
resolve(isAlive); | ||
}); | ||
}); | ||
} | ||
|
||
return Promise.all( | ||
endpoints.map(async (endpoint) => { | ||
const isAlive = await checkIsWebSocketAlive(endpoint); | ||
|
||
return { endpoint, isAlive }; | ||
}), | ||
); | ||
} | ||
|
||
const { incudeTestChains, webhookUrl } = procesArgs(); | ||
|
||
const webhook = new IncomingWebhook(webhookUrl); | ||
|
||
interface ChainEndpoint { | ||
chainKey: string; | ||
ws: string; | ||
} | ||
|
||
const filteredChainList = incudeTestChains | ||
? chainsList | ||
: chainsList.filter((chain) => !chain.isTestChain); | ||
|
||
const websocketEndpoints = filteredChainList.map(({ key, ws }) => ({ | ||
chainKey: key, | ||
ws, | ||
})); | ||
|
||
if (incudeTestChains) { | ||
console.log('Checking the endpoints of all chains, including test chains...'); | ||
} | ||
|
||
checkWebSocketEndpoints(websocketEndpoints).then(async (results) => { | ||
let output = ''; | ||
|
||
results.forEach(({ isAlive, endpoint: { chainKey, ws } }) => { | ||
if (!isAlive) { | ||
output += `\n${chainKey}: \`${ws}\`,`; | ||
} | ||
}); | ||
|
||
if (output) { | ||
const text = `The following websocket endpoints from the XCM integrations in the dapp are not working, please review them: ${output}`; | ||
|
||
console.log(text); | ||
if (webhookUrl) { | ||
await webhook.send({ | ||
text, | ||
}); | ||
} else { | ||
console.warn('Slack webhook not detected, notification not sent'); | ||
} | ||
} | ||
}); |