diff --git a/.github/workflows/xcm-wss-endpoints-monitor.yml b/.github/workflows/xcm-wss-endpoints-monitor.yml index 3b915be5..7ce1b628 100644 --- a/.github/workflows/xcm-wss-endpoints-monitor.yml +++ b/.github/workflows/xcm-wss-endpoints-monitor.yml @@ -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/setup-node@v3.0.0 + 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 }} diff --git a/package-lock.json b/package-lock.json index 822e3dbd..677ffa93 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "devDependencies": { "@changesets/changelog-github": "^0.4.8", "@changesets/cli": "^2.26.2", + "@slack/webhook": "^7.0.2", "@types/jest": "^29.5.3", "@typescript-eslint/eslint-plugin": "^5.59.8", "@typescript-eslint/parser": "^5.59.8", @@ -5148,6 +5149,31 @@ "@sinonjs/commons": "^3.0.0" } }, + "node_modules/@slack/types": { + "version": "2.11.0", + "resolved": "https://registry.npmjs.org/@slack/types/-/types-2.11.0.tgz", + "integrity": "sha512-UlIrDWvuLaDly3QZhCPnwUSI/KYmV1N9LyhuH6EDKCRS1HWZhyTG3Ja46T3D0rYfqdltKYFXbJSSRPwZpwO0cQ==", + "dev": true, + "engines": { + "node": ">= 12.13.0", + "npm": ">= 6.12.0" + } + }, + "node_modules/@slack/webhook": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/@slack/webhook/-/webhook-7.0.2.tgz", + "integrity": "sha512-dsrO/ow6a6+xkLm/lZKbUNTsFJlBc679tD+qwlVTztsQkDxPLH6odM7FKALz1IHa+KpLX8HKUIPV13a7y7z29w==", + "dev": true, + "dependencies": { + "@slack/types": "^2.9.0", + "@types/node": ">=18.0.0", + "axios": "^1.6.3" + }, + "engines": { + "node": ">= 18", + "npm": ">= 8.6.0" + } + }, "node_modules/@snowfork/snowbridge-types": { "version": "0.2.7", "resolved": "https://registry.npmjs.org/@snowfork/snowbridge-types/-/snowbridge-types-0.2.7.tgz", @@ -6578,8 +6604,7 @@ "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", - "peer": true + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, "node_modules/available-typed-arrays": { "version": "1.0.5", @@ -6593,6 +6618,31 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/axios": { + "version": "1.6.5", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.5.tgz", + "integrity": "sha512-Ii012v05KEVuUoFWmMW/UQv9aRIc3ZwkWDcM+h5Il8izZCtRVpDUfwpoFf7eOtajT3QiGR4yDUx7lPqHJULgbg==", + "dev": true, + "dependencies": { + "follow-redirects": "^1.15.4", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, + "node_modules/axios/node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dev": true, + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/babel-jest": { "version": "29.6.1", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.6.1.tgz", @@ -7360,7 +7410,6 @@ "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "peer": true, "dependencies": { "delayed-stream": "~1.0.0" }, @@ -7625,7 +7674,6 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", - "peer": true, "engines": { "node": ">=0.4.0" } @@ -8832,6 +8880,26 @@ "integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==", "dev": true }, + "node_modules/follow-redirects": { + "version": "1.15.5", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz", + "integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==", + "dev": true, + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/for-each": { "version": "0.3.3", "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", @@ -12393,7 +12461,6 @@ "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", - "peer": true, "engines": { "node": ">= 0.6" } @@ -12402,7 +12469,6 @@ "version": "2.1.35", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", - "peer": true, "dependencies": { "mime-db": "1.52.0" }, @@ -14398,6 +14464,12 @@ "node": ">= 8" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", + "dev": true + }, "node_modules/pseudomap": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", diff --git a/package.json b/package.json index 2a72cf45..1df6f46a 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "devDependencies": { "@changesets/changelog-github": "^0.4.8", "@changesets/cli": "^2.26.2", + "@slack/webhook": "^7.0.2", "@types/jest": "^29.5.3", "@typescript-eslint/eslint-plugin": "^5.59.8", "@typescript-eslint/parser": "^5.59.8", diff --git a/scripts/check-websockets.ts b/scripts/check-websockets.ts new file mode 100644 index 00000000..9023cbe2 --- /dev/null +++ b/scripts/check-websockets.ts @@ -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 { + 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'); + } + } +});