seo-task.js
2.09 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict';
const ROOT_PATH = global.ROOT_PATH;
const os = require('os');
const _ = require('lodash');
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 config = require(`${ROOT_PATH}/config/config`);
const jobTask = config.jobTask;
const md5 = require('yoho-md5');
const TASK_KEY_LOG = 'global:yoho:task:log';
const SeoTask = {
intHandle() {
let infos = '';
jobTask.forEach((item, index) => {
infos += `${os.EOL}${(index + 1)}).${item.info}`;
schedule.scheduleJob(item.v, () => {
return redis.hvalsAsync(item.k).then(urls => {
console.log(`task key: ${item.k}, urls: ${JSON.stringify(urls)}, exec...`);
urls.forEach(val => {
val = JSON.parse(val || '[]');
this.execTask(val.url, val.time);
});
});
});
});
console.log(`task creation success...${infos}`);
},
execTask(url, time) {
return util.sleep(time || 50).then(() => {
return rp({
uri: url,
resolveWithFullResponse: false, // header、statusCode
json: true,
gzip: true
}).then(rdata => {
return this.addLog(url, rdata);
}).catch(e => {
return this.addLog(url, {code: e.statusCode || 500, message: e.message});
});
});
},
addLog(url, rdata) {
let key = `${TASK_KEY_LOG}:${md5(url)}`;
console.log(`url: ${url}, result: ${JSON.stringify(rdata)}`);
rdata = _.isArray(rdata) ? rdata : [rdata];
return redis.ltrimAsync(key, 0, 1000).then(() => {
return Promise.all(_.map(
rdata,
item => redis.lpushAsync(key, JSON.stringify(Object.assign({time: Date.now()}, item)))
));
}).then(() => {
return rdata;
});
}
};
module.exports = SeoTask;