seo-task.js 2.09 KB
'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;