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