Skip to content

Commit

Permalink
Merge pull request #29 from liuxy0551/feat_1.x_log
Browse files Browse the repository at this point in the history
feat: improve log and send msg when failed
  • Loading branch information
nankaNULL authored Jan 17, 2022
2 parents b90e591 + 57f541d commit 21e7806
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
13 changes: 3 additions & 10 deletions app/service/articleSubscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,14 @@ class ArticleSubscriptionService extends Service {
},
raw: true
})
const { webHook } = articleSubscription
const { webHook, groupName } = articleSubscription
const topicIds = articleSubscription.topicIds.split(',')
const topicList = topicAll.filter(item => topicIds.includes(`${ item.id }`))

// 打印文章订阅任务结果
const logFunc = (msg, siteName, topicName) => {
const result = `文章订阅任务执行${ msg }: ${ id }, 订阅项: ${ siteName }-${ topicName }`
// 向 agent 进程发消息
this.app.messenger.sendToAgent('timedTaskResult', { result })
}

for (let item of topicList) {
const { siteName, topicName, topicUrl } = item
siteName === 'Github' && getGithubTrending(topicName, topicUrl, webHook, this.app, (msg) => { logFunc(msg, siteName, topicName) })
siteName === '掘金' && getJueJinHot(topicName, topicUrl, webHook, this.app, (msg) => { logFunc(msg, siteName, topicName) })
siteName === 'Github' && getGithubTrending(id, groupName, siteName, topicName, topicUrl, webHook, this.app)
siteName === '掘金' && getJueJinHot(id, groupName, siteName, topicName, topicUrl, webHook, this.app)
}
}

Expand Down
31 changes: 24 additions & 7 deletions app/utils/articleSubscription.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const cheerio = require('cheerio')
const axios = require('axios')
const { sendArticleMsg } = require('./index')
const config = require('../../env.json')
const { sendArticleMsg, sendAlarmMsg } = require('./index')
const webhookUrls = config && config.webhookUrls ? config.webhookUrls : [] // 钉钉通知群

// github trending
const getGithubTrending = async (topicName, topicUrl, webHook, app, logFunc) => {
const getGithubTrending = async (id, groupName, siteName, topicName, topicUrl, webHook, app) => {
try {
const pageSize = app.config.articleSubscription.pageSize
const { data } = await axios.get(`https://github.com/trending/${ topicUrl }?since=daily`, { timeout: 30_000 })
Expand All @@ -17,14 +19,14 @@ const getGithubTrending = async (topicName, topicUrl, webHook, app, logFunc) =>
}
msg += `[点击查看更多内容](https://github.com/trending/${ topicUrl }?since=daily)`
sendArticleMsg('Github Trending 今日 Top5', msg, webHook)
logFunc('成功')
logFunc(app, id, groupName, siteName, topicName, '成功')
} catch (err) {
logFunc(`失败Github 网络不佳 ${ JSON.stringify(err) }`)
logFunc(app, id, groupName, siteName, topicName, `失败`, `Github 网络不佳 ${ JSON.stringify(err) }`)
}
}

// 掘金热门
const getJueJinHot = async (topicName, topicUrl, webHook, app, logFunc) => {
const getJueJinHot = async (id, groupName, siteName, topicName, topicUrl, webHook, app) => {
try {
const pageSize = app.config.articleSubscription.pageSize
const params = {
Expand All @@ -41,9 +43,24 @@ const getJueJinHot = async (topicName, topicUrl, webHook, app, logFunc) => {
msg += `${ i + 1 }、[${ data[i].article_info.title }](https://juejin.cn/post/${ data[i].article_id })\n\n`
}
sendArticleMsg('掘金热门 Top5', msg, webHook)
logFunc('成功')
logFunc(app, id, groupName, siteName, topicName, '成功')
} catch (err) {
logFunc(`失败,${ JSON.stringify(err) }`)
logFunc(app, id, groupName, siteName, topicName, `失败`, `${ JSON.stringify(err) }`)
}
}

// 打印文章订阅任务结果
const logFunc = (app, id, groupName, siteName, topicName, msg, errMsg = '') => {
const result = `文章订阅任务, id: ${ id } 执行${ msg }, 钉钉群名称: ${ groupName }, 订阅项: ${ siteName }-${ topicName } ${ errMsg ? ', ' + errMsg : '' }`
// 向 agent 进程发消息
app.messenger.sendToAgent('timedTaskResult', { result })

// 文章订阅发送失败时,发出告警信息
if (msg === '失败') {
const msgText = `文章订阅结果:${ msg }\n钉钉群名称:${ groupName }\n订阅项:${ siteName }-${ topicName }\n\n请前往服务器查看对应日志!`
for (let webHook of webhookUrls) {
sendAlarmMsg(msgText, webHook)
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions app/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,19 @@ const sendArticleMsg = async (title, text, webhook) => {
.catch(ex => console.error(ex))
}

// 文章订阅发送失败时,发出告警信息
const sendAlarmMsg = async (content, webhook) => {
const feChatRobot = new ChatBot({ webhook })
feChatRobot.text(content)
}

module.exports = {
createFolder,
createFileSync,
sendMsg,
sendHostsUpdateMsg,
sendArticleMsg,
sendAlarmMsg,
response: (success, data = null, message)=>{
if(success) {
message='执行成功';
Expand Down
8 changes: 4 additions & 4 deletions app/utils/timedTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ const schedule = require('node-schedule')
// 判断文章订阅任务是否存在
const timedTaskIsExist = (name, agent) => {
const timedTask = schedule.scheduledJobs[`${ name }`]
log(agent, `文章订阅任务:${ name } ${ timedTask === undefined ? '不存在' : '存在' }`)
log(agent, `文章订阅任务, id${ name } ${ timedTask === undefined ? '不存在' : '存在' }`)
return timedTask !== undefined
}

// 开始文章订阅任务
const createTimedTask = (name, cron, agent) => {
if (timedTaskIsExist(name, agent)) return
log(agent, `创建文章订阅任务: ${ name }, Cron: ${ cron }`)
log(agent, `创建文章订阅任务, id: ${ name }, Cron: ${ cron }`)
schedule.scheduleJob(`${ name }`, cron, () => {
// agent 进程随机给一个 app 进程发消息(由 master 来控制发送给谁)
agent.messenger.sendRandom('sendArticleSubscription', name)
Expand All @@ -24,13 +24,13 @@ const createTimedTask = (name, cron, agent) => {
const changeTimedTask = (name, cron, agent) => {
if (!timedTaskIsExist(name, agent)) return createTimedTask(name, cron, agent)
schedule.rescheduleJob(schedule.scheduledJobs[`${ name }`], cron)
log(agent, `编辑文章订阅任务: ${ name }, Cron: ${ cron }`)
log(agent, `编辑文章订阅任务, id: ${ name }, Cron: ${ cron }`)
}

// 取消指定文章订阅任务
const cancelTimedTask = (name, agent) => {
if (!timedTaskIsExist(name, agent)) return
log(agent, `取消文章订阅任务: ${ name }`)
log(agent, `取消文章订阅任务, id: ${ name }`)
schedule.scheduledJobs[`${ name }`].cancel()
}

Expand Down

0 comments on commit 21e7806

Please sign in to comment.