task-log.js
1.13 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
'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;