Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdesu committed Sep 8, 2023
2 parents 9e3a736 + 9ec71f5 commit 08ebec5
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 121 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: deploy
run-name: Deploy artifacts

on:
# Only run this workflow automatically after ci workflow on main branch.
workflow_run:
workflows: [ci]
types: [completed]
branches: [main, develop]

jobs:
deploy-to-mainnet:
runs-on: sg
if: github.event.workflow_run.conclusion == 'success' && github.ref_name == 'main'
environment: mainnet
strategy:
matrix:
target: [SERVER_1, SERVER_2]
steps:
- name: Download Artifact
uses: dawidd6/action-download-artifact@v2
with:
workflow: ci
workflow_conclusion: success
run_id: ${{ github.event.workflow_run.id }}
run_number: ${{ github.event.workflow_run.run_number }}
name: thq-${{ github.event.workflow_run.head_commit.id }}.zip

- name: Unzip artifact
run: rm -rf ./dist && unzip -q thq-${{ github.event.workflow_run.head_commit.id }}.zip -d ./dist

- name: Deploy to server contract
run: ls -la ./dist && rsync -a ./dist/ ${{ vars[matrix.target] }}:/mnt/ckb/ckb-time-generator

- name: Restart pm2 on server contract
run: ssh ${{ vars[matrix.target] }} 'source ~/.zshrc && cd /mnt/ckb/ckb-time-generator && npm install --omit=dev && npm run reload_mainnet'

deploy-to-testnet:
runs-on: sg
if: github.event.workflow_run.conclusion == 'success' && github.ref_name == 'develop'
environment: testnet
strategy:
matrix:
target: [SERVER_1, SERVER_2]
steps:
- name: Download Artifact
uses: dawidd6/action-download-artifact@v2
with:
workflow: ci
workflow_conclusion: success
run_id: ${{ github.event.workflow_run.id }}
run_number: ${{ github.event.workflow_run.run_number }}
name: thq-${{ github.event.workflow_run.head_commit.id }}.zip

- name: Unzip artifact
run: rm -rf ./dist && unzip -q thq-${{ github.event.workflow_run.head_commit.id }}.zip -d ./dist

- name: Deploy to server contract
run: ls -la ./dist && rsync -a ./dist/ ${{ vars[matrix.target] }}:/mnt/ckb/ckb-time-generator

- name: Restart pm2 on server contract
run: ssh ${{ vars[matrix.target] }} 'source ~/.zshrc && cd /mnt/ckb/ckb-time-generator && npm install --omit=dev && npm run reload_mainnet'
36 changes: 0 additions & 36 deletions .github/workflows/deploy-to-mainnet.yaml

This file was deleted.

36 changes: 0 additions & 36 deletions .github/workflows/deploy-to-testnet.yaml

This file was deleted.

33 changes: 16 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# ckb-time-generator

[![ci](https://github.com/dotbitHQ/ckb-time-generator/actions/workflows/ci.yaml/badge.svg)](https://github.com/dotbitHQ/ckb-time-generator/actions/workflows/ci.yaml)

The generator of [ckb-time-scripts](https://github.com/DeAccountSystems/ckb-time-script).


## How to Work

- Clone and install every dependencies.
- Copy `config/default.ts` to `config/local.ts`, edit configs as needed.
- Build with `npm run build`, each time you update `config/local.ts` it is needed to rebuild.
- Run with `npm run reload_testnet` or `npm run reload_production` base on environment.
- Clone and install every dependencies with [npm](https://www.npmjs.com/).
- Install [pm2](https://pm2.keymetrics.io/docs/usage/quick-start/) globally.
- Copy `config/{env}.yaml` to `config/local-{env}.yaml`, edit configs as needed.
- Run with `npm run reload_testnet` or `npm run reload_mainnet` base on environment.

> If it is needed to kown more details of the config file loading order, click this link [node-config](https://github.com/node-config/node-config/wiki/Configuration-Files#file-load-order) .

## Tool Commands
Expand All @@ -24,21 +28,16 @@ Same as `timestamp` other option of `-t` can be `blocknumber` and `quote`, for m

## Development

First, you need to create `config/local.ts`, input private keys like below:
First, you need to create `config/local-{env}.yaml`, input private keys like below:

```typescript
export default {
loglevel: 'debug',
timestamp: {
PayersPrivateKey: '0x000000000...',
},
blocknumber: {
PayersPrivateKey: '0x000000000...',
},
quote: {
PayersPrivateKey: '0x000000000...',
}
}
loglevel: 'debug'
timestamp:
PayersPrivateKey: '0x000000000...'
blocknumber:
PayersPrivateKey: '0x000000000...'
quote:
PayersPrivateKey: '0x000000000...'
```

Then, you will be able to run commands in testnet environment like below:
Expand Down
2 changes: 1 addition & 1 deletion ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const packageJson = require('./package.json')

function generateApp (name, entry, args, env) {
const isProd = env === 'production'
const isProd = env === 'mainnet'
const appName = `${packageJson.name}_${name}`

return {
Expand Down
35 changes: 24 additions & 11 deletions src/controller/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,11 @@ class Server extends EventEmitter {
protected ws: WebSocket

protected heartbeatStatus: { id: number, timer: any, history: any[] }
protected eventStatus: { timer: any, history: any[] }
protected eventStatus: { timer_notify: any, timer_warn: any, history: any[] }
protected txStatus: { txHash: string, waitedBlocks: number }

protected eventTimeoutLimit = TIME_1_M * 5
protected newBlockNotifyLimit = TIME_1_M * 3
protected newBlockWarnLimit = TIME_1_M * 9

constructor (url: string, logger: Logger) {
super()
Expand All @@ -283,7 +284,7 @@ class Server extends EventEmitter {

this.ws = ws
this.heartbeatStatus = { id: 1, timer: null, history: [] }
this.eventStatus = { timer: null, history: [] }
this.eventStatus = { timer_notify: null, timer_warn: null, history: [] }
this.txStatus = { txHash: '', waitedBlocks: 0 }
}

Expand Down Expand Up @@ -330,24 +331,36 @@ class Server extends EventEmitter {
this.logger.debug(`Received new block[${BigInt(result.number)}]`)

const status = this.eventStatus
clearTimeout(status.timer)
clearTimeout(status.timer_notify)
clearTimeout(status.timer_warn)

this.emit('update', result)

status.timer = setTimeout(async () => {
this.logger.error(`There is no new block for ${this.eventTimeoutLimit / 1000} seconds.`)
// This error reports only once if no message received, so DO NOT use notifyWithThrottle to report.
// The key point here is eventTimeoutLimit, enlarge it is the only way
await notifyLark(`There is no new block for ${this.eventTimeoutLimit / 1000} seconds.`, 'Check if CKB node is offline and the get_tip_header interface is reachable.')
}, this.eventTimeoutLimit)
// This error reports only once if no message received, so DO NOT use notifyWithThrottle to report.
// The key point here is eventTimeoutLimit, enlarge it is the only way
status.timer_notify = setTimeout(async () => {
await notifyLark(
`There is no new block for ${this.newBlockNotifyLimit / 1000} seconds.`,
`This problem occasionally occurs and can be safely ignored, a formal warning will be triggered afer ${this.newBlockWarnLimit / 1000} seconds.`,
false,
)
}, this.newBlockNotifyLimit)

status.timer_warn = setTimeout(async () => {
await notifyLark(
`There is no new block for ${this.newBlockWarnLimit / 1000} seconds.`,
'Check if CKB node is offline and the get_tip_header interface is reachable.',
true,
)
}, this.newBlockWarnLimit)
}
}

protected async onClose (code: number, reason: string) {
this.logger.warn('Connection closed, will retry connecting later.', { code, reason })

setTimeout(() => {
clearTimeout(this.eventStatus.timer)
clearTimeout(this.eventStatus.timer_notify)
clearTimeout(this.heartbeatStatus.timer)
this.connect()
}, TIME_30_S)
Expand Down
42 changes: 22 additions & 20 deletions src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,36 +168,38 @@ export async function notifyWithThrottle(source: string, duration: number, msg:
await notifyLark(msg, how_to_fix)
}

export async function notifyLark(msg: string, how_to_fix = '') {
export async function notifyLark(msg: string, how_to_fix = '', should_at = true) {
try {
const content: any[] = [
[{tag: 'text', un_escaped: true, text: `server: ${getCurrentServer()}`}],
[{tag: 'text', un_escaped: true, text: `ckb_ws_url: ${config.CKB_WS_URL}`}],
[{tag: 'text', un_escaped: true, text: `reason: ${msg}`}],
[{tag: 'text', un_escaped: true, text: `how to fix: ${how_to_fix}`}],
]
if (process.env.NODE_ENV === 'production') {
if (process.env.NODE_ENV === 'mainnet' && should_at) {
content.push([{tag: 'at', user_id: 'all'}])
}

const res = await fetch(`https://open.larksuite.com/open-apis/bot/v2/hook/${config.LARK_API_KEY}`, {
method: 'post',
body: JSON.stringify({
email: 'xieaolin@gmail.com',
msg_type: 'post',
content: {
post: {
zh_cn: {
title: `=== THQ Node 服务告警 (${process.env.NODE_ENV}) ===`,
content,
}
},
}
const res = await fetch(`https://open.larksuite.com/open-apis/bot/v2/hook/${config.LARK_API_KEY}`, {
method: 'post',
body: JSON.stringify({
email: 'xieaolin@gmail.com',
msg_type: 'post',
content: {
post: {
zh_cn: {
title: `=== THQ Node 服务告警 (${process.env.NODE_ENV}) ===`,
content,
}
},
}
})
})
})

if (res.status >= 400) {
console.error(`helper: send Lark notify failed, response ${res.status} ${res.statusText}`)
if (res.status >= 400) {
console.error(`helper: send Lark notify failed, response ${res.status} ${res.statusText}`)
}
} else {
const logger = rootLogger.child({ command: 'update', cell_type: 'unknown' })
logger.warn(`msg: ${msg}, how_to_fix: ${how_to_fix}`)
}
} catch (e) {
console.error('helper: send Lark notify failed:', e)
Expand Down

0 comments on commit 08ebec5

Please sign in to comment.