seo-task.js
1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict';
const ROOT_PATH = global.ROOT_PATH;
const util = require(`${ROOT_PATH}/libs/util`);
const schedule = require('node-schedule');
const rp = require('request-promise');
const redis = require(`${ROOT_PATH}/libs/redis`);
const SeoTask = {
intHandle() {
// 每天的1点30分30s执行
schedule.scheduleJob('30 30 1 * * *', () => {
return redis.hvalsAsync('global:yoho:seo:task').then(urls => {
console.log(`SeoTask urls: ${urls}, exec...`);
urls.forEach((item) => {
item = JSON.parse(item || '[]');
this.execTask(item.url, item.time);
});
});
});
console.log('SeoTask 每天的1点30分30s执行 run ...');
},
execTask(url, time) {
return util.sleep(time || 1000).then(() => {
return rp({
uri: url,
resolveWithFullResponse: false, // header、statusCode
json: true,
gzip: true
}).catch(e => {
console.error(`execTask err: ${e.message}`);
return {code: 400, message: e.message};
});
});
}
};
module.exports = SeoTask;