Skip to content

Commit a7adba0

Browse files
committed
feat(error-send-ev): auto update feed url
1 parent 748e6b4 commit a7adba0

File tree

6 files changed

+64
-14
lines changed

6 files changed

+64
-14
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ module.exports = {
8383
'max-depth': 'error',
8484
'max-len': 'off',
8585
'max-lines': 'error',
86-
'max-lines-per-function': 'error',
86+
'max-lines-per-function': 'off',
8787
'max-nested-callbacks': 'error',
8888
'max-params': 'error',
8989
'max-statements': 'off',

i18n/en.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ CANCEL: Canceled
4141
PAGE_PRE: Previous page
4242
PAGE_NEXT: Next page
4343
FOUND_FEED_MORE_THAN_ONE: Found feed more than 1
44+
FOUND_FEEDS: Found feeds
45+
FEED_CHANGE_TO: Feed Change to

i18n/zh-cn.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ CANCEL: 取消操作
4141
PAGE_PRE: 上一页
4242
PAGE_NEXT: 下一页
4343
FOUND_FEED_MORE_THAN_ONE: 找到不止一个订阅地址
44+
FOUND_FEEDS: 找到的订阅地址
45+
FEED_CHANGE_TO: 订阅源地址变为

index.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,12 @@ function startFetchProcess(restartTime) {
195195
logger.error('fetch process exit to much');
196196
process.exit(1);
197197
}
198-
const chid = fork(`utils/fetch.js`);
199-
chid.on('message', function(message) {
198+
let child = process.env.NODE_PRODUTION
199+
? fork(`utils/fetch.js`)
200+
: fork(`utils/fetch.js`, [], {
201+
execArgv: ['--inspect-brk=46209']
202+
});
203+
child.on('message', function(message) {
200204
if (typeof message === 'string') logger.info(message);
201205
else if (message.success) {
202206
const feed = message.eachFeed;
@@ -209,15 +213,27 @@ function startFetchProcess(restartTime) {
209213
bot,
210214
`${feed.feed_title}: <a href="${feed.url}">${
211215
feed.url
212-
}</a> ${i18n['ERROR_MANY_TIME']} ${err}
213-
`,
216+
}</a> ${i18n['ERROR_MANY_TIME']} ${err}`,
214217
feed
215218
);
216219
}
220+
if (message.message === 'CHANGE') {
221+
const { feed, new_feed } = message;
222+
const builder = [];
223+
builder.push(
224+
`${feed.feed_title}: <a href="${feed.url}"></a> ${
225+
i18n['ERROR_MANY_TIME']
226+
}`
227+
);
228+
builder.push(`<b>${i18n['FOUND_FEEDS']}</b>:`);
229+
builder.push(...new_feed);
230+
builder.push(`${i18n['FEED_CHANGE_TO']} ${new_feed[0]}`);
231+
send(bot, builder.join('\n'), feed);
232+
}
217233
}
218234
});
219235

220-
chid.on('exit', function(code, signal) {
236+
child.on('exit', function(code, signal) {
221237
logger.error(`child process exit`);
222238
logger.error({
223239
code,

proxies/rssFeed.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,15 @@ px.handleRedirect = async (url, realUrl) => {
247247
}
248248
};
249249

250+
px.updateFeedUrl = async (oldUrl, newUrl) => {
251+
try {
252+
const db = await dbPomise;
253+
const sql = `UPDATE rss_feed
254+
SET url=?
255+
WHERE url = ?`;
256+
await db.run(sql, newUrl, oldUrl);
257+
} catch (e) {
258+
throw new Error('DB_ERROR');
259+
}
260+
};
250261
module.exports = px;

utils/fetch.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ const hashFeed = require('./hash-feed');
55
const _pick = require('lodash.pick');
66
const schedule = require('node-schedule');
77
const logger = require('./logger');
8+
const feedUtil = require('./feed');
89
const {
910
getAllFeeds,
1011
updateHashList,
1112
failAttempt,
1213
getFeedByUrl,
1314
resetErrorCount,
14-
handleRedirect
15+
handleRedirect,
16+
updateFeedUrl
1517
} = require('../proxies/rssFeed');
1618
const {
1719
notify_error_count,
@@ -20,6 +22,29 @@ const {
2022
concurrency
2123
} = require('../config');
2224

25+
async function handleErr(e, feed) {
26+
logger.info(feed, 'ERROR_MANY_TIME');
27+
process.send({
28+
success: false,
29+
message: 'MAX_TIME',
30+
err: e.message,
31+
feed
32+
});
33+
const originUrl = new URL(feed.url).origin;
34+
const res = await got(originUrl);
35+
const newUrl = await feedUtil.findFeed(res.body, originUrl);
36+
if (newUrl.length > 0) {
37+
updateFeedUrl(feed.url, newUrl[0]);
38+
process.send({
39+
success: false,
40+
message: 'CHANGE',
41+
err: e.message,
42+
new_feed: newUrl,
43+
feed
44+
});
45+
}
46+
}
47+
2348
const fetch = async (feedUrl) => {
2449
try {
2550
logger.debug(`fetching ${feedUrl}`);
@@ -44,13 +69,7 @@ const fetch = async (feedUrl) => {
4469
feed.error_count % round_time === 0 &&
4570
feed.error_count > round_time;
4671
if (feed.error_count === notify_error_count || round_happen) {
47-
logger.info(feed, 'ERROR_MANY_TIME');
48-
process.send({
49-
success: false,
50-
message: 'MAX_TIME',
51-
err: e.message,
52-
feed
53-
});
72+
handleErr(e, feed);
5473
}
5574
if (e instanceof Error && e.response) {
5675
logger.debug(e.response);

0 commit comments

Comments
 (0)