task-log.js 1.13 KB
'use strict';
const ROOT_PATH = global.ROOT_PATH;
const redis = require(`${ROOT_PATH}/libs/redis`);
const LIST_KEY = 'global:yoho:task:log';
const md5 = require('yoho-md5');

/**
 * 记录队列日志
 */

class TaskLog extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }    

    getKey(link) {
        return md5(link);
    }

    /**
     * [添加]
     * @param {[type]} key   [键]
     * @param {[type]} field [日志内容,对象{code:200, message:'', time: 0}]
     */
    add(key, field) {
        return redis.lpushAsync(`${LIST_KEY}:${key}`, JSON.stringify(Object.assign({time: Date.now()}, field)));
    }

    /**
     * [修剪]
     * @param  {[type]} key   [键]
     * @param  {[type]} start [开始位置]
     * @param  {[type]} stop  [结束位置]
     * @return {[type]}       []
     */
    ltrim(key, start, stop) {
        return redis.ltrimAsync(`${LIST_KEY}:${key}`, start || 0, stop || 1000);
    }

    /**
     * [删除]
     * @param  {[type]} key [键]
     * @return {[type]}     []
     */
    del(key) {
        return redis.delAsync(`${LIST_KEY}:${key}`);
    }
}

module.exports = TaskLog;