owhat-job
Version:
owhat红包清理
335 lines (330 loc) • 15 kB
JavaScript
//console.log = function() {};
import V from 'gcl';
import pro from 'myprocesscontroller';
import OSS from 'ali-oss';
pro.start(async function() {
const conf = V.AppSettings(this.Config, "add_static");
if (!conf) throw new Error('add_static配置获取失败!');
const count = parseInt(conf.count || 1000);
const max = parseInt(conf.max || 1);
const threadcount = conf.threadcount || 1;
console.log('数据库初始化完成', threadcount, max);
const that = this;
that.Temp = this.Middler.getObjectByAppName('Ni', 'MT');
const addRMQ = async function(data) {
//有相同参数且时间比我小5分钟以内的 不插入
//有相同参数且时间比我早5分钟以内的 更新其执行事件为我的执行
//都没有插入
console.log('addRMQ', data);
var res = await that.Temp.excute("add.redis.addstaticmq", {
value: V.toJsonString({
Type: data.ope,
Params: (data.data || []),
})
});
var ret = res.last()[0];
res.clear();
return ret;
};
return new pro.CACProcess(new class extends pro.ADataWorker {
constructor() {
super('wk', threadcount, max);
}
async getData(data) {
console.log('static-schedule获取数据');
let ret = null;
try {
ret = await that.Ni.excute('add.redis.getschedulemq', {
count: count
});
console.log('getschedulemq', ret.hasData() && ret.last()[0]);
if (ret.hasData() && ret.last()[0].length && !ret.last()[0][0].affected) {
let result = [];
const map = {};
ret.last()[0].map(v => {
try {
let value = V.json(v.value);
!map[value.Type] && (map[value.Type] = { type: value.Type, idic: {}, params: [] }, result[result.length] = map[value.Type]);
let { idic, params } = map[value.Type];
(value.Params || []).map(id => {
//这里是数组
id && !idic[id] && (idic[id] = true, params[params.length] = id);
});
//合并数据 譬如多个评论或者首页合并
} catch (ee) {
that.Log.release('add.redis.getschedulemsmq-单条 -' + ee.message + ',json=' + V.toJsonString(v));
console.log('add.redis.getschedulemsmq-单条 -', ee.stack, V.toJsonString(v));
}
});
console.log('add.redis.getschedulemsmq', `获取${ret.last()[0].length}条数据`, result);
return result;
} else if (!ret.hasData()) {
console.log('没有返回数据!', ret.last());
} else {
console.log('奇怪错误 未返回返回数据 也没有此次请求的num');
}
} catch (e) {
that.Log.release('add.redis.getschedulemsmq -' + e.message + ',json=' + V.toJsonString(ret.last()));
console.log('add.redis.getschedulemsmq -', e.stack, ret.last());
}
if (ret) {
ret.clear();
}
return null;
}
async custom(ret, data) {
if (!data.array) data.array = 1;
else data.array++;
if (!data.Log) {
data.Log = that.Log;
data.Ni = that.Middler.getObjectByAppName('Ni', 'templatemanager');
data.Config = that.Config;
}
let res = null;
let params = ret.params;
let _this = this;
try {
switch (ret.type.toLowerCase()) {
case 'banner':
await addRMQ({
ope: 'index.banner',
data: []
});
break;
case '专家':
await addRMQ({
ope: 'index.author',
data: []
});
await addRMQ({
ope: 'authors',
data: []
});
await V.each(params, async function(v) {
await addRMQ({
ope: 'author',
data: v
})
return false;
});
break;
case '文章':
{
await addRMQ({
ope: 'index.article',
data: []
});
await V.each(params, async function(v) {
await addRMQ({
ope: 'article',
data: v
});
await addRMQ({
ope: 'article.comment',
data: v
});
return false;
});
var db = await data.Ni.excute('r_emb', '~.l_article.select', {
$ID: '~=' + params.join(','),
$delFlag: '~=2,3',
authorID: true,
isExcel: true,
_distince: true
});
if (db.hasData()) {
var datas = db.last()[0];
console.log(datas);
var isExcel = false;
var excels = [];
await V.each(datas, async function(v) {
if (v.authorID) {
await addRMQ({
ope: 'author',
data: v.authorID
});
}
isExcel = isExcel || !!v.isExcel;
excels.push(v.ID);
return false;
});
if (isExcel) {
await addRMQ({
ope: 'index.excel',
data: []
});
await addRMQ({
ope: 'index.articleexcel',
data: []
});
await addRMQ({
ope: 'index.excels',
data: []
});
}
}
db.clear();
}
break;
case '评论':
{
//todo 评论变化必须放入redis 包含最近一个季度,文章正常季度
await V.each(params, async function(v) {
await addRMQ({
ope: 'article.comment',
data: v
});
return false;
});
var db = await data.Ni.excute('r_emb', '~.l_article.select', {
$ID: '~=' + params.join(','),
$delFlag: 3,
ID: true,
createTime: true,
commentCount: true,
viewCount: true,
collectCount: true,
_distince: true
});
if (db.hasData()) {
var datas = db.last()[0];
var tables = [];
await V.each(datas, async function(v) {
var createTime = new Date(Date.parse(v.createTime));
if (new Date().sub('d', createTime) < 90) {
tables['comment_last'] = true;
await data.Ni.excute('template9', `hinsert into comment_last<ID> (ID,value) values (?ID,?value) datetime ?expire;`, {
ID: v.ID,
value: V.toJsonString({
commentCount: v.commentCount || 0,
viewCount: v.viewCount || 0,
collectCount: v.collectCount || 0
}),
expire: createTime.add('m', 3).sub('ms', new Date())
});
}
var name = (createTime.getYear() + 1900) + '-' + (Math.floor(createTime.getMonth() / 3) + 1);
tables['comment_' + name] = true;
await data.Ni.excute('template9', `hinsert into comment_${name}<ID> (ID,value) values (?ID,?value);`, {
ID: v.ID,
value: V.toJsonString({
commentCount: v.commentCount || 0,
viewCount: v.viewCount || 0,
collectCount: v.collectCount || 0
})
});
return false;
});
await V.each(Object.keys(tables), async function(v) {
await addRMQ({
ope: 'comment.report',
data: v
});
return false;
});
}
db.clear();
}
break;
case '课题':
await addRMQ({
ope: 'index.topic',
data: []
});
await addRMQ({
ope: 'topics',
data: []
});
await V.each(params, async function(v) {
await addRMQ({
ope: 'topic',
data: v
});
return false;
});
break;
case '资讯':
await addRMQ({
ope: 'index.finance',
data: []
});
break;
case '名言':
await addRMQ({
ope: 'index.motto',
data: []
});
break;
case '类目':
await addRMQ({
ope: 'index.category',
data: []
});
break;
case '合作方':
await addRMQ({
ope: 'index.partner',
data: []
});
break;
case '启动页面':
await addRMQ({
ope: 'index.motto',
data: []
});
await addRMQ({
ope: 'index.finance',
data: []
});
await addRMQ({
ope: 'index.topic',
data: []
});
await addRMQ({
ope: 'index.author',
data: []
});
await addRMQ({
ope: 'index.articlehot',
data: []
});
await addRMQ({
ope: 'index.excel',
data: []
});
break;
default:
console.log('未处理的事务', V.toJsonString(ret));
break;
}
console.log(ret.type + '完成', ret);
} catch (e) {
console.log("单条处理失败" + e.message, V.toJsonString(ret));
that.Log.release("单条处理失败" + e.message + ' ' + V.toJsonString(ret));
} finally {}
if (res) {
res.clear();
}
return false;
}
async rollback(v, data) {
console.log('rollback 抛弃数据', v);
that.Log.release("单条处理放弃" + ' ' + V.toJsonString(v));
return false;
}
async dispose(data) {
try {
await super.dispose(data);
} catch (e) {
console.log('dispose', e.message);
} finally {
//todo 关闭特有资源
delete data.Log;
delete data.Config;
that.Middler.setObjectByAppName('Ni', "MT", data.Ni);
delete data.Ni;
}
return false;
}
}, this.Log, threadcount, 10000);
});