Skip to content

Commit

Permalink
create workflow main: merge fetch price and earning. Update queue Git…
Browse files Browse the repository at this point in the history
…Hub action
  • Loading branch information
thaovt287 committed Jun 25, 2024
1 parent 9c5ef94 commit 342583e
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 139 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/fetch-exchange-rate-dev.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Fetch Exchange Rate Dev
on:
schedule:
- cron: "30 0 * * *"
- cron: "20 0 * * *"
workflow_dispatch:
branches:
- dev
Expand All @@ -15,7 +15,7 @@ on:
concurrency: 'dev'

jobs:
health-check:
exchange-rate:
if: "! startsWith(github.event.head_commit.message, '[CI Skip]')"
runs-on: ubuntu-22.04
name: Fetch Exchange Rate
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/fetch-exchange-rate-main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Fetch Exchange Rate Main
on:
schedule:
- cron: "30 0 * * *"
- cron: "20 0 * * *"
workflow_dispatch:
branches:
- main
Expand All @@ -14,7 +14,7 @@ on:
concurrency: 'main'

jobs:
health-check:
exchange-rate:
if: "! startsWith(github.event.head_commit.message, '[CI Skip]')"
runs-on: ubuntu-22.04
name: Fetch Exchange Rate
Expand Down
60 changes: 0 additions & 60 deletions .github/workflows/fetch-price-dev.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/health-check-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
- name: Run scripts
run: |
BRANCH=dev yarn export-error-rpc
NODE_ENV=development yarn export-error-rpc
- name: Commit changes
id: commit_changes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,59 @@
name: Fetch Price Main
name: RPC Health Check Main
on:
schedule:
- cron: "*/30 * * * *"
- cron: "*/15 * * * *"
workflow_dispatch:
branches:
- main
inputs:
note:
description: 'Health check'
required: false
default: ''

concurrency: 'main'

jobs:
health-check:
if: "! startsWith(github.event.head_commit.message, '[CI Skip]')"
runs-on: ubuntu-22.04
name: Fetch Price Rate
name: RPC Health Check
steps:
- uses: actions/checkout@v4
with:
ref: main
ref:
fetch-depth: 0
token: ${{ secrets.GH_AUTOMATION_TOKEN }}

- name: Set input data
id: input_data
run: |
if [[ ${{ github.ref }} == 'refs/heads/master' ]]; then
echo "target=latest" >> $GITHUB_OUTPUT
else
echo "target=beta" >> $GITHUB_OUTPUT
fi
- uses: actions/setup-node@v3
with:
node-version: '18'
registry-url: 'https://registry.npmjs.org'

- name: Install dependencies
run: |
yarn install
yarn install
- name: Run scripts
run: |
NODE_ENV=production yarn fetch-price
NODE_ENV=production yarn export-error-rpc
- name: Commit changes
id: commit_changes
uses: EndBug/add-and-commit@v9
with:
author_name: Github-Bot
author_email: [email protected]
message: '[CI Skip] Fetch Price (main)'
message: '[CI Skip] Update error rpcs'

- name: Log commit changes
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/scheduler-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
PUPPETEER_EXECUTABLE_PATH: /usr/bin/google-chrome
run: |
yarn install
yarn fetch-data
NODE_ENV=development yarn fetch-data
- name: Deploy to Cloudflare Pages
id: cloudflare_deployment
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/scheduler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
PUPPETEER_EXECUTABLE_PATH: /usr/bin/google-chrome
run: |
yarn install
yarn fetch-data
NODE_ENV=production yarn fetch-data
- name: Deploy to Cloudflare Pages
id: cloudflare_deployment
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
"type": "module",
"private": false,
"scripts": {
"fetch-data": "node ./scripts/fetch-data.mjs",
"export-error-rpc": "node ./scripts/export-error-rpc.js",
"fetch-data": "node scripts/fetch-data.js",
"fetch-exchange-rate": "node ./scripts/fetch-exchange-rate.js",
"fetch-price": "node ./scripts/fetch-price.js"
"export-error-rpc": "node ./scripts/export-error-rpc.js"
},
"dependencies": {
"@polkadot/api": "^11.2.1",
Expand Down
41 changes: 23 additions & 18 deletions scripts/export-error-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const STATUS = {

const getChains = async (url = URL_GET_CHAIN) => {

const fileName = process.env.BRANCH === 'main' ? 'list.json' : 'preview.json'
const fileName = process.env.NODE_ENV === 'production' ? 'list.json' : 'preview.json'
const {success, data} = await createApiRequest({
url: `${url}/${fileName}`,
method: 'GET',
Expand Down Expand Up @@ -139,24 +139,29 @@ const getErrorRpc = async (chainInfo) => {
return Object.fromEntries(Object.entries(errorRpcs).sort())
}

const checkHealthRpc = async () => {
const chains = await getChains()
const errorChain = []
await Bluebird.map(chains, async (chainInfo) => {
const errorRpcs = await getErrorRpc(chainInfo)
console.log('errorRpcs', chainInfo.name, errorRpcs)
if (!(Object.entries(errorRpcs).length === 0 && errorRpcs.constructor === Object)) {
errorChain.push({
slug: chainInfo.slug,
name: chainInfo.name,
status: chainInfo.chainStatus,
errorRpcs,
})
}

}, {concurrency: 20})
export const checkHealthRpc = async () => {
try {
const chains = await getChains()
const errorChain = []
await Bluebird.map(chains, async (chainInfo) => {
const errorRpcs = await getErrorRpc(chainInfo)
console.log('errorRpcs', chainInfo.name, errorRpcs)
if (!(Object.entries(errorRpcs).length === 0 && errorRpcs.constructor === Object)) {
errorChain.push({
slug: chainInfo.slug,
name: chainInfo.name,
status: chainInfo.chainStatus,
errorRpcs,
})
}

}, {concurrency: 20})

writeFileSync(errorChain.sort((a, b) => (a.slug > b.slug) ? 1 : ((b.slug > a.slug) ? -1 : 0)), './data/chains/error-rpc.json')
} catch (err) {
console.log("Export error Rpc error", err)
}

writeFileSync(errorChain.sort((a, b) => (a.slug > b.slug) ? 1 : ((b.slug > a.slug) ? -1 : 0)), './data/chains/error-rpc.json')
}

setImmediate(async () => {
Expand Down
11 changes: 11 additions & 0 deletions scripts/fetch-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {fetchEarning} from "./fetch-earning.mjs";
import {fetchPrice} from "./fetch-price.js";

const main = async () => {
await Promise.all([
fetchEarning(),
fetchPrice(),
])
}

main().catch(console.error)
32 changes: 17 additions & 15 deletions scripts/fetch-data.mjs → scripts/fetch-earning.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const runBrowser = async () => {
const promiseList = poolInfos.map((pool) => {
const timeoutPromise = new Promise((resolve) => {
setTimeout(() => {
resolve([]);
resolve([]);
}, 60000);
});

Expand All @@ -105,27 +105,29 @@ const runBrowser = async () => {

data.forEach(([slug, targets]) => {
if (targets.length > 0) {
writeJSONFile(`earning/targets/${slug}.json`, targets);
writeJSONFile(`earning/targets/${slug}.json`, targets);
}
});

await virtualBrowser.close();
};

const main = async () => {
const errTimeout = setTimeout(() => {
throw new Error('Failed to fetch data');
}, 180000);
export const fetchEarning = async () => {
try {
const errTimeout = setTimeout(() => {
throw new Error('Failed to fetch data');
}, 180000);

// Run browser
await runBrowser();
// Run browser
await runBrowser();

// Wait for 1 second
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
// Wait for 1 second
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});

clearTimeout(errTimeout);
clearTimeout(errTimeout);
} catch (error) {
console.log("Fetch earning error", error)
}
};

main().catch(console.error);
10 changes: 7 additions & 3 deletions scripts/fetch-exchange-rate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ const fetchExchangeRateOnline = async () => {
return data
}

const fetchExchangeRate= async () => {
const exchangeRate = await fetchExchangeRateOnline()
await writeFileSync(exchangeRate, './data/exchange-rate/data.json')
export const fetchExchangeRate = async () => {
try {
const exchangeRate = await fetchExchangeRateOnline()
await writeFileSync(exchangeRate, './data/exchange-rate/data.json')
} catch (error) {
console.log("Fetch exchange rate error", error)
}
}

setImmediate(async () => {
Expand Down
46 changes: 20 additions & 26 deletions scripts/fetch-price.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,29 @@ import writeFileSync from "../utils/writeFile.js";

const fetchPriceApiCacher = async () => {

const url = process.env.NODE_ENV === 'production' ? 'https://api-cache.subwallet.app/api/price/get' : 'https://api-cache-dev.subwallet.app/api/price/get'
const {success, data} = await createApiRequest({
url,
method: 'GET',
})
if (!success) {
throw new Error(`Cannot get exchange rate`)
}

return data.reduce((acc, item) => {
const { id, ...rest } = item;
acc[id] = rest;
return acc;
}, {})
const url = process.env.NODE_ENV === 'production' ? 'https://api-cache.subwallet.app/api/price/get' : 'https://api-cache-dev.subwallet.app/api/price/get'
const {success, data} = await createApiRequest({
url,
method: 'GET',
})
if (!success) {
throw new Error(`Cannot get exchange rate`)
}

return data.reduce((acc, item) => {
const {id, ...rest} = item;
acc[id] = rest;
return acc;
}, {})
}

const fetchPrice = async () => {
export const fetchPrice = async () => {
try {
const exchangeRate = await fetchPriceApiCacher()
await writeFileSync(exchangeRate, './data/price/data.json')
}

setImmediate(async () => {
} catch (error) {
console.log("Fetch price error", error)
}

try {

await fetchPrice()
process.exit()
}

} catch (err) {
console.error(err)
}
})

0 comments on commit 342583e

Please sign in to comment.